<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/faces-of-rstats-twitter/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://maelle.github.io/2017/03/19/facesofr/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This week I was impressed by <a target="_blank" rel="nofollow" href="https://twitter.com/DEJPett/status/842330248549158913">this tweet</a> where Daniel Pett, Digital Humanities Lead at the British Museum, presented a collage of Twitter profile pics of all his colleagues. He made this piece of art using R (for collecting the usernames) and Python. I’m a bit of an R fanatic (or a Python dummy…) so I decided to write a code in R only to make a collage of profile pics of Twitter #rstats users.</p>
<p><span></span></p>
<p>Note: if you’re interested in Daniel Pett’s code, the data gathering is <a target="_blank" rel="nofollow" href="https://github.com/BritishMuseumDH/BMTwitter">here</a> and the montage code is <a target="_blank" rel="nofollow" href="https://github.com/BritishMuseumDH/scripts/blob/master/imageDownloadFromCSV.py">here</a>.</p>
<h1>Getting the users</h1>
<p>Once again I’ll use <code>rtweet</code>! In case you’ve been using <code>twitteR</code>, note that <code>twitteR</code> maintainer is currently passing the torch to <code>rtweet</code> so you’d better switch packages.</p>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;rtweet&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;dplyr&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;magick&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;httr&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;stringr&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;purrr&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code><span>users</span><span> </span><span>&lt;-</span><span> </span><span>search_users</span><span>(</span><span>q</span><span>=</span><span> </span><span>'#rstats'</span><span>,</span><span>
</span><span>n</span><span> </span><span>=</span><span> </span><span>1000</span><span>,</span><span>
</span><span>parse</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>users</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>users</span><span>)</span><span>
</span></code></pre>
</div>
<p>I <em>think</em> that doing this I got only profiles whose description includes “rstats” or “#rstats”. Moreover, although I got 1,000 results, many of them were duplicates, so in the end I had about 450 #rstats Twitter users.</p>
<h1>Gathering profile pics</h1>
<p>This is the part where I felt slightly creepy downloading pictures of so many people.</p>
<p>I first had to create the link to each person’s profile page.</p>
<div>
<pre><code><span>users</span><span> </span><span>&lt;-</span><span> </span><span>mutate</span><span>(</span><span>users</span><span>,</span><span>
</span><span>where</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>&quot;https://twitter.com/&quot;</span><span>,</span><span>
</span><span>screen_name</span><span>))</span><span>
</span></code></pre>
</div>
<p>Then I wrote a function for getting the profile pic for each user. I worked for everyone except 1) eggs and 2) a person whose profile pic had no extension, so I was quite satisfied with the result.</p>
<p>Update: Daniel Pett told me I could have used the column “profile_image_url” of the data.frame obtained from <code>rtweet::search_users</code>… I had completely missed that column! Thanks a lot!</p>
<div>
<pre><code><span>get_piclink</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>df</span><span>){</span><span>
</span><span>content</span><span> </span><span>&lt;-</span><span> </span><span>httr</span><span>::</span><span>GET</span><span>(</span><span>df</span><span>$</span><span>where</span><span>)</span><span>
</span><span>content</span><span> </span><span>&lt;-</span><span> </span><span>httr</span><span>::</span><span>content</span><span>(</span><span>content</span><span>,</span><span> </span><span>as</span><span> </span><span>=</span><span> </span><span>&quot;text&quot;</span><span>)</span><span>
</span><span>image</span><span> </span><span>&lt;-</span><span> </span><span>str_extract</span><span>(</span><span>content</span><span>,</span><span>
</span><span>&quot;class=\&quot;ProfileAvatar-image \&quot; src=\&quot;https://pbs.twimg.com/profile_images/.*\\..*\&quot; alt&quot;</span><span>)</span><span>
</span><span>image</span><span> </span><span>&lt;-</span><span> </span><span>str_replace</span><span>(</span><span>image</span><span>,</span><span> </span><span>&quot;class=\&quot;ProfileAvatar-image \&quot; src=\&quot;&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>image</span><span> </span><span>&lt;-</span><span> </span><span>str_replace</span><span>(</span><span>image</span><span>,</span><span> </span><span>&quot;..alt&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>)</span><span>
</span><span>return</span><span>(</span><span>image</span><span>)</span><span>
</span><span>}</span><span>
</span><span>users</span><span> </span><span>&lt;-</span><span> </span><span>by_row</span><span>(</span><span>users</span><span>,</span><span> </span><span>get_piclink</span><span>,</span><span>
</span><span>.to</span><span> </span><span>=</span><span> </span><span>&quot;piclink&quot;</span><span>,</span><span> </span><span>.collate</span><span> </span><span>=</span><span> </span><span>&quot;cols&quot;</span><span>)</span><span>
</span><span>readr</span><span>::</span><span>write_csv</span><span>(</span><span>users</span><span>,</span><span> </span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;users.csv&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>After getting the links I could start saving the pictures, in a small 50px x 50px format. At that point I started using the <a target="_blank" rel="nofollow" href="https://github.com/ropensci/magick"><code>magick</code> package</a>, a real game changer for image manipulation in R!</p>
<div>
<pre><code><span>save_image</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>df</span><span>){</span><span>
</span><span>image</span><span> </span><span>&lt;-</span><span> </span><span>try</span><span>(</span><span>image_read</span><span>(</span><span>df</span><span>$</span><span>piclink</span><span>),</span><span> </span><span>silent</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>if</span><span>(</span><span>class</span><span>(</span><span>image</span><span>)[</span><span>1</span><span>]</span><span> </span><span>!=</span><span> </span><span>&quot;try-error&quot;</span><span>){</span><span>
</span><span>image</span><span> </span><span>%>%</span><span>
</span><span>image_scale</span><span>(</span><span>&quot;50x50&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_write</span><span>(</span><span>paste0</span><span>(</span><span>&quot;data/2017-03-19-facesofr_users_images/&quot;</span><span>,</span><span> </span><span>df</span><span>$</span><span>screen_name</span><span>,</span><span>&quot;.jpg&quot;</span><span>))</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>users</span><span> </span><span>&lt;-</span><span> </span><span>filter</span><span>(</span><span>users</span><span>,</span><span> </span><span>!</span><span>is.na</span><span>(</span><span>piclink</span><span>))</span><span>
</span><span>users</span><span> </span><span>&lt;-</span><span> </span><span>split</span><span>(</span><span>users</span><span>,</span><span> </span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>users</span><span>))</span><span>
</span><span>walk</span><span>(</span><span>users</span><span>,</span><span> </span><span>save_image</span><span>)</span><span>
</span></code></pre>
</div>
<p>I got 448 images.</p>
<h1>Preparing the collage itself</h1>
<p>I want to take a minute thinking about myself in elementary school. I am left-handed and I had scissors for right-handed people that I used with my left hand because no one in my family or school knew any better. I was therefore very bad at collages because of the cutting part. Had I known that I’d do collages with a computer as a grown-up, I would have been less sad to have to skip the break to keep on slowly cutting pieces of paper! (Note: I actually had a very happy childhood.)</p>
<p>Back to R talk now! For doing the collage I had to know how many columns and rows I wanted. For this I factorized the number of pics I had. Note that I also re-sampled the images, because I didn’t want all the R-Ladies chapters and their beautiful purple logo to end up near each other in the collage.</p>
<div>
<pre><code><span>files</span><span> </span><span>&lt;-</span><span> </span><span>dir</span><span>(</span><span>&quot;data/2017-03-19-facesofr_users_images/&quot;</span><span>,</span><span> </span><span>full.names</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>1</span><span>)</span><span>
</span><span>files</span><span> </span><span>&lt;-</span><span> </span><span>sample</span><span>(</span><span>files</span><span>,</span><span> </span><span>length</span><span>(</span><span>files</span><span>))</span><span>
</span><span>gmp</span><span>::</span><span>factorize</span><span>(</span><span>length</span><span>(</span><span>files</span><span>))</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## Big Integer ('bigz') object of length 7:
## [1] 2 2 2 2 2 2 7
</code></pre>
</div>
<p>Based on this I chose this format:</p>
<div>
<pre><code><span>no_rows</span><span> </span><span>&lt;-</span><span> </span><span>28</span><span>
</span><span>no_cols</span><span> </span><span>&lt;-</span><span> </span><span>16</span><span>
</span></code></pre>
</div>
<p>In <code>magick</code> there’s a function for appending images, <code>image_append</code>, from which you get either a row or a column depending on the <code>stack</code> argument. I first created all columns.</p>
<div>
<pre><code><span>make_column</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>i</span><span>,</span><span> </span><span>files</span><span>,</span><span> </span><span>no_rows</span><span>){</span><span>
</span><span>image_read</span><span>(</span><span>files</span><span>[(</span><span>i</span><span>*</span><span>no_rows</span><span>+1</span><span>)</span><span>:</span><span>((</span><span>i</span><span>+1</span><span>)</span><span>*</span><span>no_rows</span><span>)])</span><span> </span><span>%>%</span><span>
</span><span>image_append</span><span>(</span><span>stack</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_write</span><span>(</span><span>paste0</span><span>(</span><span>&quot;cols/&quot;</span><span>,</span><span> </span><span>i</span><span>,</span><span> </span><span>&quot;.jpg&quot;</span><span>))</span><span>
</span><span>}</span><span>
</span><span>walk</span><span>(</span><span>0</span><span>:</span><span>(</span><span>no_cols</span><span>-1</span><span>),</span><span> </span><span>make_column</span><span>,</span><span> </span><span>files</span><span> </span><span>=</span><span> </span><span>files</span><span>,</span><span>
</span><span>no_rows</span><span> </span><span>=</span><span> </span><span>no_rows</span><span>)</span><span>
</span></code></pre>
</div>
<p>And finally, I appended the columns!</p>
<div>
<pre><code><span>image_read</span><span>(</span><span>dir</span><span>(</span><span>&quot;cols/&quot;</span><span>,</span><span> </span><span>full.names</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>image_append</span><span>(</span><span>stack</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>image_write</span><span>(</span><span>&quot;2017-03-19-facesofr.jpg&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<h1>The end</h1>
<p>Here is the result, where many, many R users are most certainly missing. Besides, can we talk about these accounts with the old R logo? They make me feel old, while the R-Ladies logo make me happy. Too bad <a target="_blank" rel="nofollow" href="https://twitter.com/R_Forwards">Forwards</a> wasn’t included, because their logo is really pretty.</p>
<p><img alt="" src="https://i2.wp.com/raw.githubusercontent.com/maelle/maelle.github.io/master/_source/2017-03-19-facesofr.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Now you can try doing the same with your followers, the people you follow, or making a doormat with base plot advocates.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://maelle.github.io/2017/03/19/facesofr/"> Maëlle</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/BUwRvT3jFRs"></a></div>