Skip to content

Embed Tweets in R Markdown

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

gadenbuie/tweetrmd

Repository files navigation

tweetrmd

Easily embed Tweets anywhere R Markdown turns plain text into HTML.

Installation

You can install the released version of tweetrmd from GitHub:

# install.packages("devtools")
devtools::install_github("gadenbuie/tweetrmd")

Embed a Tweet

library(tweetrmd)
tweet_embed("https://twitter.com/alexpghayes/status/1211748406730706944")

anybody have experience embedding tweets into #rmarkdown documents *without using blogdown*?https://t.co/5kQUBh7j4g

— alex hayes (@alexpghayes) December 30, 2019

Or if you would rather use the screen name and status id.

tweet_embed(tweet_url("alexpghayes", "1211748406730706944"))

anybody have experience embedding tweets into #rmarkdown documents *without using blogdown*?https://t.co/5kQUBh7j4g

— alex hayes (@alexpghayes) December 30, 2019

In rich HTML outputs, the full embedded tweet is available and interactive. Here, in GitHub-flavored markdown, only the content of the tweet is seen.

Embed many tweets

If you have several tweets you would like to embed at once, you can use the following pattern to include add a vector of tweets to your document. This works well when you want to include a thread of tweets.

thread <- c(
  "https://twitter.com/grrrck/status/1333804309272621060",
  "https://twitter.com/grrrck/status/1333804487148855300", 
  "https://twitter.com/grrrck/status/1333805092152123394"
)

htmltools::tagList(
  lapply(thread, tweet_embed, plain = TRUE)
)

I've got a new work laptop! I'm going to try to track my setup process and the software and tools I install in this thread pic.twitter.com/9X2qvHB3no

— Garrick Aden-Buie (@grrrck) December 1, 2020

Step #1, wait... pic.twitter.com/3533LZZQBt

— Garrick Aden-Buie (@grrrck) December 1, 2020

Oh wow, I really jumped the gun on this thread pic.twitter.com/XpbzLTzStf

— Garrick Aden-Buie (@grrrck) December 1, 2020

(Note that I used plain = TRUE to embed each tweet as markdown.)

Take a screenshot of a tweet

Screenshots are automatically embedded in R Markdown documents, or you can save the screenshot as a .png or .pdf file. Uses the rstudio/webshot2 package.

tweet_screenshot(tweet_url("alexpghayes", "1211748406730706944"))

Just include a tweet in any R Markdown output format

When you want to include a tweet in multiple R Markdown formats, you can use include_tweet(). It’s like knitr::include_graphics() but for tweets. The function will automatically include the tweet as HTML in HTML outputs, or as a screenshot in all others.

```{r tweet-from-dsquintana}
include_tweet("https://twitter.com/dsquintana/status/1275705042385940480")
```

{bookdown} folks: I'm trying to knit a PDF version of a HTML book that contains HTML elements (embedded tweets).

Is there a way to automatically take a screenshot of embedded tweets for PDF output?

Using the {webshot} package + PhantomJS didn't work...#Rstats

— Dan Quintana (@dsquintana) June 24, 2020

Customize tweet appearance

Twitter’s oembed API provides a number of options, all of which are made available for customization in tweet_embed() and tweet_screenshot().

tweet_screenshot(
  tweet_url("alexpghayes", "1211748406730706944"),
  maxwidth = 300,
  hide_media = TRUE,
  theme = "dark"
)

Embed without tracking

You can use tweetrmd to embed tweets in your documents and outputs without including Twitter JavaScript or tracking. The easiest way is to set plain = TRUE in include_tweet(). This will insert minimal HTML for web outputs or convert the tweet text to markdown for non-web outputs.

include_tweet(
  "https://twitter.com/dsquintana/status/1275705042385940480",
  plain = TRUE
)
```{=html}
<blockquote class="twitter-tweet" data-width="550" data-lang="en" data-dnt="true" data-theme="light"><p lang="en" dir="ltr">{bookdown} folks: I&#39;m trying to knit a PDF version of a HTML book that contains HTML elements (embedded tweets). <br><br>Is there a way to automatically take a screenshot of embedded tweets for PDF output? <br><br>Using the {webshot} package + PhantomJS didn&#39;t work...<a href="https://twitter.com/hashtag/Rstats?src=hash&amp;ref_src=twsrc%5Etfw">#Rstats</a></p>&mdash; Dan Quintana (@dsquintana) <a href="https://twitter.com/dsquintana/status/1275705042385940480?ref_src=twsrc%5Etfw">June 24, 2020</a></blockquote>

```

Alternatively, you can choose to use tweet_screenshot() or tweet_markdown() to embed all tweets in your documents.

Caching tweets with memoization

Tweets are often deleted and re-running tweet_embed() or tweet_screenshot() may fail or overwrite a previous screenshot of a tweet. To avoid this, you can use the memoise package.

library(memoise)

tweet_cached <- memoise(tweet_embed, cache = cache_filesystem('.tweets'))
tweet_shot_cached <- memoise(tweet_screenshot, cache = cache_filesystem('.tweets'))

*When memoising tweet_screenshot() you need to manually save the file to a specific location. In the future my goal is for this to be automatic.


Note: When using tweet_embed(), you may need to add the following line to your YAML header for strict markdown output formats.

always_allow_html: true

About

Embed Tweets in R Markdown

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published