Based on some simple tests, interactive()
is true when running code within rmarkdown::render()
or knitr::knit2html()
. That is, a simple .rmd
file containing
```{r} print(interactive()) ```
gives an HTML file that reports TRUE
.
Does anyone know of a test I can run within a code chunk that will determine whether it is being run "non-interactively", by which I mean "within knit2html() or render()"?
If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .
When you run render , R Markdown feeds the . Rmd file to knitr, which executes all of the code chunks and creates a new markdown (. md) document which includes the code and its output. The markdown file generated by knitr is then processed by pandoc which is responsible for creating the finished format.
Chunk options For example, echo=FALSE indicates that the code will not be shown in the final document (though any results/output would still be displayed). You use results="hide" to hide the results/output (but here the code would still be displayed).
The usual way to compile an R Markdown document is to click the Knit button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K ( Cmd + Shift + K on macOS). Under the hood, RStudio calls the function rmarkdown::render() to render the document in a new R session.
As Yihui suggested on github isTRUE(getOption('knitr.in.progress'))
can be used to detect whether code is being knitted or executed interactively.
A simple suggestion for rolling your own: see if you can access current output format:
```{r, echo = FALSE} is_inside_knitr = function() { !is.null(knitr::opts_knit$get("out.format")) } ``` ```{r} is_inside_knitr() ```
There are, of course, many things you could check--and this is not the intended use of these features, so it may not be the most robust solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With