I agree that I wish I knew of some clear documentation on which chunk options the various Run buttons take into consideration, and under what circumstances. I'm sorry that this answer isn't going to provide much clarity there.
I think some of the trouble here stems from the fact that runnable cells are a notebook feature that originated with the R Notebook format, and were then extended to regular R Markdown documents. The conceptual model for non-notebook R Markdown documents has always been that you will in fact press the knit button over and over (i.e. compile the entire document in a batch mode via render::rmarkdown()
, which is invoked by the Knit button).
If you want to understand more deeply how R Markdown and knitr
relate to computational notebooks, I highly recommend this (long!) post:
A couple of excerpts that start to get at the bigger issues behind the design decisions I think you're bumping up against:
I care a lot about reproducibility, so does RStudio, and we have made a decision on Day One that R Markdown documents should be compiled in new R sessions, which will (mostly) get rid of the problems of hidden state and out-of-order execution. Although many users have complained about it and wanted to run R Markdown documents in the current R session instead, we have never changed the decision. After we created R Markdown notebooks, we have also been constantly alerting users about the potential problems of notebooks. For example, in our book āR Markdown: The Definitive Guideā, we emphasized this issue over and over again:
[ā¦] Again, for the sake of reproducibility, you will need to compile the whole document eventually in a clean environment.
R Markdown notebooks are just plain text R Markdown documents. The ānotebookā is only a special execution mode of R Markdown documents (as we mentioned in the book āR Markdown: The Definitive Guideā). Or you may treat it as a by-product instead of a product. If you like it, use it; if you donāt, use the original R Markdown mode, i.e., the batch modeāclick the
Knit
button to compile the whole document in a new R session every time.
Being able to run individual cells in non-notebook R Markdown documents is then a convenience for quick interactive iteration, but not meant to be equivalent to compiling. But then things get muddy because the inline chunk output seems to (sort of) pay attention to (some of) the chunk options.
If you run these examples with the "Chunk Output in Console" option set (gear menu at top of document, or there's also a Global Option for it), you'll notice that none of the chunk options has any effect. And you don't really expect it to, because it's obvious that the code is just running in the console like usual. This is maybe a clearer paradigm? But inline chunk output is undeniably nice in its own ways. There's currently a discussion on the RStudio IDE GitHub about whether inline chunk evaluation is the correct default for non-notebook R Markdown documents, and which explains some more of the history:
TL;DR
Despite the way that inline chunk output pays attention to some of the chunk options, running chunks isn't the same thing as rmarkdown::render()
(which is what the Knit button actually calls), so I agree with @grosscol that you're going to have to roll your own setup/teardown system that will work when running individual chunks. You might be able to do something clever by testing for interactive()
(which returns TRUE
when using Run buttons, FALSE
when compiling), but I suspect it's still going to feel clunky and duplicative.
The other alternative seems to be always compiling the document instead of using the Run buttons ā but if you hate clicking as much as Yihui does, maybe xaringan::inf_mr()
can help? (it's not just for slides!)