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

时间:2017-10-02 16:44:27

标签: r-markdown blogdown

我正在使用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\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

如果我首先将它包装在代码标记中```它会阻止html而不是渲染它。

1 个答案:

答案 0 :(得分:4)

虽然上面的评论中指出了一个解决方案(将协议http://添加到widget.js的URL),但我想提一下the blogdown documentation,其中我特意使用嵌入Tweets作为示例。我建议你不要使用Twitter提供的嵌入代码,而是使用Hugo短代码,它更轻巧(尽管它本质上会在引擎盖下生成相同的东西)。