My #Best9of2017 tweets

[This article was first published on Maëlle, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

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

</span><span class="n">best9</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">my_tweets</span><span class="o">$</span><span class="n">tweet_permalink</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">9</span><span class="p">]</span><span class="w">
</span>

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.

<span class="n">library</span><span class="p">(</span><span class="s2">"magrittr"</span><span class="p">)</span><span class="w">

</span><span class="n">save_one_file</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">url</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="p">){</span><span class="w">
  </span><span class="n">filename</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">paste0</span><span class="p">(</span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="s2">".png"</span><span class="p">)</span><span class="w">
  </span><span class="c1"># save and output filename</span><span class="w">
  </span><span class="n">webshot</span><span class="o">::</span><span class="n">webshot</span><span class="p">(</span><span class="n">url</span><span class="p">,</span><span class="w"> </span><span class="n">filename</span><span class="p">,</span><span class="w">
                  </span><span class="n">cliprect</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="w"> </span><span class="m">150</span><span class="p">,</span><span class="w"> </span><span class="m">750</span><span class="p">,</span><span class="w"> </span><span class="m">750</span><span class="p">))</span><span class="w">
  </span><span class="n">filename</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">files</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">purrr</span><span class="o">::</span><span class="n">map2_chr</span><span class="p">(</span><span class="n">best9</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="o">:</span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="n">save_one_file</span><span class="p">)</span><span class="w">
</span>

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.

<span class="n">no_rows</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">3</span><span class="w">
</span><span class="n">no_cols</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="m">3</span><span class="w">

</span><span class="n">make_column</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">files</span><span class="p">,</span><span class="w"> </span><span class="n">no_rows</span><span class="p">){</span><span class="w">
  </span><span class="n">filename</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">paste0</span><span class="p">(</span><span class="s2">"col"</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="s2">".jpg"</span><span class="p">)</span><span class="w">

  </span><span class="n">magick</span><span class="o">::</span><span class="n">image_read</span><span class="p">(</span><span class="n">files</span><span class="p">[(</span><span class="n">i</span><span class="o">*</span><span class="n">no_rows</span><span class="m">+1</span><span class="p">)</span><span class="o">:</span><span class="p">((</span><span class="n">i</span><span class="m">+1</span><span class="p">)</span><span class="o">*</span><span class="n">no_rows</span><span class="p">)])</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">magick</span><span class="o">::</span><span class="n">image_border</span><span class="p">(</span><span class="s2">"salmon"</span><span class="p">,</span><span class="w"> </span><span class="s2">"20x20"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">magick</span><span class="o">::</span><span class="n">image_append</span><span class="p">(</span><span class="n">stack</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">TRUE</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
    </span><span class="n">magick</span><span class="o">::</span><span class="n">image_write</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span><span class="w">
  
  </span><span class="n">filename</span><span class="w">
</span><span class="p">}</span><span class="w">

</span><span class="n">purrr</span><span class="o">::</span><span class="n">map_chr</span><span class="p">(</span><span class="m">0</span><span class="o">:</span><span class="p">(</span><span class="n">no_cols</span><span class="m">-1</span><span class="p">),</span><span class="w"> </span><span class="n">make_column</span><span class="p">,</span><span class="w"> </span><span class="n">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">files</span><span class="p">,</span><span class="w">
    </span><span class="n">no_rows</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">no_rows</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">magick</span><span class="o">::</span><span class="n">image_read</span><span class="p">()</span><span class="w"> </span><span class="o">%>%</span><span class="w">
</span><span class="n">magick</span><span class="o">::</span><span class="n">image_append</span><span class="p">(</span><span class="n">stack</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">FALSE</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">magick</span><span class="o">::</span><span class="n">image_border</span><span class="p">(</span><span class="s2">"salmon"</span><span class="p">,</span><span class="w"> </span><span class="s2">"20x20"</span><span class="p">)</span><span class="w"> </span><span class="o">%>%</span><span class="w">
  </span><span class="n">magick</span><span class="o">::</span><span class="n">image_write</span><span class="p">(</span><span class="s2">"2017-12-30-best9of2017.jpg"</span><span class="p">)</span><span class="w">
</span>

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

<span class="c1"># clean </span><span class="w">
</span><span class="n">file.remove</span><span class="p">(</span><span class="n">files</span><span class="p">)</span><span class="w">
</span><span class="n">file.removes</span><span class="p">(</span><span class="n">paste0</span><span class="p">(</span><span class="s2">"col"</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="o">:</span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="s2">".jpg"</span><span class="p">))</span><span class="w">
</span>

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)