Check if R is running in RStudio

前端 未结 9 672
小鲜肉
小鲜肉 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:22

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

    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.

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

    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 ...
    
    0 讨论(0)
提交回复
热议问题