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.
<span class="n">library</span><span class="p">(</span><span class="n">rtweet</span><span class="p">)</span><span class="w">
</span><span class="n">users</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">search_users</span><span class="p">(</span><span class="n">q</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'RLadies'</span><span class="p">,</span><span class="w">
</span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">1000</span><span class="p">,</span><span class="w">
</span><span class="n">parse</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w">
</span>
Let’s see what it returns:
<span class="n">library</span><span class="p">(</span><span class="n">DT</span><span class="p">)</span><span class="w">
</span><span class="n">datatable</span><span class="p">(</span><span class="n">users</span><span class="p">[,</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="o">:</span><span class="m">5</span><span class="p">)],</span><span class="w"> </span><span class="n">rownames</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">,</span><span class="w">
</span><span class="n">options</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">list</span><span class="p">(</span><span class="n">pageLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">5</span><span class="p">))</span><span class="w">
</span>
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.