1

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...?
}
D. Studer
  • 1,711
  • 1
  • 16
  • 35

1 Answers1

1

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.

Dubukay
  • 1,764
  • 1
  • 8
  • 13
  • Thanks, that works very well. Can I also pass other variables by defining them within the for-loop? – D. Studer Nov 27 '22 at 09:58
  • Yep, absolutely. You can either define them inside the for loop or use an index (for i in 1:whatever) and then define each one as variable_vector[i] – Dubukay Nov 27 '22 at 19:39