Sacha Epskamp
Sacha Epskamp

Reputation: 47541

Check if R is running in RStudio

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 testing if a certain system variable was set.

Upvotes: 45

Views: 29840

Answers (10)

Kevin Ushey
Kevin Ushey

Reputation: 21285

The most reliable way to detect whether code is running in the main RStudio R session (without relying on the rstudioapi package) is:

commandArgs()[[1L]] == "RStudio"

Some comments on other answers:

  • Checking environment variables may be insufficient, as the "RSTUDIO" environment variable will also be inherited by child R processes launched from the main R session.

  • RStudio does not initialize its R infrastructure until after R profile scripts are run (e.g. ~/.Rprofile), so checking things like .Platform$GUI == "RStudio" or "tools:rstudio" %in% search() or rstudioapi::isAvailable() won't give what you expect in that context.

Upvotes: 2

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

There is no "running inside RStudio". RStudio is merely an IDE layer that wraps around R; at the end of the day it just launches the normal R executable you need to have on your $PATH anyway to operate RStudio.

As a proxy, and as R Studio You could test available.packages() for the 'manipulate' package though, or as a shorter version see if RStudio added itself to the .libPaths() content:

R> any(grepl("RStudio", .libPaths()))
[1] TRUE
R> 
R> 

Edit in May 2020 or eight years later The question does come up, and one can query a variety of things from within. Here is an example from the terminal of RStudio:

$ env | grep -i rstudio | sort
GIO_LAUNCHED_DESKTOP_FILE=/usr/share/applications/rstudio.desktop
PATH=[...redacted...]
RMARKDOWN_MATHJAX_PATH=/usr/lib/rstudio/resources/mathjax-27
RS_RPOSTBACK_PATH=/usr/lib/rstudio/bin/rpostback
RSTUDIO=1
RSTUDIO_CONSOLE_COLOR=256
RSTUDIO_CONSOLE_WIDTH=111
RSTUDIO_PANDOC=/usr/lib/rstudio/bin/pandoc
RSTUDIO_PROGRAM_MODE=desktop
RSTUDIO_PROJ_NAME=chshli
RSTUDIO_SESSION_ID=9C62D3D4
RSTUDIO_SESSION_PORT=13494
RSTUDIO_TERM=2BD6BB88
RSTUDIO_USER_IDENTITY=edd
RSTUDIO_WINUTILS=bin/winutils
$ 

Similarly, from within the R session:

R> se <- Sys.getenv()
R> se[grepl("rstudio",se,ignore.case=TRUE)]
GIO_LAUNCHED_DESKTOP_FILE        /usr/share/applications/rstudio.desktop
PATH                             [...also redacted...]
RMARKDOWN_MATHJAX_PATH           /usr/lib/rstudio/resources/mathjax-27
RS_RPOSTBACK_PATH                /usr/lib/rstudio/bin/rpostback
RSTUDIO_PANDOC                   /usr/lib/rstudio/bin/pandoc
R> 

Edit in Aug 2021 or nine years later As all the answers listed here in the different answer may still be too much for people, you can also install package rstudioapi from CRAN and then ask it via rstudioapi::isAvailable() which comes back TRUE for me inside RStudio and FALSE in ESS / standard R.

Upvotes: 11

Konrad
Konrad

Reputation: 18585

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"))
}

Upvotes: 2

Uwe
Uwe

Reputation: 42544

As of today, there are a few packages which include functions to check whether RStudio is running:

rstudioapi::isAvailable()
assertive::is_rstudio()

(list is non-exhaustive)

The assertive and assertive.reflections packages, resp., do include additional functions to check for other IDEs, desktop/server versions of RStudio, and various R releases (e.g., alpha, beta, devel, release, patched, etc.)

Upvotes: 7

coatless
coatless

Reputation: 20736

Check the .Platform$GUI option for "RStudio"

is.rstudio = function(){
  .Platform$GUI == "RStudio"
}

See:

http://thecoatlessprofessor.com/programming/detecting-if-r-is-in-rstudio-and-changing-rstudios-default-graphing-device/

Upvotes: 24

krlmlr
krlmlr

Reputation: 25444

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.

Upvotes: 65

user3795816
user3795816

Reputation: 21

I find the following works for me

checkRstudio <- function () {
  return ("tools:rstudio" %in% search())
}

I am sort of new to R myself, but I believe Rstudio necessarily loads the package "tools:rstudio" in order to run.

Upvotes: 2

user3785496
user3785496

Reputation: 1

On a Mac only the Sys.getenv answer works

platform x86_64-apple-darwin10.8.0
version.string R version 3.1.0 (2014-04-10)

Sys.getenv("RSTUDIO")=="1" [1] TRUE

RStudio.version() Error: could not find function "RStudio.version"

any(grepl("RStudio", .libPaths())) [1] FALSE

.libPaths() [1] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library"

Upvotes: 0

Dieter Menne
Dieter Menne

Reputation: 10205

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 ...

Upvotes: 3

Spacedman
Spacedman

Reputation: 94162

When I start RStudio it seems to have tools:rstudio in position 2 on the search path. This has a function "RStudio.version" which is undocumented but seems to return the RStudio version string:

> RStudio.version()
[1] "0.96.316"

So you can define:

is.RStudio <- function(){
  if(!exists("RStudio.version"))return(FALSE)
  if(!is.function(RStudio.version))return(FALSE)
  return(TRUE)
}

and maybe use that.

Upvotes: 8

Related Questions