# [Rd] Fwd: Using existing envars in Renviron on friendly Windows

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Fri Oct 15 18:19:20 CEST 2021

On 15/10/2021 10:44 a.m., Michał Bojanowski wrote:
> Dear colleagues,
>
> I would be grateful if somebody could explain and perhaps help work
> around the following.
>
> I have .Renviron with, say:
>
> AVAR=\${APPDATA}/foo/bar
>
> Which is a documented way of referring to existing environment
> variables. Now, with that in R I'm getting:

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

>
> Sys.getenv("APPDATA")    # That works OK
> [1] "C:\\Users\\mbojanowski\\AppData\\Roaming"
>
> so OK, but:
>
> Sys.getenv("AVAR")
> [1] "C:UsersmbojanowskiAppDataRoaming/foo/bar"
>
> So all the (back)slashes are gone from APPDATA.
>
> Does processing \${} removes the backslashes? I could not find anything
> on that in R Windows FAQ nor on the web.
> Thanks in advance!
>
> Michal
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>