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.
paste(path.expand("~/mypath"), Sys.getenv("PATH"), sep=":")
[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
On 28 Aug 2020, at 11:00 , Jeff King <szwjf08 at gmail.com> wrote:
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
export PATH=~/mypath:$PATH
echo $PATH
/Users/jeff/mypath:...
```
However, this style of assignment is not allowed in `Sys.setenv`, it
treats its argument as a literal string, so
```
Sys.setenv(PATH= " ~/mypath:$PATH")
Sys.getenv("PATH")
[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
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
[[alternative HTML version deleted]]