R Markdown / Quarto - Avoid loading package every time I knit a document
07:49 26 Sep 2022

Simple question but can't find a solution. I have a quarto document (but this apply to Markdown as well) in which I use R to execute some code. Obviously, in the first chunk of the document, I load the packages needed (let's say for example):

```{r setup}
library(tidyverse)
library(survival)
library(survminer)
```

Now, everytime I knit the file to render the document, these packages are loaded, which can be pretty time consuming especially if you have a long list of packages to import. using cache=TRUE doesn't seem to work properly. Is there anyway to avoid loading the package everytime I knit the document, and only load them when they are not loaded in the environment/in the first knit call of the session at least?

r r-markdown markdown quarto