2

Suppose that we have an .Rmd (or .Rnw) document and and an R module, which are connected via read_chunk(). In the R module, we would like to have an ability to check the run-time status of knitr, in other words, to verify whether execution of the module's code chunks occurs under knitr's control, or it's a simple R session. How this could be done? I thought about initializing a global flag in the first code chunk, where knitr gets loaded, but I'm not sure, if it's the best solution and even whether it would work.

UPDATE:

I just verified my global variable approach and it didn't work. Not sure whether I'm doing something wrong, or I don't understand the mechanics of knitr's loading and initialization. I have done the following: simply initialized the flag to FALSE in the beginning of an R module and then reset the flag to TRUE in the first chunk in .Rmd document, where knitr gets loaded. Then I placed print() statement somewhere in the R module and ran it as R session and via processing .Rmd document. Regardless, it prints FALSE.

Aleksandr Blekh
  • 2,462
  • 4
  • 32
  • 64
  • While nobody seems to be interested in answering this question, I've figured a **workaround** for my problem. I think that I can just leave both `knitr`-*specific* and *independent-R-session-specific* statements side by side in the `R` module and simply use `knitr`'s `include = FALSE` *directive* in corresponding chunks in the `.Rmd` document to **disable** the `R` session output, thus, preventing it from mixing with `knitr`'s output while in the `knitr` mode. – Aleksandr Blekh Sep 12 '14 at 03:16
  • 1
    If you wanna be able to simply check if code is executed within a knitr context, see [this answer](https://stackoverflow.com/a/33121933/7196903). – Salim B Oct 26 '18 at 02:36
  • @SalimB Thank you for the link (+1). Will keep it in mind. – Aleksandr Blekh Oct 26 '18 at 04:26

0 Answers0