Iterate multiple RMarkdown reports

Can anyone point me to examples of using an R Markdown file sourced as a function? I can create a function with Openxlsx statements, for example, and use purrr to iterate through a list of tibbles, producing an Excel file with multiple worksheets. What I want is to produce multiple stand-alone PDFs of tables and graphs by iterating through the list of tibbles.

thought example:

map(LOT, source(R Markdown.rmd))

Or is there a better way to accomplish what I want?

Many thanks.

Hi @ChicagoDave,

I am trying to make sure I fully understand your question before suggesting the approach I would use. Are you wanting to be able to have some iterative process that feeds data/parameters to a .Rmd document so that the document renders specifically using those data?

That is what I want. Exactly.

1 Like

Okay, my suggestion would be as follows (see ?rmarkdown::render for details on the params argument):

  1. Parameterize the R Markdown file such that it can accept data frames as parameters

  2. One call to the function rmarkdown::render('file.Rmd', params = list(data = 'data1')) will render the document with data1

  3. Extend this by walking the function rmarkdown::render with purrr::walk() to iterate over many data frames.

purrr::walk(name_of_data_frames, ~rmarkdown::render('file.Rmd', params = list(data = .), output_file = glue::glue('file_{.}.pdf'))

EDIT: You can learn more about parameterized reports here

1 Like

This is fantastic -- thank you so much. My dive into parameterized reports will now be so much more efficient!

2 Likes

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.