Tweeting from R

June 10, 2009
48 Views

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

You may be interested

How SAP Hana is Driving Big Data Startups
Big Data
298 shares2,909 views
Big Data
298 shares2,909 views

How SAP Hana is Driving Big Data Startups

Ryan Kh - July 20, 2017

The first version of SAP Hana was released in 2010, before Hadoop and other big data extraction tools were introduced.…

Data Erasing Software vs Physical Destruction: Sustainable Way of Data Deletion
Data Management
42 views
Data Management
42 views

Data Erasing Software vs Physical Destruction: Sustainable Way of Data Deletion

Manish Bhickta - July 20, 2017

Physical Data destruction techniques are efficient enough to destroy data, but they can never be considered eco-friendly. On the other…

10 Simple Rules for Creating a Good Data Management Plan
Data Management
69 shares623 views
Data Management
69 shares623 views

10 Simple Rules for Creating a Good Data Management Plan

GloriaKopp - July 20, 2017

Part of business planning is arranging how data will be used in the development of a project. This is why…