3 min read

The 12 R functions of Xmas pt4/6

R fun at Xmas

R fun at Xmas

Wow, we’re already up to part 4 of this little series! If you want to go back and see where we started, check out this post.

Today’s two (mostly pointless) R functions are evolutions of things we seen before. In the first we have more URL manipulation and in the second, more fun with sample().

Simple tweet

In the 2nd installment we wrote a small function to launch a gif search on Giphy.com. For that function we constructed a URL inside the function and then launched a browser with the result. This function does more of the same.

Twitter maintain a service that allows website owners to implement a simple “Tweet” button on their site. This button allows visitors to share content from that site to the social network. Since this service uses a URL to post the content to Twitter it’s ripe for our R function treatment. Like the Giphy search function this one will build a URL for us and then launch the URL in the default browser.

Note: If you want to do any real work with twitter I’d recommend the rtweet package from Mike Kearney.

simple_tweet <- function(msg){
  intent_url <- "https://twitter.com/intent/tweet?text="
  content <- URLencode(msg)
  hashtags <- "rstats,r12fun"
  url <- paste0(intent_url, content, "&hashtags=", hashtags)

The function builds the URL we need, like this one: https://twitter.com/intent/tweet?text=This%20is%20your%20message&hashtags=rstats,r12fun

First your message is URL encoded - which is just a fancy way of saying that characters like " " (space), which are not permissible in URLs, are replaced with the URL-safe equivalents like “%20”. After that the hashtags, which are hard-coded in this version of the function, are added and the URL assembled before browseURL() launches it in your default browser. Naturally, you’ll need a Twitter account for this one!

simple_tweet launches Twitter’s Tweet posting web page

simple_tweet launches Twitter’s Tweet posting web page

The tweet posting page is pre-filled using the data that Twitter obtains from the URL you visited and if you hit the “Tweet” button your tweet will be posted.

Some ideas for modifications and improvements:

  • Make the hashtags configurable
  • Check that the supplied message isn’t above the 280 character limit
  • Do other social networks offer a similar service?

Magic R ball

The Magic 8 ball is a classic pointless Christmas present given to children and adults alike. And this R version is equally pointless!

magic_R_ball <- function(){
  answers <- c(
    "It is certain.",
    "It is decidedly so.",
    "Without a doubt.",
    "Yes - definitely.",
    "You may rely on it.",
    "As I see it, yes.",
    "Most likely.",
    "Outlook good.",
    "Signs point to yes.",
    "Reply hazy, try again.",
    "Ask again later.",
    "Better not tell you now.",
    "Cannot predict now.",
    "Concentrate and ask again.",
    "Don't count on it.",
    "My reply is no.",
    "My sources say no.",
    "Outlook not so good.",
    "Very doubtful."
  answer <- answers[sample(1:length(answers), 1)]

Run the function to receive one of the 20 standard Magic 8 ball answers. The function chooses a random item (There’s sample() again!) from the list of possible responses.

> magic_R_ball()
Cannot predict now.

The answers were all taken verbatim from the Magic 8 Ball wikipedia page.

Keep this function handy for when you’re working in R and need some clarity on an important question but there’s nobody around to ask.

Ideas for improvements:

  • Write your own R or data science related answers, eg. “Methodology flawed”
  • Implement a parameter to switch between different answer types, eg. “8ball” or “Data Science”

That’s it for today. Check back tomorrow for two more similarly pointless little bits of R fun for the festive period.