My #Best9of2017 tweets

By Maëlle Salmon

(This article was first published on Maëlle, and kindly contributed to R-bloggers)

You’ve probably seen people posting their #Best9of2017, primarily on Instagram I’d say. I’m not an Instagram user, although I do have an account to spy on my younger sister and cousins, so I don’t even have 9 Instagram posts in total but I do love the collage people get to show off… So what about my best 9 tweets of 2017?

Get my 9 best tweets by number of likes

I first wanted to use rtweet::get_timeline but it only returned me tweets from July, even when using include_rts = FALSE, so I downloaded my analytics files from the Twitter website, one per trimester.

my_files  c("tweet_activity_metrics_ma_salmon_20170101_20170402_en.csv",
              "tweet_activity_metrics_ma_salmon_20170402_20170702_en.csv",
              "tweet_activity_metrics_ma_salmon_20170702_20171001_en.csv",
              "tweet_activity_metrics_ma_salmon_20171001_20171231_en.csv")
paths  paste0("data/", my_files)
# read them all at once
my_tweets  purrr::map_df(paths, readr::read_csv)
# just in case I got some data ranges wrong
my_tweets  unique(my_tweets)
# get the top 9!
my_tweets  dplyr::arrange(my_tweets, - likes)
my_tweets  janitor::clean_names(my_tweets)

best9  my_tweets$tweet_permalink[1:9]

My husband advised me to use something more elaborate than number of likes, which is a wise idea, but I was happy with that simple method.

Take screenshots and paste them

There’s a great R package to do screenshots from R, webshot. I was a bit annoyed at the “Follow” button appearing, but I did not want to have to write Javascript code to first login in the hope to make that thing disappear. I tried using a CSS selector instead of a rectangle, but I was less satisfied. An obvious problem here is that contrary to Instagram images, tweets have different heights depending on the text length and on the size of the optional attached media… It’s a bit sad but not too sad, my collage will still give a look at my Twitter 2017.

library("magrittr")

save_one_file  function(url, name){
  filename  paste0(name, ".png")
  # save and output filename
  webshot::webshot(url, filename,
                  cliprect = c(0, 150, 750, 750))
  filename
}

files  purrr::map2_chr(best9, 1:9, save_one_file)

Regarding the collage part using magick, I used my “Faces of R” post as a reference, which is funny since it features in my top 9 tweets.

no_rows  3
no_cols  3

make_column  function(i, files, no_rows){
  filename  paste0("col", i, ".jpg")

  magick::image_read(files[(i*no_rows+1):((i+1)*no_rows)]) %>%
  magick::image_border("salmon", "20x20") %>%
  magick::image_append(stack = TRUE) %>%
    magick::image_write(filename)
  
  filename
}

purrr::map_chr(0:(no_cols-1), make_column, files = files,
    no_rows = no_rows) %>%
  magick::image_read() %>%
magick::image_append(stack = FALSE) %>%
  magick::image_border("salmon", "20x20") %>%
  magick::image_write("2017-12-30-best9of2017.jpg")

And since I’m well behaved, I clean after myself.

# clean 
file.remove(files)
file.removes(paste0("col", 1:3, ".jpg"))

So, apart from fun blog posts (Where to live in the US, Faces of #rstats Twitter, A plot against the CatterPlots complot) and more serious ones (Automatic tools for improving R packages, How to develop good R packages (for open science), Where have you been? Getting my Github activity), my top 9 tweets include a good ggplot2 tip (this website) and above all, the birth of baby Émile! Why have I only included two heart emojis?

What about your 2017?

I’ve now read a few awesome posts from other R bloggers, from the top of my head:

What’s your take on your 2017, dear reader? In any case, I wish you a happy New Year!

To leave a comment for the author, please follow the link and comment on their blog: Maëlle.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more…

Source:: R News

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.