Skip to content

Evaluate values in `Sys.setenv`

3 messages · Jiefei Wang, Peter Dalgaard

#
Hi all,

I would like to know if there is any way to evaluate the values in
`Sys.setenv` before setting the environment variables. For example, if we
want to add a path to the environment variable `PATH`, we can do this in a
terminal

```
/Users/jeff/mypath:...
```

However, this style of assignment is not allowed in  `Sys.setenv`, it
treats its argument as a literal string, so

```
[1] " ~/mypath:$PATH "
```
Note that both the symbol `~` and the variable `$PATH` are not expanded.
While we can manually evaluate the home symbol `~`  and PATH using
`Sys.getenv`, the code seems not quite neat. I would like to suggest adding
a parameter in ` Sys.setenv` to make the function more convenient(e.g.
Sys.setenv(..., fixed = TRUE)) if no existing function in base R can do
them in one line.

Best,
Jiefei
#
It is the shell that does the ~ and $ expansions, and Sys.setenv() doesn't go via the shell, so you cannot expect it to understand the shell metacharacters.  Instead, you need to do the corresponding computations in R, e.g.
[1] "/Users/pd/mypath:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/gfortran/bin:/usr/local/clang8/bin:/Library/TeX/texbin:/opt/X11/bin"

which you can then pass to Sys.setenv().

-pd

  
    
1 day later
#
Thanks for your explanation. The function `path.expand` is very helpful!

Best,
Jiefei
On Fri, Aug 28, 2020 at 8:34 AM peter dalgaard <pdalgd at gmail.com> wrote: