r - Rmarkdown YAML 中的引号和内联 R 代码

标签 r r-markdown

我想在 R markdown 文档中添加一些特殊字体作为标题/副标题的一部分,但根据我使用的引用类型,我得到了不同的结果。

这是一个显示我的问题的小 R markdown 示例。我想在推特句柄之前打印推特 Logo 作为 R markdown 副标题的一部分。

---
title: "Untitled"
subtitle: '`r fontawesome::fa("twitter", fill = "steelblue")` @twitter handle'
author: "My Name"
date: "7/12/2018"
output: html_document
---

This is it.

如果我使用单引号,那么我几乎可以得到我想要的结果。 Logo 和推特句柄之间有一个换行符,如下所示,但这很好 - 它可以用 css 修复,因为 Logo 设置在 <svg> ... </svg> 中。 .

但是,如果我使用双引号,那么事情就会变得一团糟:

subtitle: "`r fontawesome::fa('twitter', fill = 'steelblue')` @twitter handle"

现在 没有 YAML 信息出现在最终的 html 文档中。我猜它与 this question on entering the current date as part of the yaml 有关或者可能 this post where the YAML inline code doesn't run但我很好奇为什么 YAML 中引号类型的变化对渲染有如此深远的影响,因为双引号显然适用于其他 YAML 参数。

我正在使用 rmarkdown v1.10.

enter image description here

最佳答案

fontawesome::fa() 函数返回一个 SVG 元素。
例如,fontawesome::fa("twitter", fill = "steelblue") 返回的字符串是:

<svg style="height:0.8em;top:.04em;position:relative;fill:steelblue;" viewBox="0 0 512 512">
  <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"/>
</svg>

这个字符串包含双引号:这就是为什么单引号可以得到正确的结果而双引号不能很好地执行的原因。 如果您使用 keep_md 选项检查中间 markdown 文件,则很明显:

---
title: "Untitled"
subtitle: "`r fontawesome::fa('twitter', fill = 'steelblue')` @twitter handle"
author: "My Name"
date: "7/12/2018"
output: 
  html_document:
    keep_md: true
---

可以使用 YAML literalfolded block 消除这些引用注意事项:

subtitle: |
    `r fontawesome::fa('twitter', fill = 'steelblue')` @twitter handle

subtitle: >
    `r fontawesome::fa('twitter', fill = 'steelblue')` @twitter handle

关于r - Rmarkdown YAML 中的引号和内联 R 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51296364/

相关文章:

正则表达式从 R 中的字符串中删除所有非数字符号

在 r 中水平旋转直方图

r - 如何在 R 中创建 for 循环来进行这种特殊的计算

在 Rmarkdown 笔记本中渲染 SQL,而不仅仅是在 knit 时

r - 在 knitr/markdown 中更改代码块颜色

r - 使用r包 'flextable'将多个表格导出到word文档

R 包 mlr 耗尽多核内存

r - 如何在 R 代码块中创建 R-markdown 部分?具有正确的代码显示

r - knit HTML 不会将 html 保存在vignettes/中

r-markdown - 可以在另一个 ioslide 中嵌套/包含 ioslide 吗?