R-MARKDOWN:生成许多名称的Word文档
我正在根据我的数据框架生成Word文档。但是我想使用两个列作为参数。我如何为约翰(意大利)和约翰(英国)等单独创建报告
mydata<-
id employee country val1 val2 val3
1 John Italy 2 4 5
2 John UK 5 6 8
3 Mary USA 2 3 6
4 Andy Germany 5 6 7
5 Andy USA 7 8 9
rmarkdown::render
names <-c("John", "Mary", "Andy")
lapply(names, function(x) rmarkdown::render("template.Rmd", output_file = paste0("Employee-", x,), params = list(employee = x)))
。RMD:
---
title: "Employee"
author: "Rita"
date: "`r Sys.Date()`"
output: word_document
params:
employee: null
country: null
---
```{r, setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
Country: `r params$country`
Emp: `r params$employee`
```{r echo=FALSE, message=FALSE, warning=FALSE, comment='\t', results='asis'}
cat(sprintf("Your employee, %s from %s worked with %s mydata$val1 clients for %s hours", mydata$employee, mydata$country, mydata$val1, mydata$val2 ))
```
I am generating word documents based on my data frame. But I want to use two columns as parameters. How do I create separate reports for John(Italy) and John(UK) etc.
mydata<-
id employee country val1 val2 val3
1 John Italy 2 4 5
2 John UK 5 6 8
3 Mary USA 2 3 6
4 Andy Germany 5 6 7
5 Andy USA 7 8 9
rmarkdown::render
names <-c("John", "Mary", "Andy")
lapply(names, function(x) rmarkdown::render("template.Rmd", output_file = paste0("Employee-", x,), params = list(employee = x)))
Rmd:
---
title: "Employee"
author: "Rita"
date: "`r Sys.Date()`"
output: word_document
params:
employee: null
country: null
---
```{r, setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
Country: `r params$country`
Emp: `r params$employee`
```{r echo=FALSE, message=FALSE, warning=FALSE, comment='\t', results='asis'}
cat(sprintf("Your employee, %s from %s worked with %s mydata$val1 clients for %s hours", mydata$employee, mydata$country, mydata$val1, mydata$val2 ))
```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
lapply
基于ID而不是名称,即使文档未使用ID参数。IE
lapply
based on IDs rather than names, even if the ID parameter isn't used by the document.i. e.