What defines the setup chunk?

Until today I thought the "setup chunk" of an R Markdown document was the one with a "setup" label, but I can't find any reference, so am now wondering what makes a chunk the setup chunk. Its label? Its position? Its containing knitr::opts_knit$set? Thanks!


E.g. in

I see "This option is only effective when used inside the setup chunk." but it does not say what makes a chunk a setup chunk


My guess is that the setup chunk is used only in the context of the RStudio IDE and defined by its label.
See for instance these lines of the RStudio IDE source code:


I think it's the scope of the chunk that makes it "setup-y"— e.g. from the knitr documentation

opts_chunk$set() can change the default global options in a document (e.g. put this in a code chunk: knitr::opts_chunk$set(comment=NA, fig.width=6, fig.height=6) ).


You are right- the links are scarce on this one. Here is a snippet from R4DS:

"There is one chunk name that imbues special behaviour: setup . When you’re in a notebook mode, the chunk named setup will be run automatically once, before any other code is run."

And a bit more from R Markdown: The Definitive Guide in the Notebooks chapter:

"If it is necessary to execute notebook chunks in a different directory, you can change the working directory for all your chunks by using the knitr root.dir option...This option is only effective when used inside the setup chunk."


Also, I think the setup chunk is only something useful for IDE feature as it was already pointed out in this discussion.

However, it is also useful for shiny prerendered document were the setup chunk is expected to be labelled setup. A chunk with label setup means the setup context.

It is an alias for c("render", "server-start").

I really wonder if this setup chunk does not come from this shiny thing inside Rmd with the context thing. :thinking:

Apart from that, a chunk named setup is useful for some IDE behaviour for Rmd and Notebook.


Thanks everyone! :grinning:

So we'll keep never unnaming that chunk in the namer package. :angel:


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