Check if R is running in RStudio

前端 未结 9 671
小鲜肉
小鲜肉 2020-12-09 01:46

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

相关标签:
9条回答
  • 2020-12-09 02:09

    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.

    0 讨论(0)
  • 2020-12-09 02: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 .libPath() 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> 
    
    0 讨论(0)
  • 2020-12-09 02:17

    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/

    0 讨论(0)
  • 2020-12-09 02:17

    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"

    0 讨论(0)
  • 2020-12-09 02:18

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

    0 讨论(0)
  • 2020-12-09 02: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.

    0 讨论(0)
提交回复
热议问题