[英]Using YAML set parameters IN YAML header Knitr with parameters
我在R Markdown的YAML标头中声明参数Report_Date
。 我想将Report_Date
参数用作标头中的强制标头,包括它后面的行。 无论我使用哪个字符,我似乎都无法引用该参数。
我尝试了所有我能想到的变体,并遵循了所有其他找到的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 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
参数的标头Report_Date
。
错误消息包括:
! Missing $ inserted.
<inserted text>
You may need to add $ $ around a certain inline R expression `r `
对我来说唯一有意义的语法是
---
output: pdf_document
params:
Report_Date: "YYYY-MM-DD"
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[LO,LE]{`r params$Report_Date`}
---
如果使用该命令,则会收到有关params
未知的其他错误消息。 这确实是有道理的,因为仅在解析YAML标头之后才定义params
。 幸运的是,您也可以在文档正文中使用\\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标头中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.