MichaĆ Bojanowski
2021-Oct-15 14:44 UTC
[Rd] Fwd: Using existing envars in Renviron on friendly Windows
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: 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
Duncan Murdoch
2021-Oct-15 16:19 UTC
[Rd] Fwd: Using existing envars in Renviron on friendly Windows
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 at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Ivan Krylov
2021-Oct-15 16:37 UTC
[Rd] Fwd: Using existing envars in Renviron on friendly Windows
On Fri, 15 Oct 2021 16:44:28 +0200 Micha? Bojanowski <michal2992 at gmail.com> wrote:> AVAR=${APPDATA}/foo/bar > > Which is a documented way of referring to existing environment > variables. Now, with that in R I'm getting: > > Sys.getenv("APPDATA") # That works OK > [1] "C:\\Users\\mbojanowski\\AppData\\Roaming" > > so OK, but: > > Sys.getenv("AVAR") > [1] "C:UsersmbojanowskiAppDataRoaming/foo/bar"Hmm, that -- Best regards, Ivan
Ivan Krylov
2021-Oct-15 16:40 UTC
[Rd] Fwd: Using existing envars in Renviron on friendly Windows
Sorry for the noise! I wasn't supposed to send my previous message. On Fri, 15 Oct 2021 16:44:28 +0200 Micha? Bojanowski <michal2992 at gmail.com> wrote:> AVAR=${APPDATA}/foo/bar > > Which is a documented way of referring to existing environment > variables. Now, with that in R I'm getting: > > Sys.getenv("APPDATA") # That works OK > [1] "C:\\Users\\mbojanowski\\AppData\\Roaming" > > so OK, but: > > Sys.getenv("AVAR") > [1] "C:UsersmbojanowskiAppDataRoaming/foo/bar"Hmm, a function called by readRenviron does seem to remove backslashes, but not if they are encountered inside quotes: https://github.com/r-devel/r-svn/blob/3f8b75857fb1397f9f3ceab6c75554e1a5386adc/src/main/Renviron.c#L149 Would AVAR="${APPDATA}"/foo/bar work? -- Best regards, Ivan