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

- 25,056
- 14
- 120
- 217
Check the .Platform$GUI
option for "RStudio"
is.rstudio = function(){
.Platform$GUI == "RStudio"
}
See:

- 20,011
- 13
- 69
- 84
-
5Unfortunately, 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
-
3I think that would be a bug in `rstudioapi` then :) I'll open an issue! – Kevin Ushey Sep 24 '16 at 16:53
-
7Note 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
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.

- 46,417
- 11
- 121
- 167

- 360,940
- 56
- 644
- 725
-
This works only if the .libPaths has not been changed by a script, which is the case on my company setup – RockScience Aug 22 '14 at 09:29
-
1Well, as they say, "if you break it, you get to keep the pieces". – Dirk Eddelbuettel Aug 22 '14 at 14:33
-
1another solution (but again, works if you don't load rstudio manually ;) "package:rstudio" %in% search() – RockScience Aug 26 '14 at 04:05
-
This one failed for me in RStudio `1.1.442`, with `sessionInfo()` giving `R version 3.5.0 (2018-04-23), Platform: x86_64-apple-darwin15.6.0 (64-bit), Running under: macOS High Sierra 10.13.6`. – miguelmorin Oct 09 '18 at 13:25
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.

- 92,590
- 12
- 140
- 224
-
1Perhaps this should be `rstudio::versionInfo()$version` now instead of `rstudio::version()`. – krlmlr Aug 25 '14 at 07:14
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.)

- 41,420
- 11
- 90
- 134
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 ...

- 48,880
- 13
- 125
- 145

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

- 21
- 1
-
-
1Not necessarily. This can be adjusted in `options` and changed in a `.Rprofile` so as not to be loaded on startup. – Rich Scriven Aug 26 '14 at 04:11
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"))
}

- 17,740
- 16
- 106
- 167
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()
orrstudioapi::isAvailable()
won't give what you expect in that context.

- 5,296
- 1
- 39
- 57

- 20,530
- 5
- 56
- 88
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"