Mark
Mark

Reputation: 4537

How to include arbitrary HTML in RMarkdown document?

I'm using RMarkdown to author a blog via blogdown. I would like to include, for example, embedded tweets. Like below:

<blockquote class="twitter-tweet" data-partner="tweetdeck">
  <p lang="en" dir="ltr">Section 1.5 uses the gutenbergr package to pull literary text. Unfortunately, the function doesn&#39;t get through our work firewall.</p>&mdash; Jeremy GH (@JGreenbrookHeld) 
  <a href="https://twitter.com/JGreenbrookHeld/status/912774476533719040?ref_src=twsrc%5Etfw">September 26, 2017</a>
</blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

I know how to include R code which will be evaluated, but I don't know how to just stick in a block of HTML code and have it passed along directly. Any tips?

As an example, I have a simple RMarkdown document

---
title: "Test"
author: "Mark Ewing"
date: "October 2, 2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

<blockquote class="twitter-tweet" data-partner="tweetdeck">
  <p lang="en" dir="ltr">Section 1.5 uses the gutenbergr package to pull literary text. Unfortunately, the function doesn&#39;t get through our work firewall.</p>&mdash; Jeremy GH (@JGreenbrookHeld) 
  <a href="https://twitter.com/JGreenbrookHeld/status/912774476533719040?ref_src=twsrc%5Etfw">September 26, 2017</a>
</blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

When I just plop HTML down in the middle of the document I get this error:

pandoc.exe: Could not fetch //platform.twitter.com/widgets.js
//platform.twitter.com/widgets.js: openBinaryFile: does not exist (No such file or directory)
Error: pandoc document conversion failed with error 67
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS test.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\u772700\Documents\R\win-library\3.4\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "C:\Users\u772700\AppData\Local\Temp\RtmpK4i4dH\rmarkdown-str30e47033f51.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"' had status 67 
Execution halted

If I wrap it first in the code marker ``` it blockquotes the html instead of rendering it.

Upvotes: 2

Views: 2148

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30184

Although one solution has been pointed in a comment above (add the protocol http:// to the URL of widget.js), I want to mention the blogdown documentation, where I specifically used embedding Tweets as an example. I don't recommend you to use the embed code provided by Twitter, but use a Hugo shortcode instead, which is much more lightweight (although it essentially generates the same thing under the hood).

Upvotes: 4

Related Questions