How to fetch Twitter users with R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This is the first one of a 3-posts-series, where I go from fetching Twitter users and preparing the data to visualizing it (If I wanted to show everything I’ve done in a single post, it would be almost as long as my first one! And believe me: nobody wants that ???? ):
- How to fetch Twitter users with R: this one, the title is kind of self explanatory…
- How to deal with ggplotly huge maps: where I go through the details of why I chose not to use
ggplotly
and useplot_geo
instead to generate the HTML. - How to plot animated maps with gganimate: again, pretty obvious subject.
Finally I present my favourite visualization here.
I should warn you that there are a lot of emojis in this series, courtesy of the emo
package Hadley recently released and I fanatically adopted ????
Let’s get started!
Getting Twitter users
I had to learn how to retrieve data from the Twitter API, and I chose to use the rtweet
package, which is super easy to use! Since I only use public data I don’t have to worry about getting my Twitter personal access token.
Every R-Ladies’ chapter uses a standard handle, with the RLadiesLocation format (thankfully they are very compliant with this!). I use the rtweet::search_users
function, setting the query to be searched with q = 'RLadies'
and the number of users to retrieve with n = 1000
, that being the maximum from a single search. As I want a dataframe as a result, I set the parse
parameter to TRUE
. This way I get 1,000 rows of users, with 36 variables regarding them. I’m only showing the variables I’m going to use, but there is a lot of extra information there.
library(rtweet)
users <- search_users(q = 'RLadies',
n = 1000,
parse = TRUE)
Let’s see what it returns:
library(DT)
datatable(users[, c(2:5)], rownames = FALSE,
options = list(pageLength = 5))
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.