The 12 R functions of Xmas

R at Xmas

For the other posts in this series check out the following links:

A couple of weeks ago I tweeted about coding as a form of procrastination. It’s something I do quite a bit, but I also do something far more annoying that keeps me from coding altogether.

You see, I am a frequent victim of the “creepy feature creature”. This mythical beast urges all programmers to add more features to their programs whether they really need them or not. My own personal creepy feature creature strikes me earlier than that though, while I’m still formulating my ideas.

Having ideas is literally part of my job, so this is no small issue. I write very little code for work these days though, so the main impact is in my personal projects.

I’ll have an idea which seems pretty good in principal, but instead of coding up that idea and then iterating, I get carried away with the idea itself and will iterate on that instead. New features get added before I’ve even written a single line of code, round and round, over and over. Often this will spiral to such an extent that the previously simple project then seems unmanageably large and I don’t bother starting it at all.

One such idea was “the barcode game”. I’d been meaning to write a game that used the barcode scanner that I have lying around in one of my “misc technology” boxes. Initially the idea was pretty straightforward. Scan a couple of barcodes and see who wins, but by the time I’d been through a few mental iterations it was a sprawling thing with a shiny app and images and all sorts of stuff.

Needless to say, I never started that version. Too much other stuff to do.

I also wanted more code examples in R that I could share with my kids (aged 8 and 10), to help foster their interest in programming. They have some interest already, but it’s not always easy to find things that either capture their imaginations or that don’t become too complex too quickly.

And so was born “the 12 R functions of Xmas”. The plan: to write 12 small and simple R functions to do small and simple things and to get it done as quickly as possible. And to start it all off, the barcode game!

So I forced myself to write a really simple version of the barcode game and 11 other little functions over the course of about 36 hours at the weekend and the plan now is to share them here, along with some ideas for how they could be extended in case it’s useful for others. I’ll try and write up 2 per day until they’re all gone and I’ll be including some ideas for improvements you could try to make for each if you’re interested.

Some are tiny – just a couple of lines of code – and some are a fair bit larger. But even the longest is less than 35 lines. Creepy feature creature be damned!

The barcode game

Note: Some people would probably prefer to call this “the UPC game”!

Two players each pick a barcode (or UPC if you prefer) from around the house, scan the code (or type it in) and the winner is the player whose individual barcode numbers add up to the highest number.

My son and his friend (aged 11) had great fun running around the house looking for new barcodes to scan and trying to guess which one would win.

Player 1 wins!

We actually used ours with an old barcode scanner I had lying around (from another project), but you can just type the numbers in and it works just as well. The scanner we have just pretends to be a USB keyboard, so the code remains the same in either case.

barcode_game <- function(){
  num1 <- readline(prompt = "Player 1 scan your barcode > ")
  num2 <- readline(prompt = "Player 2 scan your barcode > ")

  result1 <- sum(as.numeric(strsplit(num1, "")[[1]]))
  result2 <- sum(as.numeric(strsplit(num2, "")[[1]]))

  if (result1 > result2){
    cat("Player 1 wins!")
  } else if (result2 > result1){
    cat("Player 2 wins!")
  } else {
    cat("It's a draw!")

Some ideas for improvements:

  • A more interesting algorithm to decide the winner
  • some input validation
  • multiple rounds to decide the overall winner
  • A shiny app!

How many days until Xmas?

This is one of the really short ones. Run it and it’ll tell you how many days left until Xmas day.

days_until_xmas <- function(){
  cat(359 - as.numeric(format(Sys.Date(), "%j")))

The %j used for the format string returns the day of the year. For example, today is the 345th day of the year and Christmas day is the 359th day of the year.

> days_until_xmas()

Ideas for improvement:

  • This version breaks on leap years
  • What about other interesting dates? New year, Halloween, Thanksgiving etc.

That’s it for today - two more tomorrow. And if you do decide to play around with any of these be sure to let me know!