This month I got a chance to work with R and I must say it does wonders with almost no code, definitely a must try if you work with a lot of data and infographics. The existing packages and libraries are vast and cover almost all of the major APIs you would be working with. In the instructions below I was trying to work with the twitter APIs.
So here it goes, step by step instructions to get you started with R:
- Install R software from the link – https://cran.r-project.org/bin/windows/base/
- After installing R software, open the installed R GUI and then you would need the package that integrates R with Twitter. The library name is twitteR. To install this library, in the R interface you need to go to Packages->Install Packages->From the list select twitteR and click on Ok. Also install the package ‘bit64’ and ‘base64enc’ in case it’s not automatically installed, the exact same way as its mentioned before.
- So when you start working with R the first step that needs to be done is loading up the library that we are working with for which the one line syntax is:
library(twitteR) //This will load up the twitteR library that we would be using.
- The twitter APIs use OAuth for authentication for us to access their data. To get this firstly you need to create an application for yourself at https://twitter.com/apps/new. Here you will get your API key, API secret, Access token, Access secret and use the details in this command in the given format:
setup_twitter_oauth(“API key”, “API secret”,”Access token”,”Access secret”) //This would authenticate you to access twitter’s data for the active R session.
- r_tweets <- searchTwitter(“#chennaifloods”, n=300) //This would load up the tweets with the given hash tag here we are doing for Chennai floods and n signifies the number of tweets to be retrieved.
- head(tweets) // This would load up just the top 5-6 tweets from the tweets retrieved.
tweets // This would load up all the tweets from the tweets retrieve.
Example Sample for visualizations:
There is also a sample code below for doing visualizations with twitter data:
Loading required package: RCurl
Loading required package: bitops
Loading required package: RJSONIO
- result <- userTimeline(“BarackObama”, n=3200)
- df <- twListToDF(result)
- Created <- tweet.df$created
- counts <- table(as.Date(Created))[-1]
- dates <- as.Date(names(counts))
- # png(“twitteR-BarackPosts.png”, 650, 500)
- plot(dates, counts, type=”h”)
- # dev.off()
Let me know via the comments below if you get stuck anywhere.