Mass create documents in R markdown

拈花ヽ惹草 提交于 2019-12-04 22:47:35

问题


I'm wondering if anyone can help me with a dilemma I'm having.

I have a dataset of around 300 individuals that contains some basic information about them (e.g. age, gender). I want to knit a separate R markdown report for each individual that details this basic information, in Word format. And then I want to save each report with their unique name.

The actual code which sits behind the report doesn't change, only the details of each individual. For example:

Report 1: "Sally is a female who is 34 years old". (Which I would want to save as Sally.doc)

Report 2: "Mike is a male who is 21 years old." (Saved as Mike.doc)

Etc, etc.

Is there a way I can do this without manually filtering the data, re-kniting the document, and then saving manually with unique names?

Thanks a lot!


回答1:


Use the render function and pass a list of names to the function:

renderMyDocument <- function(name) {
  rmarkdown::render("./printing_procedures/dagr_parent.Rmd",
                    params = list(names = name),

                    output_file = paste("~/document_", name, '.doc', sep = '')
  )
}

lapply(names, renderMyDocument)

Then just make sure your RMD file can take the params argument via the YAML:

---
params:
  names:  
---    

RStudio documentation on parameterized reports here: https://rmarkdown.rstudio.com/developer_parameterized_reports.html



来源:https://stackoverflow.com/questions/49301776/mass-create-documents-in-r-markdown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!