Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mass create documents in R markdown

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!

like image 551
bsuthersan Avatar asked Mar 15 '18 14:03

bsuthersan


1 Answers

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

like image 65
Ryan Morton Avatar answered Oct 24 '22 19:10

Ryan Morton