<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/a-call-to-tweets-blog-posts/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/10/22/a-call-to-tweets-blog-posts/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Way back in July of 2009, the first version of the <code>twitteR</code> package was published by <a rel="nofollow" href="https://github.com/geoffjentry/twitteR" target="_blank">Geoff Jentry</a> in CRAN. Since then it has seen 28 updates, finally breaking the 0.x.y barrier into 1.x.y territory in March of 2013 and receiving it’s last update in July of 2015.</p>
<p>For a very long time, the <code>twitteR</code> package was <em>the way</em> to siphon precious nuggets of 140 character data from that platform and is the top hit when one searches for <a rel="nofollow" href="https://www.google.com/search?q=r+twitter+package" target="_blank"><code>r twitter package</code></a>. It even ha[sd] it’s own <a rel="nofollow" href="http://lists.hexdump.org/pipermail/twitter-users-hexdump.org/" target="_blank">mailing list</a> and is quite popular, judging by RStudio’s CRAN logs total downloads stats <img alt="" src="http://cranlogs.r-pkg.org/badges/grand-total/twitteR">.</p>
<p>I blog today to suggest there is a better way to work with Twitter data from R, especially if your central use-case is searching Twitter and mining tweet data. This new way is <a rel="nofollow" href="https://cran.rstudio.com/web/packages/rtweet/" target="_blank"><code>rtweet</code></a> by <a rel="nofollow" href="https://github.com/mkearney/rtweet/" target="_blank">Michael Kearney</a>. It popped up on the scene back in August of 2016 and receives quite a bit of <img alt="💙" src="https://s.w.org/images/core/emoji/2.3/72x72/1f499.png" class="wp-smiley"> from the developer, especially <a rel="nofollow" href="https://github.com/mkearney/rtweet/" target="_blank">on GitHub</a>.</p>
<p>This post is short and mostly designed to convince you to (a) try out the package and (b) blog and tweet about the package — if you do agree that it’s the best modern way to work with Twitter from R — to raise awareness about it. Because of that focus, I won’t be delving into all of <code>rtweet</code>‘s seekrits, but you can explore them yourself on it’s spiffy <a rel="nofollow" href="http://rtweet.info/" target="_blank">pkgdown site</a>.</p>
<p>While both packages have nigh complete access to the Twitter API, I posit that the quintessential use-case for working with Twitter in R is searching through tweets/users and then performing various types of data mining on the retrieved results. To that end, I’m going to show one use-case (out of many potential ones) that will both save you API-time and post-API munging time in order to convince you to switch to <code>rtweet</code> and spread the word about it.</p>
<h3>Data-mining 300 <code>#rstats</code> Tweets : A Play in Two Acts</h3>
<p>We’ll search Twitter for <code>#rstats</code>-tagged tweets with both <code>twitteR</code> and <code>rtweet</code>, starting with the former:</p>
<pre><code>library(twitteR)

# this relies on you setting up an app in apps.twitter.com
setup_twitter_oauth(
  consumer_key = Sys.getenv(&quot;TWITTER_CONSUMER_KEY&quot;), 
  consumer_secret = Sys.getenv(&quot;TWITTER_CONSUMER_SECRET&quot;)
)

r_folks &lt;- searchTwitter(&quot;#rstats&quot;, n=300)

str(r_folks, 1)
## List of 300
##  $ :Reference class 'status' [package &quot;twitteR&quot;] with 17 fields
##   ..and 53 methods, of which 39 are  possibly relevant
##  $ :Reference class 'status' [package &quot;twitteR&quot;] with 17 fields
##   ..and 53 methods, of which 39 are  possibly relevant
##  $ :Reference class 'status' [package &quot;twitteR&quot;] with 17 fields
##   ..and 53 methods, of which 39 are  possibly relevant

str(r_folks[1])
## List of 1
##  $ :Reference class 'status' [package &quot;twitteR&quot;] with 17 fields
##   ..$ text         : chr &quot;RT @historying: Wow. This is an enormously helpful tutorial by @vivalosburros for anyone interested in mapping &quot;| __truncated__
##   ..$ favorited    : logi FALSE
##   ..$ favoriteCount: num 0
##   ..$ replyToSN    : chr(0) 
##   ..$ created      : POSIXct[1:1], format: &quot;2017-10-22 17:18:31&quot;
##   ..$ truncated    : logi FALSE
##   ..$ replyToSID   : chr(0) 
##   ..$ id           : chr &quot;922150185916157952&quot;
##   ..$ replyToUID   : chr(0) 
##   ..$ statusSource : chr &quot;<a rel="nofollow" href="https://www.r-bloggers.com/a-call-to-tweets-blog-posts/\%22http://twitter.com/download/android\%22" target="_blank">Twitter for Android</a>&quot;
##   ..$ screenName   : chr &quot;jasonrhody&quot;
##   ..$ retweetCount : num 3
##   ..$ isRetweet    : logi TRUE
##   ..$ retweeted    : logi FALSE
##   ..$ longitude    : chr(0) 
##   ..$ latitude     : chr(0) 
##   ..$ urls         :'data.frame': 0 obs. of  4 variables:
##   .. ..$ url         : chr(0) 
##   .. ..$ expanded_url: chr(0) 
##   .. ..$ dispaly_url : chr(0) 
##   .. ..$ indices     : num(0) 
##   ..and 53 methods, of which 39 are  possibly relevant:
##   ..  getCreated, getFavoriteCount, getFavorited, getId, getIsRetweet, getLatitude, getLongitude, getReplyToSID,
##   ..  getReplyToSN, getReplyToUID, getRetweetCount, getRetweeted, getRetweeters, getRetweets, getScreenName,
##   ..  getStatusSource, getText, getTruncated, getUrls, initialize, setCreated, setFavoriteCount, setFavorited, setId,
##   ..  setIsRetweet, setLatitude, setLongitude, setReplyToSID, setReplyToSN, setReplyToUID, setRetweetCount,
##   ..  setRetweeted, setScreenName, setStatusSource, setText, setTruncated, setUrls, toDataFrame, toDataFrame#twitterObj</code></pre>
<p>Both packages follow the similar idioms and you need to have done some prep-work by creating a Twitter “app” (both packages have instructions for that).</p>
<p>That operation took about 3 seconds on a fast internet connection and wicked fast computer. What you get back is definitely usable data, but it’s in lists of custom objects. This is due to the way that package models the Twitter API on to custom R objects. It’s elegant, but also likely overkill for most operations. You can use something like <code>purrr::map_df(r_folks, as.data.frame)</code> to get that list into a data frame, there are some other “gotchas”, such as text encoding (on a later run of this code both <code>dplyr::glimpse()</code> and <code>str()</code> gave me <em>“invalid multibyte string”</em> errors but that same thing did not happen with <code>rtweet</code>.</p>
<p>Here’s the <code>rtweet</code> version:</p>
<pre><code>library(rtweet)

# this relies on you setting up an app in apps.twitter.com
create_token(
  app = Sys.getenv(&quot;TWITTER_APP&quot;),
  consumer_key = Sys.getenv(&quot;TWITTER_CONSUMER_KEY&quot;), 
  consumer_secret = Sys.getenv(&quot;TWITTER_CONSUMER_SECRET&quot;)
) -> twitter_token

saveRDS(twitter_token, &quot;~/.rtweet-oauth.rds&quot;)

# ideally put this in ~/.Renviron
Sys.setenv(TWITTER_PAT=path.expand(&quot;~/.rtweet-oauth.rds&quot;))

rtweet_folks &lt;- search_tweets(&quot;#rstats&quot;, n=300)

dplyr::glimpse(rtweet_folks)
## Observations: 300
## Variables: 35
## $ screen_name                    </code></pre>