Generating multiple "blank" .Rmd child files from a list of names in a for loop (or similar)


I am writing a report in R Markdown which will contain one section (out of many). This one section, in turn, will contain many sub-sections (> 20). Each of these sub-sections will contain mainly text. In order to dynamically control whether one or more of these sub-sections is included in the final report, my intention is to include all of these sub-sections through "child" .Rmd files, and controlling their inclusion (or not) through code chunks and the eval option in the main .Rmd file (how this is achieved is not relevant for the question at hand), like so:

```{r child = '[name].Rmd', eval = TRUE}

Where [name].Rmd is the relevant "child" .Rmd file containing the text pertaining to the sub-section it is included under.

Since there are many sub-sections there is a need for equally many "child" .Rmd files. Instead of creating and naming these "child" .Rmd files manually, I am looking for a solution whereby I can generate these "child" .Rmd files after looping through a data frame of names and output these .Rmd files to a relevant location on my drive.

Let's say I have a data frame with the following names.


names <- tibble(
  name =  c("name_one", "name_two")

What I want to achieve (or at least what I envision) is something that loops through each of these names, and outputs an .Rmd for each of the names (for the example above, that would be two files: name_one.Rmd and name_two.Rmd). The contents of each "child" .Rmd file thus output should be blank/containing the bare minimum necessary, since these will mainly contain text that I have to input manually (unfortunately). I have tried achieving this using the rmarkdown::render function, as such (the template.Rmd is the "blank" .Rmd file I am basing the "children" on):

for (i in 1:length(names$name)) {
         output_file = paste0(names$name, ".Rmd"),
         output_dir = here::here()))

This outputs the relevant .Rmd files, but these are not possible to open and contain (what is to me) jibberish - which I suspect has something to do with the intention of the rmarkdown::render function is to output HTML, PDF or Word files.

Is there anyone out there that has a suggestion on how I can achieve my goal? Any nudges in the right direction would be greatly appreciated!

Thanks for a great community.


I came across the {yamlme} package that includes a write_rmd function. With this, I was able to output .Rmd files with the desired format (i.e. "blank"), using the following loop.

for (i in 1:length(names$name)) {
    title = names$name[i],
    output = "pdf_document",
    body = txt_body(names$name[i]), # Only included so as to check that the content of each .Rmd file is output correctly
    filename = paste0(names$name[i], ".Rmd")) # This was mainly what I wanted to achieve, i.e. the programmatical creation of each child .Rmd

One problem, however, is that the write_rmd function does not allow for the specification of the output destination of the .Rmd files. Are there any suggestions as to how I can achieve this, i.e. output the "child" .Rmd files to a sub-folder of the R Project that I house the main .Rmd file in?



render() is really about rendering a Rmd file to an output using knitr + pandoc. If you only want to create new Rmd file, without rendering anything but just something like copying + adjusting template I would use other tools

  • Within rmarkdown package, there is the draft function which allow to you to create new Rmd based on a template: Create a new document based on a template — draft • rmarkdown

  • You could simply have a template.Rmd that you copy and rename in your loop. fs::file_copy() and other functions can help

  • Template package like whisker can also be a good solution when you are just tweaking a text file. This allows to have a file template, which could contain some variable fields and you would whisker::whisker.render() your template to another file with variable fields filled in.
    On this, knitr contains a knitr::knit_expand() function that could serve the same purpose.

Using one of those tool, I believe you can easily loop through variable to create multiple file from the same template.

Thank you, Christophe - the {fs} package did the trick for me!

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.