I am looking for a way to test if R is being run from RStudio. For some reason I could find the answer on google yesterday but not today, but I think it had to do with testi
Neat solution is now available through the startup package via the is_rstudio_console
function:
startup:::is_rstudio_console()
[1] TRUE
It may be worth adding that this function checks for two environment variables, RSTUDIO
, which was mentioned in the answer by @krlmr and RSTUDIO_TERM
that doesn't seem to be mentioned across the preceding answers at the moment.
function ()
{
(Sys.getenv("RSTUDIO") == "1") && !nzchar(Sys.getenv("RSTUDIO_TERM"))
}
This is from ?rstudio
:
# Test whether running under RStudio
isRStudio <- Sys.getenv("RSTUDIO") == "1"
There is also rstudioapi::isAvailable()
, but checking this is not as reliable because RStudio doesn't seem to really need the rstudioapi
package to work correctly.
To add to the number of nice guesses, here is a message from 2011 (Ice Age)
http://support.rstudio.org/help/discussions/problems/413-location-of-installed-packages
if (Sys.getenv("RSTUDIO_USER_IDENTITY")!= ""){
.libPaths(.Library) # Avoid additional libraries } else { # not rstudio ...