从参数设置Rmarkdown中的文档标题。

时间:2022-12-04 23:34:53

I've got an Rmarkdown template that works well, and I've parameterized it so I can generate variants of the same report from different data sources. However, I'd like to change the title of the report in each case. How do I do that?

我有一个运行良好的Rmarkdown模板,并对它进行了参数化,以便可以从不同的数据源生成相同报告的变体。但是,我想在每个情况下更改报告的标题。我该怎么做呢?

Here's the YAML header I have so far:

这是我到目前为止的YAML标题:

---
title: "My Title"
author: "Me, Inc."
date: "August 4, 2015"
output: pdf_document
params:
  title: default
---

I've tried using params=list(title="ASDF") in the call to rmarkdown::render, and although my code can see that variable, it doesn't change the title. I've also tried using r params$title in the YAML, but that gives a syntax error.

我在调用rmarkdown:::render时尝试使用params=list(title="ASDF"),虽然我的代码可以看到该变量,但它不会改变标题。我也尝试过在YAML中使用r params$title,但是这会导致语法错误。

Is there something else I should be trying? Thanks!

我还有什么需要尝试的吗?谢谢!

2 个解决方案

#1


48  

Try to use a second YAML metadata block, and put the parameterized metadata in there.

尝试使用第二个YAML元数据块,并将参数化元数据放在其中。

I got the following code to work as expected (i.e., producing a document title from the list of params):

我得到了下面的代码以按预期工作(即。,由文件类别列表产生文件标题):

---
output: html_document
params: 
    set_title: "My Title!"
---

---
title: `r params$set_title`
---

The RMarkdown documentation notes that YAML metadata blocks are combined by Pandoc. Use the first block to define the parameter set, and the second one to use the parameters as metadata. Knitr will execute the R code to interpret the parameters in the second block.Then Pandoc will merge the metadata blocks together.

RMarkdown文档指出YAML元数据块是由Pandoc组合的。使用第一个块定义参数集,第二个块使用参数作为元数据。Knitr将执行R代码来解释第二个块中的参数。然后Pandoc将合并元数据块。

Update (2017):

更新(2017):

This can be accomplished in a single block, like so:

这可以在单个块中完成,比如:

---
output: html_document
params: 
    set_title: "My Title!"
title: "`r params$set_title`"
---

This works because the title comes after the params definition. I put quotes around the in-line R code to prevent "Scanner errors".

这是可行的,因为标题在params定义之后。我在内联的R代码中加上引号以防止“扫描错误”。

#2


1  

Adding this answer as it helps in making R markdown titles dynamic.

添加这个答案,因为它有助于使R markdown标题充满活力。

Just use !r followed by the object name defined (test_title in the case below) to make the title dynamic.

只需使用!r后面跟着定义的对象名(下面的例子中是test_title),就可以使标题动态。

---
output: pdf_document
params:
set_title: !r test_title
---
---
title: `r params$set_title`
---

#1


48  

Try to use a second YAML metadata block, and put the parameterized metadata in there.

尝试使用第二个YAML元数据块,并将参数化元数据放在其中。

I got the following code to work as expected (i.e., producing a document title from the list of params):

我得到了下面的代码以按预期工作(即。,由文件类别列表产生文件标题):

---
output: html_document
params: 
    set_title: "My Title!"
---

---
title: `r params$set_title`
---

The RMarkdown documentation notes that YAML metadata blocks are combined by Pandoc. Use the first block to define the parameter set, and the second one to use the parameters as metadata. Knitr will execute the R code to interpret the parameters in the second block.Then Pandoc will merge the metadata blocks together.

RMarkdown文档指出YAML元数据块是由Pandoc组合的。使用第一个块定义参数集,第二个块使用参数作为元数据。Knitr将执行R代码来解释第二个块中的参数。然后Pandoc将合并元数据块。

Update (2017):

更新(2017):

This can be accomplished in a single block, like so:

这可以在单个块中完成,比如:

---
output: html_document
params: 
    set_title: "My Title!"
title: "`r params$set_title`"
---

This works because the title comes after the params definition. I put quotes around the in-line R code to prevent "Scanner errors".

这是可行的,因为标题在params定义之后。我在内联的R代码中加上引号以防止“扫描错误”。

#2


1  

Adding this answer as it helps in making R markdown titles dynamic.

添加这个答案,因为它有助于使R markdown标题充满活力。

Just use !r followed by the object name defined (test_title in the case below) to make the title dynamic.

只需使用!r后面跟着定义的对象名(下面的例子中是test_title),就可以使标题动态。

---
output: pdf_document
params:
set_title: !r test_title
---
---
title: `r params$set_title`
---