在RMarkdown YAML字段中使用多个参数
假设我有在RMarkdown YAML字段中使用多个参数,r,r-markdown,R,R Markdown,假设我有df作为我的数据帧 df <- data.frame(title = c("Hello there", "Good morning", "Good afternoon", "Good evening"), date = c("4 Jan 2019", "6 Jan 2019", "10 Jan 2019", "14 Jan 2019")) 复制我的R脚本: library(stringr) library(tidyverse) title &l
df
作为我的数据帧
df <- data.frame(title = c("Hello there", "Good morning", "Good afternoon", "Good evening"),
date = c("4 Jan 2019", "6 Jan 2019", "10 Jan 2019", "14 Jan 2019"))
复制我的R脚本:
library(stringr)
library(tidyverse)
title <- df$title
reports <- tibble(
filename = str_c(title, ".html"),
params = map(title, ~list(title = .))
)
reports %>%
select(output_file = filename, params) %>%
pwalk(rmarkdown::render, input = "template.Rmd", output_dir = "output")
库(stringr)
图书馆(tidyverse)
所有权%
pwalk(rmarkdown::render,input=“template.Rmd”,output_dir=“output”)
多谢各位 文件
模板.Rmd
:
---
title: "`r params$title`"
date: "`r params$date`"
params:
title: no # default
date: no # default
author: "Darren Tsai"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
On `r params$date`, she said `r params$title`.
library(tidyverse)
df <- data.frame(title = c("Hello there", "Good morning", "Good afternoon", "Good evening"),
date = c("4 Jan 2019", "6 Jan 2019", "10 Jan 2019", "14 Jan 2019"))
df2 <- df %>%
mutate(filename = str_c(title, ".html"))
df2
# title date filename
# 1 Hello there 4 Jan 2019 Hello there.html
# 2 Good morning 6 Jan 2019 Good morning.html
# 3 Good afternoon 10 Jan 2019 Good afternoon.html
# 4 Good evening 14 Jan 2019 Good evening.html
df2 %>%
pwalk(~ rmarkdown::render("template.Rmd", output_file = ..3,
params = list(title = ..1, date = ..2)))
R脚本:
---
title: "`r params$title`"
date: "`r params$date`"
params:
title: no # default
date: no # default
author: "Darren Tsai"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
On `r params$date`, she said `r params$title`.
library(tidyverse)
df <- data.frame(title = c("Hello there", "Good morning", "Good afternoon", "Good evening"),
date = c("4 Jan 2019", "6 Jan 2019", "10 Jan 2019", "14 Jan 2019"))
df2 <- df %>%
mutate(filename = str_c(title, ".html"))
df2
# title date filename
# 1 Hello there 4 Jan 2019 Hello there.html
# 2 Good morning 6 Jan 2019 Good morning.html
# 3 Good afternoon 10 Jan 2019 Good afternoon.html
# 4 Good evening 14 Jan 2019 Good evening.html
df2 %>%
pwalk(~ rmarkdown::render("template.Rmd", output_file = ..3,
params = list(title = ..1, date = ..2)))
库(tidyverse)
df%
pwalk(~rmarkdown::render(“template.Rmd”,输出文件=…3,
参数=列表(标题=…1,日期=…2)))
.1
、.2
和.3
代表df2
中的标题、日期、和文件名
注意:您在render()
的参数params
中设置的内容必须在YAML中声明,因此在我的Rmd文件中,我在params
下将默认值no
设置为title
和date
检查四个HTML文件:
如果我正确读取了核心代码,则您没有将
params
传递到rmarkdown::render
函数。这完全按照预期工作。我唯一添加到rmarkdown::render
的是output\u dir=“output”
以保持文件夹的组织。我从示例数据移动到主数据集,在50个文件之后,我得到了以下错误:yaml::yaml.load(…,eval.expr=TRUE)中的错误:解析器错误:在解析第1行的块映射时,第1列在第1行第49列未找到所需的键,我想这可能是缺少的数据项,但我已检查并没有找到缺少的值。@Kranja很抱歉……我没有您的文件和数据集,因此我无法仅从您的注释中理解发生了什么。我认为这是一个与原始问题不同的问题,因此您可以将其作为新问题发布。缺少一个条目,并且脚本没有类似于tryCatch
的错误处理选项。