Differentiate Rmarkdown by how it's being used

I am trying to differentiate between 3 different situations in my Rmarkdown:

  1. ctrl + shift + k knit,
  2. opening up the Rmd and running code by chunks, and
  3. rendering it using rmarkdown::render.

I can differentiate the first one as it gives FALSE for interactive() whereas the other two situation gives TRUE. However, I don't know how to differentiate them further. I know I can add a params to the rmarkdown::render function, but I rather not if I don't have to. Appreciate any suggestions. Thanks heaps.

Why would you want to differentiate ? What are you trying to do ?
Essentially, 1 and 3 are rendering a document. 2 is a IDE feature in RStudio IDE.

1 will execute rmarkdown::render() in background job
2 will not run a render() the IDE will just run the code chunk (not even running knitr)
3 depends where you call it - in the R console opened, then it will render in the current session, but you could also run the command in background job. interactive()value will depend on that.

We need to know which problem you are trying to solve to help you with that I think

I have a personal package that I call in the Rmarkdown, but it doesn't work when running the code by chunk, hence it means I won't be able to do run all chunks before.

I have now resolved the problem using tryCatch, I just thought that there should be a way to differentiate between those scenarios. I have looked into system variables and they seem identical for option 2 and 3, and they are both considered interactive.

Personally no longer need solution to this, but still curious whether this is possible.

1 Like

Do you want to make it work ? Or does it not work on purpose ?

For the case or running chunk by chunk, it would be maybe an IDE feature request to check an env var for example to know that function is run by RStudio feature. It is not a package thing really.

Yes in both case R is run interactively.

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