Search code examples
rtidyversernotebook

Loop over an entire r-notebook


I was just wondering if it is possible to loop over a r-notebook from an external r file?

My dataset contains a group variable "group" and I should generate a report for each of the 32 groups separately (plus one for the groups together).

My notebook looks basically like this:

dat <- read_spss(...)

dat %>% ...
  ... %>%
  ggplot() %>%
  ggsave(here::here("image1_grp1.png"))

dat %>% ...
  ... %>%
  ggplot() %>%
  ggsave(here::here("image2_grp1.png"))

...

Now, my idea was to simply filter the dataset on top of the r-notebook like this:

dat <- read_spss(...) %>%
 filter(group == group)

and for the image filenames I would simply use ggsave(here::here(paste0("image",grp"2.png"))

But how can I run a notebook from an external r-script and pass the parameter grp to the notebook?

groups <- unique(dat$group)

for(group in groups){
   ...execute notebook...?
}

Solution

  • This is where purling comes in! Rather than knitting a notebook into HTML or other document type, you can purl it and extract all the R code into an Rscript that's then callable by an external script via source.

    In your project, it looks like you could do something along the lines of:

    knitr::purl("notebook_name.Rmd", output="notebook_script.R")
    
    for(group in groups){
      source("notebook_script.R")
    }
    

    which makes the "group" variable available to the script but behaves identically otherwise.