Today @oppenheimmd re-tweeted a nice tweet about building Twitter Followers Map with R. Certainly, I decided to build my own map and here it is:
Total number of followers on the screen is smaller than in Twitter. I think it happens because not all people specified the location in the account settings. Take a note that to be able execute this script you need to specify the location in your Twitter account.
As you probably understand from the title, I did this picture using RProvider instead of executing existing R code. Actually, use of twitterMap.R is pretty simple, if not to pay attention to difficulties with Twitter authorization and SSL certificate validation (this part is ugly a bit).
For this demo we need two R packages twitteR and RCurl (with all their dependencies). Please install them:
#I @"..\packages\RProvider.1.0.5\" #load "RProvider.fsx" //open RProvider.utils //R.install_packages("twitteR") //R.install_packages("RCurl") open RDotNet open RProvider open RProvider.utils open RProvider.``base`` open RProvider.twitteR open RProvider.RCurl open RProvider.ROAuth
I am lazy a bit to fight with RProvider syntax in some places. Actually, I do not even know if it is possible to rewrite such R code using RProvider or not… I have decided to cheat a bit and define a function that gets R expression as a string and evaluates it.
let eval (text:string) = namedParams ["text", box text] |> R.parse |> R.eval
You need to have consumerKey and consumerSecret from your registered Twitter application. If you do not have such ones yet, please follow the steps from this article that helps you to register a new Twitter application. The following code is tended to authenticate you in Twitter:
let twitCred = namedParams [ // TODO: insert your consumerKey and consumerSecret "consumerKey", box "xxxxxxxxxxxxxxxxxxxxxx" "consumerSecret", box "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" "requestURL", box "https://api.twitter.com/oauth/request_token" "accessURL", box "http://api.twitter.com/oauth/access_token" "authURL", box "http://api.twitter.com/oauth/authorize" ] |> R.OAuthFactory R.download_file(url = "http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem") R.assign("twitCred", twitCred) eval """twitCred$handshake(cainfo="cacert.pem")"""
Here you need to do some manual work. These are the last authentication steps:
- Copy URL from FSI window and paste it in your browser
- Allow your twitter app to access your account data
- Copy authorization number-code from browser
- Paste code in FSI and press Enter
After that, you need to save your authorization data and set SSL certificate to be used globally, which allow twitterMap.R to communicate with Twitter under your account.
R.registerTwitterOAuth(twitCred) eval """options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))"""
The last step is to run the script and plot your own map:
R.source("http://biostat.jhsph.edu/~jleek/code/twitterMap.R") // TODO: do not forget to specify your twitter login and increase nMax if you have more than 5000 followers eval """twitterMap("your_login", fileName="d:\\TwitterMap.pdf", nMax=5000)"""
P.S. If you rewrite this script without eval please post a link in comments 😉