[Rd] Evaluate values in `Sys.setenv`

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Fri Aug 28 14:34:10 CEST 2020


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 using 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 a
> terminal
> 
> ```
>> 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 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
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com



More information about the R-devel mailing list