Setting PATH environment variable in Rprofile

This is related to:
https://community.rstudio.com/t/how-to-make-rstudio-use-the-right-system-path/119880/4

I want to use an executable installed with Homebrew (macOS 13.5.2), but the necessary path (/opt/homebrew/bin) is not in the PATH environment variable of an R Studio session. I can add it within a session using a command like this:

Sys.setenv(PATH=paste("/opt/homebrew/bin", Sys.getenv("PATH"), sep=":"))

But I don't want to have to do this every time within a session.

I tried adding that line to my .Rprofile to apply it automatically in every session. This works in the R GUI, but not in RStudio - RStudio is doing something after loading my .Rprofile that completely overwrites any changes I make to the PATH environment variable there.

Is there another RStudio-specific configuration file where I can add paths to the PATH environment variable for system commands within the R console?

I've been using ~/.Renviron

R_INCLUDE_DIR=/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/include
R_LIBS_SITE=/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
R_LIBS_USER=/Users/ro/Library/R
RSTUDIO_PANDOC=/usr/local/bin/pandoc
RSTUDIO_QUARTO=/usr/local/bin/quarto

Last two entries are Officially Disapproved by my betters

None of those examples show how to modify the PATH environment variable, and especially how to prepend a path to the existing variable.

PATH="/opt/homebrew/bin:$PATH"

Does not work inside .Renviron: "$PATH" is interpreted as a literal string (with or without the quotes). Besides, RStudio overwrites the PATH environment variable with something entirely different, after loading .Renviron and .Rprofile. The same thing does not happen in the base R GUI.

More details here:

I must of been in the middle of one of my senior hours because I omitted the relevant line.

PATH=/Users/ro/.nvm/versions/node/v18.9.1/bin:/opt/homebrew/Cellar/python@3.10/3.10.9/bin:/Applications/CMake.app/Contents/bin:/opt/homebrew/bin:/Users/ro/.cargo/bin:/Applications/Julia-1.8.app/Contents/Resources/julia/bin/julia:/usr/local/go/bin:/Users/ro/.fig/bin:/usr/local/mysql-8.0.28-macos11-x86_64/bin:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/sbin:/Users/ro/.local/bin

Try with curly braces: "${PATH}"

Thanks @AlexisW - that works in R GUI, but not in RStudio, because:

RStudio overwrites the PATH environment variable with something entirely different, after loading .Renviron and .Rprofile. The same thing does not happen in the base R GUI.

It does not matter what I put in .Renviron or .Rprofile with respect to PATH in RStudio. :frowning:

Right, that was an answer to this part:

For RStudio, it's harder to tell, for example, on my Windows, it works.

The first thing would be to check if this .Renviron file is read at all, e.g. if you define a variable MYPATH='aaa' can you see it from inside RStudio, or is only PATH overwritten?

I would also check that RStudio is loading the right version of R (e.g. sometimes after installing R4.3.2 RStudio is still loading R4.3.1). That's particularly meaningful if you are using a .Renviron in R_HOME/etc, not if it's in your ~.

I would also check for a project-specific file. If you create a new project, does RStudio still not take into account your .Renviron?

An important note: R only reads a single .Renviron file, so if you have 3 files defined, 2 of them will be ignored.

1 Like

I haven't tested on Windows yet, but I will try to find some time to do so.

The first thing would be to check if this .Renviron file is read at all, e.g. if you define a variable `MYPATH='aaa'` can you see it from inside RStudio, or is only `PATH` overwritten?

Other environment variables can be set normally. It appears that only PATH is overwritten, as described in the GitHub issue I linked to earlier.

I would also check that RStudio is loading the right version of R (e.g. sometimes after installing R4.3.2 RStudio is still loading R4.3.1). That's particularly meaningful if you are using a .Renviron in R_HOME/etc, not if it's in your `~`.

I am using RStudio Version 2023.09.1+494 (2023.09.1+494) on Apple silicon macOS 13.5.2, which loads R v4.3.2. Looks ok to me.

I would also check for a project-specific file. If you create a new project, does RStudio still not take into account your .Renviron?

There is no project, nor a project-specific file. This happens with no project open, and the wd set to "~" (by default).

I can tell that the .Renviron and .Rprofile are read - other effects are persistent, including your suggestion of a custom MYPATH environment variable. Also (as described in my comment on the GitHub issue), If I add this line to the end of ~/.Rprofile:

cat(Sys.getenv("PATH"), "\n")

it shows the expected modified PATH in the console output at the start of a session:

/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin 

But once the session starts, the same command (Sys.getenv("PATH")) shows a completely different output, without any of the changes applied by .Renviron or .Rprofile, as far as the PATH environment variable is concerned. So it looks very much like RStudio is overwriting PATH with something else, after loading and running .Renviron and .Rprofile. I know that GUI applications inherit their environment variables (such as PATH) differently than shells in macOS, but the same thing does not happen in the base R GUI, and I'm wondering what R Studio is doing differently (and why?).

Thanks,
Jonathan

1 Like

I see.

One last suggestion: have you seen this topic, and its linked issue? In particular, is it better if you launch RStudio from the Terminal (instead of Finder)? Does the clever trick by msteinbaugh with later::later() show you the correct PATH?

1 Like
at_start <- Sys.getenv("PATH")
1+1
#> [1] 2
after_start <- Sys.getenv("PATH")
identical(at_start,after_start)
#> [1] TRUE
# .Renviron
# Pandoc and Quarto settings are not recommended by
# Posit sources
readLines(file.path(Sys.getenv("HOME"), ".Renviron"))
#> [1] "PATH=/Users/ro/.nvm/versions/node/v18.9.1/bin:/opt/homebrew/Cellar/python@3.10/3.10.9/bin:/Applications/CMake.app/Contents/bin:/opt/homebrew/bin:/Users/ro/.cargo/bin:/Applications/Julia-1.8.app/Contents/Resources/julia/bin/julia:/usr/local/go/bin:/Users/ro/.fig/bin:/usr/local/mysql-8.0.28-macos11-x86_64/bin:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/sbin:/Users/ro/.local/bin"
#> [2] "R_INCLUDE_DIR=/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/include"                                                                                                                                                                                                                                                                                                    
#> [3] "R_LIBS_SITE=/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library"                                                                                                                                                                                                                                                                                                      
#> [4] "R_LIBS_USER=/Users/ro/Library/R"                                                                                                                                                                                                                                                                                                                                                       
#> [5] "RSTUDIO_PANDOC=/usr/local/bin/pandoc"                                                                                                                                                                                                                                                                                                                                                  
#> [6] "RSTUDIO_QUARTO=/usr/local/bin/quarto"                                                                                                                                                                                                                                                                                                                                                  
#> [7] ""
# .Rprofile
# In practice, I use update.packages("ask = FALSE") normally
readLines(file.path(Sys.getenv("HOME"), ".Rprofile"))
#>  [1] "options(repos=structure(c(CRAN=\"https://cran.rstudio.com/\"), install.packages.check.source = \"both\"))"
#>  [2] "utils::assignInNamespace("                                                                                
#>  [3] "  \"q\", "                                                                                                
#>  [4] "  function(save = \"no\", status = 0, runLast = TRUE) "                                                   
#>  [5] "  {"                                                                                                      
#>  [6] "    .Internal(quit(save, status, runLast))"                                                               
#>  [7] "  }, "                                                                                                    
#>  [8] "  \"base\""                                                                                               
#>  [9] ")"                                                                                                        
#> [10] ""                                                                                                         
#> [11] ""
sessionInfo()
#> R version 4.3.2 (2023-10-31)
#> Platform: aarch64-apple-darwin20 (64-bit)
#> Running under: macOS Sonoma 14.1.1
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> time zone: America/Los_Angeles
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] styler_1.10.2     digest_0.6.33     fastmap_1.1.1     xfun_0.41        
#>  [5] magrittr_2.0.3    glue_1.6.2        R.utils_2.12.2    knitr_1.45       
#>  [9] htmltools_0.5.7   rmarkdown_2.25    lifecycle_1.0.3   cli_3.6.1        
#> [13] R.methodsS3_1.8.2 vctrs_0.6.4       reprex_2.0.2      withr_2.5.2      
#> [17] compiler_4.3.2    R.oo_1.25.0       R.cache_0.16.0    purrr_1.0.2      
#> [21] rstudioapi_0.15.0 tools_4.3.2       evaluate_0.23     yaml_2.3.7       
#> [25] rlang_1.1.1       fs_1.6.3

Created on 2023-11-22 with reprex v2.0.2

Thanks for the links. None of the so-called workarounds worked for me, except this:

launch RStudio from your Terminal (open /Applications/RStudio.app) the PATH variable should be correct when running Sys.getenv("PATH") in your RStudio console.

This part still does not work for me:

Again, you can save that PATH to your ~/.Renviron file if you don't want to have to remember to launch from the terminal each time.

Strangely, when I launch RStudio from the Terminal, the PATH modifications made in .Rprofile are applied and not overwritten.

I tested on my Windows 10 machine with the most recent versions of RStudio & R, and I do not see the same behaviour: RStudio on that platform respects the modifications in .Rprofile. So I only see it in RStudio on Apple Silicon (in my case, macOS 13.5.2).

@technocrat - are you running RStudio on a different platform where the .Renviron PATH persists in RStudio?

At this point, I am thinking of opening another issue and linking to the existing ones. I don't know if it's related, but my issue is not exactly the same.

Amended by previous answer to give complete setup info on my Sonoma M3

@technocrat : what versions of R, RStudio, and macOS are you using together, where it works for you? It does not work for me with:

RStudio Version 2023.09.1+494 (2023.09.1+494)
R v4.3.2
macOS 13.5.2 (Apple Silicon M1 chip)

Here's my setup:

> sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.1.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Los_Angeles
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):


I have `rosetta` installed on the M3 configuration shown above. I can't think of anything else I've had to tinker with lately. The `quarto` and `pandoc` settings are nonstandard.

!cat
cat .Rprofile
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: .Rprofile
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ options(repos=structure(c(CRAN="https://cran.rstudio.com/"), install.packages.check.source = "both"))
   2   │ utils::assignInNamespace(
   3   │   "q",
   4   │   function(save = "no", status = 0, runLast = TRUE)
   5   │   {
   6   │     .Internal(quit(save, status, runLast))
   7   │   },
   8   │   "base"
   9   │ )
  10   │
  11   │
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
cat .Renviron
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: .Renviron
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ PATH=/Users/ro/.nvm/versions/node/v18.9.1/bin:/opt/homebrew/Cellar/python@3.10/3.10.9/bin:/Applications/CMake.app/Contents/bin:/opt/homebr
       │ ew/bin:/Users/ro/.cargo/bin:/Applications/Julia-1.8.app/Contents/Resources/julia/bin/julia:/usr/local/go/bin:/Users/ro/.fig/bin:/usr/local
       │ /mysql-8.0.28-macos11-x86_64/bin:/bin:/usr/bin:/usr/local/bin:/usr/sbin:/sbin:/Users/ro/.local/bin
   2   │ R_INCLUDE_DIR=/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/include
   3   │ R_LIBS_SITE=/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
   4   │ R_LIBS_USER=/Users/ro/Library/R
   5   │ RSTUDIO_PANDOC=/usr/local/bin/pandoc
   6   │ RSTUDIO_QUARTO=/usr/local/bin/quarto
   7   │
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Shall I assume you are running the latest version of RStudio? So far the only major difference I'm seeing is macOS 14 vs my macOS 13.

I'm on Version 2023.09.1+494 (2023.09.1+494), having learned to be cautious to use the latest version or dailies.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.