Skip to content
Prev 60187 / 63424 Next

Using existing envars in Renviron on friendly Windows

On 15/10/2021 10:44 a.m., Micha? Bojanowski wrote:
I think in your example, AVAR would be set using

AVAR=C:\Users\mbojanowski\AppData\Roaming/foo/bar

When I export that value in a bash shell (on a Mac, not Windows), I get 
the same thing as you saw:

$ printenv AVAR
C:UsersmbojanowskiAppDataRoaming/foo/bar

Here R was not involved at all, this is the shell eating the backslashes.

So I suppose R is following the same rules as bash (or maybe getting 
bash or sh to handle .Renviron).  Those rules are that the single 
backslashes are treated as escapes, and so they are dropped and the 
following character is preserved: 
https://www.gnu.org/software/bash/manual/html_node/Escape-Character.html .

I think you don't have a lot of choice here:  if you don't have control 
over how an environment variable is being set, then don't try to use it 
in an expansion in .Renviron.  If you do have control, then avoid using 
backslashes.

So this would be fine:

APPDATA=C:/Users/mbojanowski/AppData/Roaming
AVAR=${APPDATA}/foo/bar

but your APPDATA setting needs to be handled in some other way, e.g. in 
.Rprofile instead of .Renviron.

Duncan Murdoch