41

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.

Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131

10 Answers10

61

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.

krlmlr
  • 25,056
  • 14
  • 120
  • 217
22

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/

coatless
  • 20,011
  • 13
  • 69
  • 84
  • 5
    Unfortunately, this one isn't perfect -- `.Platform$GUI` is not set until _after_ the user `.Rprofile` has been executed (for complicated reasons), and so won't be sufficient for startup scripts. In general, checking the `RSTUDIO` environment variable is preferred. – Kevin Ushey Sep 23 '16 at 18:37
  • 1
    @KevinUshey, for those following at home, the suggested approach is then what was mentioned by krlmlr: `Sys.getenv("RSTUDIO") == "1"`? Though, peaking at `rstudioapi::isAvailable`, also mentioned by krlmlr, shows `.Platform$GUI` being used in the check. – coatless Sep 24 '16 at 14:42
  • 3
    I think that would be a bug in `rstudioapi` then :) I'll open an issue! – Kevin Ushey Sep 24 '16 at 16:53
  • 7
    Note that despite the issue in Kevin's comment, this correctly distinguishes between code run in the RStudio console and run through the Terminal tab in RStudio, unlike the `RSTUDIO` environment variable. – Hugh Oct 30 '18 at 06:18
  • FWIW, this is the approach employed in the `manipulate` package to distinguish if the IDE is RStudio. – epsilone Aug 26 '20 at 11:18
  • For completeness, the [implementation of RStudio detection](https://github.com/klmr/box/blob/62cfa4f4a0b836fcadbc7a63de6b9b56857ffb8b/R/paths.r#L185-L195) in the ‘box’ package combines this answer with @KevinUshey’s comment. I’ve found this to be the only robust way. – Konrad Rudolph May 11 '22 at 13:39
11

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.

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
8

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.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • 1
    Perhaps this should be `rstudio::versionInfo()$version` now instead of `rstudio::version()`. – krlmlr Aug 25 '14 at 07:14
7

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

Uwe
  • 41,420
  • 11
  • 90
  • 134
3

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 ...
GSee
  • 48,880
  • 13
  • 125
  • 145
Dieter Menne
  • 10,076
  • 44
  • 67
  • On my setup, Sys.getenv("RSTUDIO_USER_IDENTITY") returns "" whether it is run inside or outside Rstudio – RockScience Aug 26 '14 at 04:10
  • I checked, it still works ok for me (Windows 7 Desktop). I also checked it on a Virtual Box running Ubuntu and RStudio Server. Please specify your setup so others can check. – Dieter Menne Aug 26 '14 at 06:57
  • What do you want to know? packageVersion("rstudio") returns '0.97.551', version returns: platform x86_64-w64-mingw32 arch x86_64 os mingw32 system x86_64, mingw32 version.string R version 3.1.1 (2014-07-10) – RockScience Sep 01 '14 at 08:04
2

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.

2

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"))
}
Konrad
  • 17,740
  • 16
  • 106
  • 167
1

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.

Davor Josipovic
  • 5,296
  • 1
  • 39
  • 57
Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88
0

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"