Quarto equivalent to RMarkdown setup chunk?

I am slowly converting to using Quarto instead of RMarkdown. One thing I still haven't figured out is if there is an equivalent to the RMarkdown setup chunk. E.g., I always used to start my RMarkdown documents with a chunk like

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

This would allow me to avoid seeing all the messages and warnings from packages when I knit the document, and made sure it would be run before things below. Is there something similar for Quarto? I have tried context: setup and label: setup but neither of them seem to do it.

1 Like

I'm now realizing that the piece that might have hidden the messages and warnings was the default include=FALSE that the setup chunk usually came with. Maybe I should add that to my Quarto docs! The other piece still holds-- does label: setup or context: setup offer any of the benefits of the RMarkdown setup chunk?

1 Like

This topic was automatically closed 21 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.

Sorry for the no answer on this. Asking on Quarto Discussion (Discussions · quarto-dev/quarto-cli · GitHub) will often get you a more quicker response by the dev team, as Community member may not be able to help with all question here on Quarto.

setup chunk has a special meaning in the RStudio IDE I believe, when interactively running chunks. It was introduced first for Shiny Documents. I believe this why it gets confusing.

Initially it is context for shiny, which should be supported in Quarto to

but Chunk label are used as a shortcut instead of setting context. See explanation in

Above we’ve shown the use of the context attribute to associate chunks with various execution contexts. The label of R chunks can also serve as a shorthand way of specifying a context (e.g. a chunk with label setup automatically gains the context="setup" attribute).

That is why both label: setup or context: setup should work. However, in Quarto the former is no shorthand of the latter, so using context should probably be preferred.

hope it helps

/# label: setup did work for me, but /# context: setup did not.

@petzi53 Are you in a shiny prerendered context ?

Or just expecting the IDE to run first the setup chunk no matter which chunk you interactively use ?

Maybe I did not understand well what is the expected behavior of label: setup you are waiting ?

Can you help me understand ?

Thanks

I was still referring to the question of the OP: to find "an equivalent to the RMarkdown setup chunk". In RMarkdown you can say:

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

The same effect of a setup chunk happens in a quarto document with the following code:

```{r}
#| label: setup
library(tidyverse)
```

But you mentioned that it should also work with context: setup. So I tried it with:

```{r}
#| context: setup
library(tidyverse)
```

Following by another code chunk to test it:

```{r}
tibble(toss = c("w", "l", "w", "w", "w", "l", "w", "l", "w"))
```

Now I restarted R (to clean the environment) and executed only the second chunk with the tibble() function. R knows that it comes from loading library(tidyverse) in the setup chunk with the line label: setup but I received an error message with the line context: setup.

Hopefully, I could make clear my previous short post: #| label: setup worked, but #| context: setup did not work for me. Or the question posted: How do I have to apply the option context: to turn a code chunk to a setup code chunk?

I think I misundertood the first question. context: setup is only for the shiny usage Quarto - Execution Contexts

it is not useful is not in a shiny context. Only #| label: setup is useful in this case if the RStudio IDE correctly respect it.

Hope it clarifies

1 Like