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: 957 x 3
##    created_at          screen_name text                                         
##    <dttm>              <chr>       <chr>                                        
##  1 2020-05-09 22:58:06 mdsumner    "#rstats #rspatial I georeferenced volcano, …
##  2 2020-05-05 00:04:52 mdsumner    "polylidar C++ lib for points (not just lida…
##  3 2020-05-09 05:25:25 mdsumner    "#rspatial #rstats @edzerpebesma how does on…
##  4 2020-05-09 08:11:15 cfcoverdale "Playing around with #Rspatial sets - took a…
##  5 2020-05-09 04:59:26 maurosc3ner "Relative risk for COVID-19 as an underlying…
##  6 2020-05-04 08:32:11 maurosc3ner "Relative risk of anemia among women in Sub-…
##  7 2020-05-08 21:29:21 FisherRick  "@geomenke @dmfenton @billdollins 🤣🤣🤣 so doe…
##  8 2020-05-08 16:58:04 BeckyC85    "I am \U0001f90f to giving up on R forever! …
##  9 2020-05-08 04:06:31 LuisDVerde  "🇦🇶High latitude maps in R. Example with sea…
## 10 2020-05-07 21:47:39 StatnMap    "Also you can ask your R map questions using…
## # … with 947 more rows

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 April #rspatial: 195 tweets, 84 different contributors.

Most retweeted #rspatial: https://twitter.com/jakub_nowosad/status/1251872204012302337 by @jakub_nowosad.

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}
  }
Comments
comments powered by Disqus