Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pre-render an R script for each chapter of a quarto book project

Tags:

r

bookdown

quarto

I have a bookdown project in R and like to switch to Quarto. In the bookdown project I have an R script _common.R (it includes libraries and various settings and helper functions) to run before the rendering of each chapter (separate qmd file).

How can I implement such a script-file in Quarto?

I tried to set in _quarto.yml:

project:
  type: book
  pre-render: _common.R

But this will only run the script once before the rendering of the complete book, not before each chapter. Each chapter seems to be rendered in its own environment so all the settings and scripts from _common.R are not available.

A workaround is to include

```{r echo = FALSE, message=FALSE, warning=FALSE}
source("_common.R")
```

at the top of each .Rmd/.qmd file. Is there a better solution?

Any help appreciated!

like image 843
wilbert Avatar asked Jan 30 '26 19:01

wilbert


1 Answers

As an alternative to your proposed workaround, you can use a project-specific .Rprofile file in your project and call the source inside that .Rprofile.

.Rprofile

source("_common.R")

And in this approach, you don't have to source the _common.R file at the start of each chapter qmd files.

like image 116
Shafee Avatar answered Feb 01 '26 12:02

Shafee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!