<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/tutorial-how-to-set-up-a-twitter-bot-using-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.eoda.de/2017/02/02/tutorial-how-to-set-up-a-twitter-bot-using-r/"> eoda english R news</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>On occasion of the 10,000<sup>th</sup> R package on CRAN, <a target="_blank" rel="nofollow" href="http://www.eoda.de/en/news.html">eoda</a> operated an automated Twitter account which regularly posted the current number of packages available on CRAN until the 10,000-package milestone has been passed on January 28, 2017.</p>
<blockquote data-lang="de" class="twitter-tweet">
<p dir="ltr" lang="en"><a target="_blank" rel="nofollow" href="https://twitter.com/hashtag/Rstatsgoes10k?src=hash">#Rstatsgoes10k</a> – Hello World, it’s 2017-01-28 01:59:03 and currently there are 10000 packages on CRAN.</p>
<p>— eodacelebratesR (@Rstatsgoes10k) <a target="_blank" rel="nofollow" href="https://twitter.com/Rstatsgoes10k/status/825146132691111938">28. Januar 2017</a></p>
</blockquote>
<p></p>
<p>To operate this account, we wrote an R script for reading the current number of R packages on CRAN every hour and automatically sending a Tweet with the status quo. All we needed to make this work was the Twitter API as well as the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/twitteR/index.html">twitteR package</a>.</p>
<p><span></span></p>
<p>For using the Twitter API, it is necessary to create a corresponding app for the Twitter account.</p>
<p>For this, log in with the respective account on <a target="_blank" rel="nofollow" href="https://apps.twitter.com/">apps.twitter.com</a> and fill out the form under Create New App.</p>
<figure class="wp-caption"><img sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" alt="Einrichten einer Twitter App auf apps.twitter.com" class="wp-image-4005" src="https://i0.wp.com/blog.eoda.de/wp-content/uploads/2017/02/Create-an-application.png?resize=456%2C398&ssl=1" data-recalc-dims="1"><br>
<figcaption class="wp-caption-text">Creating a Twitter app on apps.twitter.com</figcaption>
</figure>
<p>In the next window, you can set among other things the permissions of the app, “read only”, “read and write” or “read, write and access direct messages”. To operate the app, you also need the Consumer Key and Consumer Secret as well as the Access Token and Access Token Secret you can find in the tab Key and Access Tokens.</p>
<figure class="wp-caption"><img sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" alt="Application Settings" class="wp-image-4008" src="https://i0.wp.com/blog.eoda.de/wp-content/uploads/2017/02/Appclication-Settings.png?resize=456%2C254&ssl=1" data-recalc-dims="1"><br>
<figcaption class="wp-caption-text">Application Settings</figcaption>
</figure>
<figure class="wp-caption"><img sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" alt="Access Token" class="wp-image-4020" src="https://i1.wp.com/blog.eoda.de/wp-content/uploads/2017/02/Access-Token-1024x511.png?resize=456%2C227&ssl=1" data-recalc-dims="1"><br>
<figcaption class="wp-caption-text">Access Token</figcaption>
</figure>
<p>We transferred this information into an extra file in order to be able to load them into the R script later on. Now the app is ready for use.</p>
<p>For sending Tweets out of R, you need the functions setup_twitter_oauth and tweet from the twitter package. The function setup_twitter_oauth manages the authentication and tweet finally sends the appropriate Tweet.</p>
<p>The following R script demonstrates how we applied this to our Rstatsgoes10k account:</p>
<pre class="wp-code-highlight">
# Packages ----------------------------------------------------------------

library(rvest)
library(stringr)
library(dplyr)
library(twitteR)


# get number of packages --------------------------------------------------

url &lt; - &quot;https://cran.r-project.org/web/packages/&quot;


page &lt;- read_html(url)

n_packages &lt;- page %>%
  html_text() %>% 
  str_extract(&quot;[[:digit:]]* available packages&quot;) %>% 
  str_extract(&quot;[[:digit:]]*&quot;) %>% 
  as.numeric()

n_packages_last_time &lt; - read.table(file = &quot;n_packages.csv&quot;,stringsAsFactors = F, sep = &quot;;&quot;)

n_packages_last_time &lt;- n_packages_last_time$V2[nrow(n_packages_last_time)]

## check if news packages are published, new tweet only when number of packages changed
if(n_packages > n_packages_last_time) {
  
  # Twitter -----------------------------------------------------------------
  
  # set up twitter api
  api_keys &lt; - read.csv2(&quot;twitter_access.csv&quot;, stringsAsFactors = FALSE)
  
  setup_twitter_oauth(consumer_key = api_keys$consumer_key,
                      consumer_secret = api_keys$consumer_secret,
                      access_token = api_keys$access_token,
                      access_secret = api_keys$access_secret)
  
  
  
  time &lt;- Sys.time()
  
  # create tweet
  tweet_text &lt;- paste0(&quot;#Rstatsgoes10k - Hello World, it's &quot;, time, &quot; and currently there are &quot;, n_packages, &quot; packages on CRAN. &quot;)
  
  # send tweet
  tweet(tweet_text)
  
  
  # write n to file ---------------------------------------------------------
  
  n_packages_df &lt;- data.frame(time = Sys.time(), n = n_packages)
  
  write.table(n_packages_df, file = &quot;n_packages.csv&quot;, row.names = FALSE,col.names = FALSE,
              append = TRUE, sep = &quot;;&quot;)
  
}

</pre>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.eoda.de/2017/02/02/tutorial-how-to-set-up-a-twitter-bot-using-r/"> eoda english R news</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/KDC6pVDguC8"></a></div>