【问题标题】:Using YAML set parameters IN YAML header Knitr with parameters使用 YAML 设置参数 IN YAML 标头 Knitr 和参数
【发布时间】:2019-12-03 22:05:44
【问题描述】:

我在 R Markdown 的 YAML 标头中声明了我的参数 Report_Date。我想使用 Report_Date 参数作为 header-includes 后面的行中的强制标头。无论我使用哪个字符,我似乎都无法引用该参数。

我尝试了我能想到的任何变体,并遵循了我发现的所有其他 SO。即使有些人的问题几乎相同,但答案对我不起作用,或者我做错了其他事情。

- \fancyhead[LO,LE]{"'r params$Report_Date'"}
- \fancyhead[LO,LE]{r params$Report_Date}
- \fancyhead[LO,LE]{\"'r params$Report_Date'"}
- \fancyhead[LO,LE]{\r params$Report_Date'}
- \fancyhead[LO,LE]{'`r params$Report_Date`'}
- \fancyhead[LO,LE]{'r params$Report_Date'}
- \fancyhead[LO,LE]{$"'r params$Report_Date'"$}
- \fancyhead[LO,LE]{$"'r params$Report_Date'"}
- \fancyhead[LO,LE]{\$r params$Report_Date'"$}
- \fancyhead[LO,LE]{$"'r params$Report_Date'"$}
- \fancyhead[LO,LE]{$'r params$Report_Date'$}
- \fancyhead[LO,LE]{"r params$Report_Date"}

甚至尝试过:

includes:

in_header:'`r params$Report_Date`'

如所述:YAML current date in rmarkdown

这是我当前的 YAML R Markdown 代码(它不在单独的模板文件中,只是在我想要创建的常规 .rmd 文件中)

 ---
    title: "Monthly Diagnostic Report"
    author: "Morgan :)"
    date: "July 12, 2019"
    toc: true
    params:
      Report_Date: "YYYY-MM-DD"
    header-includes:
    - \usepackage{fancyhdr}
    - \pagestyle{fancy}
    - \fancyhead[CO,CE]{Monthly Diagnostic Report}
    - \fancyhead[LO,LE]{"'r params$Report_Date'"}
    - \fancyfoot[RE,RO]{\thepage}
    output: pdf_document
    ---

我最好有一个评估 paste0("Report Created for: ", params$Report_Date) 的左页眉和一个评估 paste0("Report Created on: ", format(Sys.time(), "%d %B, %Y")) 的页脚。

但现在我只接受一个包含Report_Date 参数的标题。

错误信息包括:

! Missing $ inserted.
<inserted text>
You may need to add $ $ around a certain inline R expression `r `

【问题讨论】:

  • 你试过`r params$Report_Date`吗?
  • 是的,我有,它有同样的错误

标签: r latex yaml r-markdown knitr


【解决方案1】:

唯一对我有意义的语法是

---
output: pdf_document
params:
      Report_Date: "YYYY-MM-DD"
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[LO,LE]{`r params$Report_Date`}
---

如果我使用它,我会收到关于 params 未知的不同错误消息。这确实是有道理的,因为 params 仅在 YAML 标头被解析后定义。幸运的是,您也可以在文档正文中使用 \fancyhead\fancyfoot(在 LaTeX 中使用 \begin{document} 之后)。因此,以下内容应为您提供所需的输出:

---
output: pdf_document
params:
      Report_Date: "YYYY-MM-DD"
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{Monthly Diagnostic Report}
- \fancyfoot[RE,RO]{\thepage}
---

\fancyhead[LO,LE]{Report created for: `r params$Report_Date`}
\fancyfoot[CO,CE]{Report created on: `r format(Sys.time(), '%d %B, %Y')`}

<actual content>

注意:我也在正文中设置创建日期,因为很难将文字冒号放入 YAML 标头。

【讨论】:

  • 如何格式化?我将底部 2 行添加到我的代码的最后一行(不是在一个块中),它甚至没有在这两个额外的标题上显示任何内容,所以我将它添加到一个块中,它不喜欢它以 a 开头/ Quitting from lines 28-31 (Monthly_Diagnostic_Reporting_Code.Rmd) Error in parse(text = x, srcfile = src) : &lt;text&gt;:2:1: unexpected input 1: # ALL COMMENTS ARE IN THE 6TH CHUNK, CHUNKS AFTER ARE ALL IDENTICAL 2: \ ^ 所以我添加了一个 - 现在我得到一个解析代码错误...
  • @morg 这两行应该出现在实际内容之前。
  • 是的,这行得通!非常感谢!!我试了好几天,没有其他论坛有这么简单的答案:)
猜你喜欢
  • 2016-08-07
  • 2015-11-14
  • 2022-11-10
  • 2021-10-04
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多