The 12 R functions of Xmas pt2/6

R at Xmas{width=100%}

Yesterday I posted the first two of the 12 small and hopefully fun R functions that I challenged myself to write last weekend. If you read that post, you’ll already get the idea. They’re small and fun functions that also do something small and fun!

I wanted to stick to no more than a single function (though I may have cheated a bit in at least one of them) in order to ensure that the scope stayed small and I didn’t distract myself with too many unnecessary features. I also wanted stuff that I could use to help teach my kids more about programming, so a lack of complexity is a benefit there too.

Today’s two functions are both really short. The first has a little URL manipulation and the second is just a straight wrapper around sample().

It seems that pretty much everyone likes an animated gif (with a hard “g”, none of that “jif” nonsense!), so wouldn’t it be great if you could begin a gif search directly from inside R?

Of course it would!

For those who haven’t heard of it, Giphy is a website that let’s you create and share animated gifs. Kind of like Youtube but for gifs.

This function will launch a search on the website from inside R, so you’ll run the function in RStudio and end up seeing the Giphy search page in your browser.

giphy_search <- function(text = "christmas"){
  search_string <- gsub(" ", "-", text)
  search_url <- paste0("", search_string)

Giphy use a specific search URL, so this function takes your input (or uses the default, “christmas”), swaps any spaces it finds for dashes and then appends it to the search URL. Finally, browseURL() attempts to open the URL we’ve built in the default browser.

Giphy search for &ldquo;christmas&rdquo;

Essentially what we’re doing here is constructing the URL outside of Giphy and then visiting it. Normally this would be taken care of by the search box on the Giphy site, but that’s clearly not as much fun! It also sidesteps any questions about ethics and web scraping by just taking you directly to the site, just as if you’d visited it yourself.

Some ideas for improvements:

  • Input validation
  • Search on other gif sites as well
  • Could you use it to search other places as well?

Roll dice

Christmas is a great time for hanging out with your family and friends and playing a few board games, but so often lost dice will ruin the fun. Well, no more!

roll_dice <- function(num_dice = 1){
  for (roll in 1:num_dice){
    cat(sample(1:6, 1), " ")

This function is among the smallest in the collection and just outputs a random number from 1 to 6. The optional num_dice parameter lets you control the number of dice that you roll, in case you need to roll more than one.

> roll_dice(5)
2  3  3  1  5 

The interesting thing about this one is the number of ways it could be adapted or extended. It could even form the basis of a complete game like Sellors-family-favourite, Yacht.

Some ideas for variations and improvements:

  • Write your own coin flip function
  • Support different types of dice, with different number of sides or with different things on the faces
  • Build your own “Story Cubes” game - great to play with younger kids
  • Add a mechanism for re-rolling some of the dice
  • use as the basis for a larger game
  • Build it into a shiny app

That’s it for today. Tomorrow we’re playing “guess what number I’m thinking of” and pretending to be in Sci-Fi movies, so check back then. And as ever, if you do play around with any of this stuff, let me know.