Find most retweeted tweets of the #rspatial bot

Bots that retweet #rspatial and #BioImageAnalysis tweets are working well. It is now time to add a little functionnality with the best retweeted tweet of the month, a function added to {tweetrbot} package.

Last summer, with Marion Louveaux, we built two Twitter bots that retweet #rspatial and #BioImageAnalysis tweets. That’s our way to collect news of our preferred communities. If you are interested, I presented in this blog post how to “Create [such] a twitter bot on a Raspberry Pi 3 using R” I especially created package {tweetrbot} so that my Raspberry Pi can take care of this retweet task. Today I added a small functionnality that will tweet a monthly summary.

Retrieve all tweets with #rspatial

Inpired by posts of Marion on “Analysis Twitter data with R”, I decided it was time to start using data stored by our bots. Indeed, {tweetrbot} functions store all tweets with their respective hashtag in a .rds file.
I added the current state of the #rspatial dataset inside the package.

file <- system.file("complete_tweets_rspatial.rds", package = "tweetrbot")

all_tweets <- read_rds(file)

all_tweets %>% 
  select(created_at, screen_name, text)
## # A tibble: 1,568 x 3
##    created_at          screen_name    text                                      
##    <dttm>              <chr>          <chr>                                     
##  1 2020-09-10 13:41:43 PennMUSA       "Have you seen the fire hydrant inspectio…
##  2 2020-09-10 13:40:32 StatnMap       "🌍This is not #rspatial, but can be of in…
##  3 2020-09-10 00:23:40 taraskaduk     "Hey #gischat,\nWhat's your world's bound…
##  4 2020-09-09 19:17:55 dpprdan        "#rspatial https://t.co/Y0uuMDmQ3W"       
##  5 2020-09-09 13:17:18 charliejhadley "How does {mapview} decide when to send i…
##  6 2020-09-09 10:24:15 MichaelDorman… "@tjukanov Nice! :-)\n\nlibrary(sf)\nx = …
##  7 2020-09-09 00:22:54 charles_gauvin "What do these buildings ecumene tell us …
##  8 2020-09-08 17:39:22 ambiogeo       "Live online course on Geographical Infor…
##  9 2020-09-08 07:56:26 maurosc3ner    "Bayesian Hierarchical Models+ICAR in #IN…
## 10 2020-09-07 15:32:40 ultrazool      "@geospacedman do you have any recommenda…
## # … with 1,558 more rows

You can retrieve the updated dataset for all tweets here and the dataset for the timeline (followers) there

Find the top retweeted tweet and other explorations

If you want to explore this dataset a little longer, feel free ! You can get inspiration on how to explore such a Twitter dataset with Marion’s blog posts: “Analysing Twitter data with R”, “Exploring user profiles and relationships” and “Exploring tweets content”.

In {tweetrbot}, I decided to show the daily number of contributors, the most retweeted tweets and the counts of the month. To do so, I use the top_tweets() function. Here, I set post_tweet = FALSE because I do not want my blog post to tweet this. The bot already did it.
Here are the media of the tweet:

# remotes::install_github("statnmap/tweetrbot")
library(tweetrbot)
output <- top_tweets(all_tweets = all_tweets, post_tweet = FALSE, top_number = 5)

output$number_contributors

output$number_tweets

output$top_retweet

And there is the text of the tweet:

output$text_tweet

Summary of August #rspatial: 1568 tweets, 420 different contributors.

Most retweeted: https://twitter.com/zevross/status/1224686455274070017 by @zevross.

Set the Raspi to execute the script monthly

As in “Create a twitter bot on a Raspberry Pi 3 using R”, we will use cron to tweet regularly.

Create the R script that will be run by CRON.

bash

mkdir ~/talk_rspatial
cd ~/talk_rspatial
vim rtweet_summary.R

Fill the file with R code. This reads data gathered by get_and_store() function and create the content of the tweet. R

library(tweetrbot)

# Most retweets
all_tweets <- readRDS("~/talk_rspatial/complete_tweets_rspatial.rds")
# Tweet this
top_tweets(all_tweets = all_tweets, post_tweet = TRUE, top_number = 5)

Allow the Raspi to create graphics

The Raspi is a server without graphical interface. You cannot directly create graphics in R on a distant server. The Raspi is not exception. If you try, you can get the following message:

Error in X11(paste("png::", filename, sep = ""), g$width, g$height, pointsize,  : 
  unable to start device PNG
Calls: png
In addition: Warning message:
In png("test.png", width = 900, height = 400) :
  unable to open connection to X11 display ''
Execution halted

There exists a workaround. As answered by Dirk Eddelbuettel on SO, you can run R with the virtual framebuffer X11 server. You will first need to install it as follows:

bash

sudo apt install xvfb xauth xfonts-base

And then use xvfb-run R --no-save to run R script that create graphics. Also, with {ggplot2}, you can face error like:

#> X11 font -adobe-helvetica-%s-%s---%d-------*, face 2 at size 11 could not be loaded

According to answers on askubuntu, you may need to install the following fonts:

bash

sudo apt-get install t1-xfree86-nonfree ttf-xfree86-nonfree ttf-xfree86-nonfree-syriac xfonts-75dpi xfonts-100dpi

Set the CRON

Configure CRON to run your script once a month and tweet the top of the previous month.

bash

sudo crontab -e
# if you want to run for a specific user
crontab -u yourusername -e 

In the crontab file that opens, you can add a command that looks like :

Minute Hour Day-of-Month Month Day-Of-Week Command

You can set the crontab to run the script the first day of each month at 10:00 (* is for all):

0 10 1 * * sudo xvfb-run $HOME/R/bin/Rscript ~/talk_rspatial/rtweet_summary.R

The tweet of the month

The #rspatial bot already tweeted the summary of last month during my tests.

A new Twitter bot in the family

When writing the first blog post “Create a twitter bot on a Raspberry Pi 3 using R”, I created @talk_rspatial that retweets #rspatial tweets and @talk_BioImg that retweets #BioImageAnalysis tweets.
This new blog post sees the birth of @talk_rstatsfr that retweets #RstatsFR tweets. I hope this will further strengthen the ties of this already active French-speaking R community. If you speak about R in French, use #rstatsFR to be retweeted by the bot. And follow the @talk_rstatsfr bot to keep updated about what happens in this R French-speaking community !

See you on Twitter ?



Citation:

For attribution, please cite this work as:
Rochette Sébastien. (2020, May. 16). "Find most retweeted tweets of the #rspatial bot". Retrieved from https://statnmap.com/2020-05-10-find-most-retweeted-tweets-of-the-rspatial-bot/.


BibTex citation:
@misc{Roche2020Findm,
    author = {Rochette Sébastien},
    title = {Find most retweeted tweets of the #rspatial bot},
    url = {https://statnmap.com/2020-05-10-find-most-retweeted-tweets-of-the-rspatial-bot/},
    year = {2020}
  }