Automatic execution of setup chunk in Rmd

A few of us have been exploring on a twitter thread the idea that:

If you have a code block named 'setup' like

```{r setup} 
foo()
```

then every time you restart RStudio and execute any code in the middle of your markdown document, this block will be automatically run once before, i.e. your libraries will be loaded first.

Some people in the thread seem to be able to get this to work, but it doesn't work for me.

Here's a sort-of reprex as a github gist.

Using the reprex above, if I start a fresh R session, go to the test chunk and execute it without running the setup chunk first, I get an error (can't find function tibble, which dplyr should load). So it seems to me that the setup chunk is not automatically read in prior to running a subsequent chunk.

See further relevant tweets and possibly relevant answer from Alison.

I am using RStudio 1.3.959.
It's all a bit of a mystery. What am I missing?

This still isn't working for me. Any ideas, anyone?

Summary: After starting a fresh R session, manually running a chunk in an Rmd file does not automatically run the setup chunk at the top of the file, as some people say it should. I know the setup chunk is not being successfully executed because I get "could not find function" errors in the chunk below, showing that library() calls in the setup chunk are not being run.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Hi there- in my answer you linked to I referenced using the IDE in "notebook mode" being the key, but then I didn't tell you how to get there! So notebook mode means that you have your IDE/Rmd preferences set up to show output inline. Here is how you do that:

2 Likes