Q:

How to detect if function is called inside Rstudio or Jupyter notebook

I am working on a 'high-level' function that requires user input. The way the function asks for input should be different, depending if it is called in an Rstudio session, Jupyter Notebook or terminal.
How can one test for these environments?
I have a really hard time searching for this issue. I have been thinking to use interactive() and getOption("device") to deduct where I am, but I was hoping there might be a more explicit way to achieve this.

A:

RStudio also always sets an environment variable RSTUDIO, among others.
In RStudio:
R> Sys.getenv("RSTUDIO")
[1] "1"
R> 

Standard session:
R> Sys.getenv("RSTUDIO")
[1] ""
R> 

So you can compare to "":
R> Sys.getenv("RSTUDIO") == ""
[1] TRUE
R> 

