Use other environment variable in Renviron

Hi all,

is there a way to use existing environment variables in Renviron?

I'm trying to set an environment variable in that is made of another system environment variable.

What's the correct syntax to do like this?

# (or .Renviron)
R_TEST = %USERPROFILE%/test/folder

This example is not working:

> Sys.getenv("R_TEST")
[1] "%USERPROFILE%/test/folder"

> Sys.getenv("USERPROFILE")
[1] "C:\\Users\\xxxx"

Is this possible at all?

Thank you!

1 Like

The help for ?Startup described the mechanism for doing this (in the Details section):

Lines in a site or user environment file should be either comment lines starting with #, or lines of the form name=value. The latter sets the environmental variable name to value, overriding an existing value. If value contains an expression of the form ${foo-bar}, the value is that of the environmental variable foo if that is set, otherwise bar. For ${foo:-bar}, the value is that of foo if that is set to a non-empty value, otherwise bar. (If it is of the form ${foo}, the default is "".) This construction can be nested, so bar can be of the same form (as in ${foo-${bar-blah}}). Note that the braces are essential: for example $HOME will not be interpreted.

I haven't tested this, but IIUC, this means you can use:

R_TEST = ${USERPROFILE}/test/folder

Thank you, @andrie. I should have consulted the internal help before browsing the web for this.

This topic was automatically closed 7 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.