R Markdown 根据列中的值创建输出文件
R Markdown Creating Output Files base on Values in a Column
我正在尝试在 R Markdown 中创建单独的收据文档。我已经使用了 this link 这真的很有帮助。 https://rmarkdown.rstudio.com/articles_mail_merge.html
我需要的与以下过程不同的一件事是,我不需要将文件命名为“handout1、handout2、handout3...”,而是需要保存在数据列中的更具体的文件名放。使用 linked 示例中的代码,我需要在文件名中包含人名。有办法吗?
R 脚本:
## Packages
library(knitr)
library(rmarkdown)
## Data
personalized_info <- read.csv(file = "meeting_times.csv")
## Loop
for (i in 1:nrow(personalized_info)){
rmarkdown::render(input = "mail_merge_handout.Rmd",
output_format = "pdf_document",
output_file = paste("handout_", i, ".pdf", sep=''),
output_dir = "handouts/")
}
R 降价
---
output: pdf_document
---
```{r echo=FALSE}
personalized_info <- read.csv("meeting_times.csv", stringsAsFactors = FALSE)
name <- personalized_info$name[i]
time <- personalized_info$meeting_time[i]
```
Dear `r name`,
Your meeting time is `r time`.
See you then!
需要 R 脚本:此代码不起作用
## Packages
library(knitr)
library(rmarkdown)
## Data
personalized_info <- read.csv(file = "meeting_times.csv")
## Loop
for (i in 1:nrow(personalized_info)){
rmarkdown::render(input = "mail_merge_handout.Rmd",
output_format = "pdf_document",
output_file = paste(personalized_info$name.pdf, sep=''), # grab the name from the name
column of personalized_info at i
output_dir = "handouts/")
}
我建议您在 yaml
header 中使用 params
属性,因为它可以更轻松地传递数据:
---
output: pdf_document
params:
name: ""
meeting_time: ""
---
Dear `r params$name`,
Your meeting time is `r params$time`.
See you then!
然后在 R
中:
for (i in 1:nrow(personalized_info)){
rmarkdown::render(input = "mail_merge_handout.Rmd",
output_format = "pdf_document",
output_file = paste(personalized_info$name[i], ".pdf", sep=''),
params = personalized_info[i,],
# grab the name from the name column of personalized_info at i
output_dir = "handouts/")
}
数据
personalized_info <- structure(list(name = c("John", "Jane", "Marry", "Larry", "Harry"
), meeting_time = structure(c(1606418788.4989, 1606505188.4989,
1606591588.4989, 1606677988.4989, 1606764388.4989), class = c("POSIXct",
"POSIXt"), tzone = "")), class = "data.frame", row.names = c(NA,
-5L))
我正在尝试在 R Markdown 中创建单独的收据文档。我已经使用了 this link 这真的很有帮助。 https://rmarkdown.rstudio.com/articles_mail_merge.html
我需要的与以下过程不同的一件事是,我不需要将文件命名为“handout1、handout2、handout3...”,而是需要保存在数据列中的更具体的文件名放。使用 linked 示例中的代码,我需要在文件名中包含人名。有办法吗?
R 脚本:
## Packages
library(knitr)
library(rmarkdown)
## Data
personalized_info <- read.csv(file = "meeting_times.csv")
## Loop
for (i in 1:nrow(personalized_info)){
rmarkdown::render(input = "mail_merge_handout.Rmd",
output_format = "pdf_document",
output_file = paste("handout_", i, ".pdf", sep=''),
output_dir = "handouts/")
}
R 降价
---
output: pdf_document
---
```{r echo=FALSE}
personalized_info <- read.csv("meeting_times.csv", stringsAsFactors = FALSE)
name <- personalized_info$name[i]
time <- personalized_info$meeting_time[i]
```
Dear `r name`,
Your meeting time is `r time`.
See you then!
需要 R 脚本:此代码不起作用
## Packages
library(knitr)
library(rmarkdown)
## Data
personalized_info <- read.csv(file = "meeting_times.csv")
## Loop
for (i in 1:nrow(personalized_info)){
rmarkdown::render(input = "mail_merge_handout.Rmd",
output_format = "pdf_document",
output_file = paste(personalized_info$name.pdf, sep=''), # grab the name from the name
column of personalized_info at i
output_dir = "handouts/")
}
我建议您在 yaml
header 中使用 params
属性,因为它可以更轻松地传递数据:
---
output: pdf_document
params:
name: ""
meeting_time: ""
---
Dear `r params$name`,
Your meeting time is `r params$time`.
See you then!
然后在 R
中:
for (i in 1:nrow(personalized_info)){
rmarkdown::render(input = "mail_merge_handout.Rmd",
output_format = "pdf_document",
output_file = paste(personalized_info$name[i], ".pdf", sep=''),
params = personalized_info[i,],
# grab the name from the name column of personalized_info at i
output_dir = "handouts/")
}
数据
personalized_info <- structure(list(name = c("John", "Jane", "Marry", "Larry", "Harry"
), meeting_time = structure(c(1606418788.4989, 1606505188.4989,
1606591588.4989, 1606677988.4989, 1606764388.4989), class = c("POSIXct",
"POSIXt"), tzone = "")), class = "data.frame", row.names = c(NA,
-5L))