Over at Cerebral Mastication, you can find the code for a function that will allow you to send a status update to Twitter directly from the R console. The code's pretty short, so here it is in full:
library("RCurl")
opts <- curlOptions(header = FALSE,
userpwd = "username:password", netrc = FALSE)
tweet <- function(status){
method <- "http://twitter.com/statuses/update.xml?status="
encoded_status <- URLencode(status)
request <- paste(method,encoded_status,sep = "")
postForm(request,.opts = opts)
}
With this function, you can send a tweet simply by using the update function:
tweet("This tweet comes from R! #rstats")
(CM called the function
update, but that overloads an existing function in R.) Now, you may be asking yourself, "Why would anyone want to do this?". And it's an excellent question: why use the R console when there are plenty of good applications that interface with Twitter already? But consider: what if you had an automated R application running in production, and needed to alert a large number of users when an event occurs: a long simulation has completed, a stock reaches a predetermined price, a chemical assay has detected a compound of interest. Now, with just a few lines of code and the
RCurl package, you have a way of doing just that.
Or... if you really need to spend time in R, and shouldn't be wasting time with your favorite Web 2.0 watering holes, you can just update your tweets from R. I have done this from Excel but from R is just way much more cool. I might even tweet excellent tricks I learn in R at the times I learn them.
Posted by: Nicole Radziwill | June 10, 2009 at 11:46
Problem is, this depends on Rcurl and Revolution R 64-bit doesn't provide it. And it's really hard to learn when new packages are added, because there's no easy way to diff the list of packages that exists now vs existed yesterday. And this is not the only package that is missing.
Posted by: J | June 11, 2009 at 07:21
Thanks for the link back, Davo. I'm going to get your twitter handle tatooed to my ass in appreciation! So I've had some prompting to do a post on how to grab data online with RCurl and then parse it with R. Why don't you make my life easier and do a post on the topic and I'll just link to you... and drink beer. I'm lazy and you're good at this crap. If you don't know what data to use, you could start with ftp://ftp.cme.com/settle/. You know, if you don't have anything better to do.
Posted by: j.d. long | June 11, 2009 at 17:53
BTW, your syntax is all pretty colors and stuff. Are you using a syntactic blog add in? Which one and how do you have it configured?
Posted by: j.d. long | June 11, 2009 at 18:28
@j.d. long: I'll look out for a good example of online data processing. And for the pretty code, I just cut'n'paste from the REvolution R GUI on the Mac -- it has syntax highlighting.
Posted by: David Smith | June 11, 2009 at 18:55
I saw a demonstration of this a long time ago. I tried to do this using the code just now, but I get the following R error:
[1] "\n\n Basic authentication is not supported\n\n"
attr(,"Content-Type")
charset
"application/xml" "utf-8"
Warning message:
In postForm(request, .opts = opts) : No inputs passed to form
Has something changed with Twitter such that this code needs to be amended?
Posted by: e | November 22, 2010 at 20:31