<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/r%e2%81%b6-capture-tweets-with-tweet_shot/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/12/30/r%E2%81%B6-capture-tweets-with-tweet_shot/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<span>(You can find all R⁶ posts <a rel="nofollow" href="https://rud.is/b/tag/r6/" target="_blank">here</a>)</span>
</div>
<p>A Twitter discussion:</p>
<blockquote class="twitter-tweet" data-lang="en">
<p lang="en" dir="ltr">I'm going to keep my eyes out for this one! Would love to have an easy way to embed tweets in Rmd talks!</p>
<p>— Jeff Hollister (<a rel="nofollow" href="http://twitter.com/jhollist" target="_blank">@jhollist</a>) <a rel="nofollow" href="https://twitter.com/jhollist/status/947082036019388416?ref_src=twsrc%5Etfw" target="_blank">December 30, 2017</a></p></blockquote>
<p></p>
<p>that spawned from Maëlle’s <a rel="nofollow" href="http://www.masalmon.eu/2017/12/30/best9of2017/" target="_blank">recent look-back post</a> turned into a quick function for capturing an image of a Tweet/thread using <code>webshot</code>, <code>rtweet</code>, <code>magick</code> and <code>glue</code>.</p>
<p>Pass in a status id or a twitter URL and the function will grab an image of the mobile version of the tweet. </p>
<p>The ultimate goal is to make a function that builds a tweet using only R and <code>magick</code>. This will have to do until the new year.</p>
<pre><code>tweet_shot &lt;- function(statusid_or_url, zoom=3) {

  require(glue, quietly=TRUE)
  require(rtweet, quietly=TRUE)
  require(magick, quietly=TRUE)
  require(webshot, quietly=TRUE)

  x &lt;- statusid_or_url[1]

  is_url &lt;- grepl(&quot;^http[s]://&quot;, x)

  if (is_url) {

    is_twitter &lt;- grepl(&quot;twitter&quot;, x)
    stopifnot(is_twitter)

    is_status &lt;- grepl(&quot;status&quot;, x)
    stopifnot(is_status)

    already_mobile &lt;- grepl(&quot;://mobile\\.&quot;, x)
    if (!already_mobile) x &lt;- sub(&quot;://twi&quot;, &quot;://mobile.twi&quot;, x)

  } else {

    x &lt;- rtweet::lookup_tweets(x)
    stopifnot(nrow(x) > 0)
    x &lt;- glue_data(x, &quot;https://mobile.twitter.com/{screen_name}/status/{status_id}&quot;)

  }

  tf &lt;- tempfile(fileext = &quot;.png&quot;)
  on.exit(unlink(tf), add=TRUE)

  webshot(url=x, file=tf, zoom=zoom)

  img &lt;- image_read(tf)
  img &lt;- image_trim(img)

  if (zoom > 1) img &lt;- image_scale(img, scales::percent(1/zoom))

  img

}</code></pre>
<p>Now just do one of these:</p>
<pre><code>tweet_shot(&quot;947082036019388416&quot;)
tweet_shot(&quot;https://twitter.com/jhollist/status/947082036019388416&quot;)</code></pre>
<p>to get:</p>
<p><a rel="nofollow" href="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/12/preview.png?ssl=1" target="_blank"><img data-orig-file="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/12/preview.png?fit=517%2C899&amp;ssl=1" data-orig-size="517,899" data-medium-file="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/12/preview.png?fit=173%2C300&amp;ssl=1" data-attachment-id="7735" data-image-meta="{" data-image-description="" src="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/12/preview.png?resize=517%2C899&ssl=1" data-permalink="https://rud.is/b/2017/12/30/r%e2%81%b6-capture-tweets-with-tweet_shot/preview-3/" data-large-file="https://i0.wp.com/rud.is/b/wp-content/uploads/2017/12/preview.png?fit=517%2C899&amp;ssl=1" alt="" data-image-title="preview" width="450" data-comments-opened="1" class="wp-image-7735" data-recalc-dims="1"></a></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="https://rud.is/b/2017/12/30/r%E2%81%B6-capture-tweets-with-tweet_shot/"> R – rud.is</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/NKkjK93KT2A" height="1"></a></div>