首页 > 解决方案 > 如何将缺少的 YAML 标头信息添加到脚本中的 .rmd R Markdown 文件?

问题描述

我正在开发一个工作流程,我

  1. 协作处理 Google Doc,然后
  2. 使用 googledrive 包下载为 .docx,
  3. 使用 rmarkdown:pandoc_convert() 转换为 .rmd,
  4. 样式和标志应用和
  5. 呈现为 .html 和 PDF 以供分发。

当 .rmd 文件没有标题时,我目前正在第 3 步挂断。

 pandoc_convert("example.docx", "markdown", output = "out.Rmd")

如何从工作流脚本中的另一个文件注入 YAML?例如这个标题:

---
  title: "Title1"    
  html_document:
  number_sections: yes
  self_contained: yes
  toc: yes
  toc_depth: 3
  toc_float: yes
---

标签: rr-markdownpandoc

解决方案


假设您的标题位于header.yaml. 然后只需读取这两个文件并将它们写成一个:

fulltext <- c(readLines("header.yaml"), readLines("out.Rmd"))
writeLines(fullText, "out2.Rmd")

当然,您也可以将标头放入字符串变量中,而不是从文件中读取它,例如

header <- '---
  title: "Title1"    
  html_document:
  number_sections: yes
  self_contained: yes
  toc: yes
  toc_depth: 3
  toc_float: yes
---'
fulltext <- c(header, readLines("out.Rmd"))
writeLines(fullText, "out2.Rmd")

推荐阅读