如何在 RMarkdown 文档中包含任意 HTML?

How to include arbitrary HTML in RMarkdown document?

我正在使用 RMarkdown 通过 blogdown 创作博客。例如,我想包括嵌入式推文。如下所示:

<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>

我知道如何包含将被评估的 R 代码,但我不知道如何只插入 HTML 代码块并直接传递它。有什么建议吗?

举个例子,我有一个简单的 RMarkdown 文档

---
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>

当我把 HTML 放在文档中间时,我得到了这个错误:

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.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

如果我先将它包装在代码标记```中,它会引用 html 而不是渲染它。

虽然上面的评论中指出了一个解决方案(将协议http://添加到widget.js的URL),但我想提一下the blogdown documentation,其中我专门以嵌入推文为例。我不建议你使用 Twitter 提供的嵌入代码,而是使用 Hugo 短代码,它更轻量级(尽管它本质上在幕后生成相同的东西)。