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">

Tweeting from R

4 Min Read

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,

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.

Cerebral Mastication: Twitter from R… Sure, Why Not!

Link to original post

Share This Article
Exit mobile version