在呈现调用中从参数中更改rmarkdown pdf的名称和标题

时间:2022-12-04 23:39:29

Here is some R markdown code in a "test_param.Rmd" file:

下面是“test_param”中的一些R标记代码。限制型心肌病”文件:

---
output: pdf_document
params:
  number: "1"
title: `r params$number`
---

```{r setup, include=TRUE}

knitr::opts_chunk$set(echo = TRUE)

```

```{r one, include = TRUE}

i = 2
#data = data.frame(x = c(1,2,3))

#for(i in 1:dim(data)[1]){

 # plot(i*1000)

#}

```

`r params$number`

that is called using a .r file here:

这被称为使用。r文件:

library(rmarkdown)

rmarkdown::render("C://Users//me//Desktop//test_param.Rmd", 
                  params = list(number= "1"))

I get this error when I run the .r

我在运行。r时得到这个错误

Error in yaml::yaml.load(enc2utf8(string), ...) : 
  Scanner error: while scanning for the next token at line 4, column 8found character that cannot start any token at line 4, column 8

Any idea why?

知道为什么吗?

I am using `` not '' to do the title: r params$number

我使用' not '来做标题:r params$number

2 个解决方案

#1


2  

Question 1 is nicely answered here, you can use params to inject a new title into the YAML config:

问题1在这里得到了很好的回答,您可以使用params将一个新标题注入到YAML配置中:

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

Then the output filenames for rendered files can be set by the output_file parameter of rmarkdown::render.

然后,可以通过rmarkdown::呈现的output_file参数设置呈现文件的输出文件名。

#2


1  

For anyone who comes across this, instead of trying to call paste you can use something like"``r params$MyParam` test paste`". such as:

对于任何遇到这种情况的人,您可以使用“' r params$MyParam ' test paste '之类的东西,而不是尝试调用paste。如:

--- output: html_document params: new_title: "My Title!" title: "``r params$new_title` test paste`" ---

---输出:html_document params: new_title:“My Title!

This worked using RStudio 1.0.143 with R 3.4.0

这在使用RStudio 1.0.143和r3.4.0时有效

#1


2  

Question 1 is nicely answered here, you can use params to inject a new title into the YAML config:

问题1在这里得到了很好的回答,您可以使用params将一个新标题注入到YAML配置中:

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

Then the output filenames for rendered files can be set by the output_file parameter of rmarkdown::render.

然后,可以通过rmarkdown::呈现的output_file参数设置呈现文件的输出文件名。

#2


1  

For anyone who comes across this, instead of trying to call paste you can use something like"``r params$MyParam` test paste`". such as:

对于任何遇到这种情况的人,您可以使用“' r params$MyParam ' test paste '之类的东西,而不是尝试调用paste。如:

--- output: html_document params: new_title: "My Title!" title: "``r params$new_title` test paste`" ---

---输出:html_document params: new_title:“My Title!

This worked using RStudio 1.0.143 with R 3.4.0

这在使用RStudio 1.0.143和r3.4.0时有效