Twitter from R… Sure, why not!


So I have started following the #RStats tag in twitter. Prior to a week ago I had never Twitterbated so I thought I would give it a go since I am not one to shy away from new technology… much. I think of Twitter like a call in radio show where I get to cut off callers when they annoy me.

Well one of the interesting things I ran across was this tweet that pointed to this page about posting tweets from R.  The code example was missing a couple of things, so here’s the cleaned up version:

install.packages("RCurl")
library("RCurl")
opts = curlOptions(header = FALSE, userpwd = "username:password", netrc = FALSE)

update <- function(status){
  method <- "http://twitter.com/statuses/update.xml?status="
  encoded_status <- URLencode(status)
  request <- paste(method,encoded_status,sep = "")
  postForm(request,.opts = opts)
}
update("My first tweet from R! @CMastication is my daddy! #RStats")

This is a pretty good, and fairly self explanatory example of how to use RCurl to spew out some data. Don’t be a scriptard and try to run this without changing ‘username’ and ‘password.’ If you do, I’m coming to your house and going to beat you with your own keyboard until you admit that I am, indeed, your daddy.

Am I planning on Tweeting from R? You have to be kidding. That’s just silly. But I might need to use RCurl again. Although for long running scripts I may very well put code at the end of my scripts that sends out a Tweet when the code has finished running. Waaaay easier than setting up email and trying to ensure it runs from EC2 as well as my home machine, etc. The more I think about that, the more I am sure I’m a friggin genius. And of course, on every one of my script completion tweets I’m going to use the #RStats tag so everyone in twitterland will know that I am amazing. And a prick. All at the same time. I’m complex like that.

Oh yeah, I’m @CMastication on Twitter.

13 Comments

  1. Eva says:

    Hey,

    The tweet you refer to was an RT of an HT of one of my tweets, and just to get the referencing right I now made an effort to find out who wrote the code – it was @sebastiansuncle

    Yeah, great and yeah not going to use it a lot. Although I don`t think it`s silly. I use Tinn-R, and often scribble on future tweets and other stuff there anyways. You can tweet without leaving your work and getting distracted by all those other twitterers…

    Eva or @Ehva if you like :)

  2. Shu says:

    Neat! The first thought that came to mind for using RCurl was pulling data of the internet directly to analyse, but then it could probably be more efficiently done with other scripting languages to munge the data and thrown into R for analysis.

  3. jebyrnes says:

    That’s actually quite useful. One can setup a script that will take a loooong time to finish on a server, and have it tweet you with results as they come out (or when it is done). This could also be quite useful for a company who is getting constantly updated data, and wants to tweet something to its client/userbase when the daily/hourly/whatever data is munched.

    Now if only there was a way to make it say that it was posted from R instead of the web….

  4. J says:

    jebyrnes, I banged around for an hour or two trying to get the “posted from R” (actually I wanted to make it say “Posted from R, Bitches” because I am rude like that) to appear instead of “posted from web” There are lots of examples online of how to set the “source” parameter so that the source application text is changed. Only after wasting hours did I discover that the twitterfuckers have discontinued that feature. I found that tidbit in the FAQ: http://apiwiki.twitter.com/FAQ here’s the quote:

    “How do I get “from [MyApp]” appended to updates sent from my API application?

    We now recommend developers use OAuth to perform authentication with the API. When applications use OAuth, Twitter automatically knows the source of status updates. We are therefore able to append source attribution (from “[MyApp]“) to tweets. If you would like tweets from your application to recieve a source parameter, please register an application and implement OAuth authentication. We will automatically include your application as the source for any tweets sent from your application.

    We originally allowed applications to create a source paramter for non-OAuth use but that has been discontinued. Applications pre-OAuth source parameters will remain active, but new registrations are no longer accepted.”

    I was lead astray since there are so many examples online that use the “source” tag. Grrrrrr.

  5. J says:

    I’m not sure why the trackbackometer didn’t pick this up, but Dave over at Revolutions blogged about this as well: http://blog.revolution-computing.com/2009/06/tweeting-from-r.html

    yeah, I hate formatting links in comments. damn

  6. Eva says:

    If you feel an urge to, you can automatically express your feelings on Twitter in this way, based on your results in R.

    Tiny example:
    > TheSum if (TheSum != 100) tweet(‘O-oh! @Ehva must look for the needle in the haystack’) else tweet(‘Happiness!’)

    http://twitter.com/Ehva/statuses/2220116077

    if I had this, the tweet would be:
    > TheSum <- 67+16+17 # Set4 Variable2

    Note: I name the function “tweet” instead of “update”.

    @Ehva

  7. Eva says:

    If you feel an urge to, you can automatically express your feelings on Twitter in this way, based on your results in R…

    Tiny example:
    > TheSum if (TheSum != 100) tweet(’O-oh! @Ehva must look for the needle in the haystack’) else tweet(’Happiness!’)

    http://twitter.com/Ehva/statuses/2220116077

    had I had the data
    > TheSum <- 67+16+17 # Set4 Variable2
    the tweet would express another feeling :)

    Note: I name the function “tweet” instead of “update”.

    @Ehva

    PS: Sorry, I had to post again, there were errors in the first comment.

  8. Eva says:

    If you feel an urge to, you can automatically express your feelings on Twitter in this way, based on your results in R…

    Tiny example:
    > TheSum if (TheSum != 100) tweet(’O-oh! @Ehva must look for the needle in the haystack’) else tweet(’Happiness!’)

    http://twitter.com/Ehva/statuses/2220116077

    had I had the data
    > TheSum <- 67+16+17 # Set4 Variable2
    the tweet would express another feeling :)

    Note: I name the function “tweet” instead of “update”.

    @Ehva

    (PS: Terribly sorry about those previous comments with missing lines!)

  9. Eva says:

    If you feel an urge to, you can automatically express your feelings on Twitter in this way, based on your results in R…

    Tiny example:
    > TheSum if (TheSum != 100) tweet(’O-oh! @Ehva must look for the needle in the haystack’) else tweet(’Happiness!’)

    http://twitter.com/Ehva/statuses/2220116077

    had I had the data
    > TheSum <- 50+50 #No2
    the tweet would express another feeling :)

    Note: I name the function “tweet” instead of “update”.

    @Ehva

    (PS: Terribly sorry about those previous comments with missing lines!)

  10. Eva says:

    If you feel an urge to, you can automatically express your feelings on Twitter in this way, based on your results in R…

    Tiny example:

    > TheSum if (TheSum != 100) tweet(’O-oh! @Ehva must look for the needle in the haystack’) else tweet(’Happiness!’)

    http://twitter.com/Ehva/statuses/2220116077

    had I had the data
    > TheSum <- 50+50 # No2
    the tweet would express another feeling :)

    Note: I name the function “tweet” instead of “update”.

    @Ehva

    (PS: Terribly sorry about those previous comments with missing lines!)

  11. Eva says:

    Correction: After “Tiny example:”, it is supposed be:

    > TheSum if (TheSum != 100) tweet(’O-oh! @Ehva must look for the needle in the haystack’) else tweet(’Happiness!’)

  12. e says:

    realize this is a late post. this sounds like a really nice feature. tried to implement this tonight, and it didn’t work. was wondering if things have changed on the twitter page that requires some new tweaks? Thanks for sharing this!!

  13. JD Long says:

    E, Twitter now requires OAuth which is not easy at all to implement in a system like R. I’ve been trying to get someone smarter than me to give this some thought but have not come up with anything yet. http://stackoverflow.com/questions/3391460/oauth-access-for-r

Leave a Reply