7

I have an R script that runs perfectly when run from Rstudio. It uses an .Renviron file to provide environment variables which are visible in Sys.getenv() (so I know it works).

However, when I run the exact same script via powershell, the R script mysteriously doesn't find those environment variables (I confirmed this by print(Sys.getenv()) - the environment variables provided through .Renviron are nowhere to be seen.

Question

Why aren't the .Renviron variables being found when the R script is run from powershell (as opposed to when the script is run in RStudiom, where everything works)

What I've tried

The .Renviron file is currently saved in "C:/Users/Administrator/Documents/.Renviron"

Inspecting normalizePath(Sys.getenv('R_HOME')) returns "C:\\Program Files\\R\\R-3.6.0", so I also tried putting .Renviron there. No luck

Based on this I also tried Sys.getenv("C:/Users/Administrator/Documents/.Renviron"), but again no luck

Community
  • 1
  • 1
stevec
  • 41,291
  • 27
  • 223
  • 311
  • 1
    Yep - can also confirm that with WIN 10 `.Renviron` located in R_USER directory (ie `C:\\Users\\Username`) is not being read when starting R from the command line. – GWD May 18 '21 at 21:40

3 Answers3

5

I had the same problem: .Renviron files were being completely ignored on Windows.
I thought that what finally worked was reinstalling R (I had R 3.6, installed R 4.0).

But it turns out I was wrong. The magic sauce is adding --cd-to-userdocs to the R.exe or Rscript.exe command. I couldn't find documentation for this option anywhere, and it doesn't show up in command line parameters help, but it works.

Zbyl
  • 2,130
  • 1
  • 17
  • 26
  • 1
    This workaround is "ok" but I often do not want to change to the `userdocs` (`My Documents`) folder when starting R from the command line - so not really a satisfying situation, is it. – GWD May 18 '21 at 21:43
3

This is what worked for me

path_to_Renviron_file <- "C:/Users/Administrator/Documents/.Renviron"
readRenviron(path_to_Renviron_file) 
stevec
  • 41,291
  • 27
  • 223
  • 311
  • This is incorrect, R *does* load the `~/.REnviron` file, and the file shouldn’t be loaded explicitly. – Konrad Rudolph Jul 10 '19 at 12:50
  • @KonradRudolph the line fixed the issue. If not for the reason above, I am not sure why? Do you know? Can you test it on your machine, I am not sure why it would be any different for you. I am running windows server 2019 on EC2 (`Microsoft Windows Server 2019 Base - ami-0bbdf9279190cdd33`) – stevec Jul 10 '19 at 12:52
  • See my answer. The problem with such workaround fixes is that they don’t address the actual problem and may thus break in unexpected ways once something changes. And using hard-coded absolute paths is a big no-no anyway — part of the reason is explained here: https://www.tidyverse.org/articles/2017/12/workflow-vs-script/ – Konrad Rudolph Jul 10 '19 at 12:57
  • 8
    I think the real problem is that there is no good way to discover what R is really doing. There is no explicit environment path variable to inspect to see where it is actually looking and no way to get feedback from R to allow one to have it display which files it is or is not reading. – G. Grothendieck Jul 10 '19 at 13:21
  • 1
    @G.Grothendieck This is a very good point. Debugging these issues is a real pain. – Konrad Rudolph Jul 10 '19 at 13:40
  • Agree with @G.Grothendieck. – Sanjid Apr 20 '21 at 22:14
  • I have come across 3 difference locations on Windows `.REnviron` could get read from: --- 1. `C:/Users/Username` if script run from Git Bash 2. `C:/Users/Username/Documents` if script run from RStudio 3. `C:/Windows/System32` if script run with RStudio TaskscheduleR addin – Sanjid Apr 20 '21 at 22:21
1

RScript performs the same initialisation as normal R, unless this is specifically disabled with command line options (--no-environ). The .Renviron and .Rprofile files are searched for in different locations, in descending order of priority:

  1. In the current working directory
  2. In the user’s home directory
  3. In the R home directory (R_HOME)

The documentation gives more details.

Since you’ve tried to change the file in locations (2) and (3) without success, it stands to reason that you have another .Renviron file in your current working directory from which you are launching Rscript.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • I like the idea of best practice (i.e. avoiding absolute paths). But I am sure there is no extra `.Renviron` file(s) - just one. (the `.R` script is definitely being run with `Rscript "C:\Users\Administrator\Desktop\myscript.R` – stevec Jul 10 '19 at 13:10
  • Also, if I put `print(Sys.getenv())` either side of the call to `readRenviron()` line, I can see the two list of environment variables. The first doesn't have the ones declared in `.Renviron`, and the second one does. I don't think the duplicate `.Renviron` file is the cause.. – stevec Jul 10 '19 at 13:13
  • @user5783745 *Where* are you running this command from, though? What does `cd` print? Is there an .Renviron file there? And what is `Rscript`? Is it maybe an alias to the *actual* `Rscript` binary, which sets the `--no-environ` option? If neither of these is the case I suggest reinstalling R because your installation appears to be broken. – Konrad Rudolph Jul 10 '19 at 13:13
  • @ I have a shortcut in `C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup` which points to a directory on the desktop with only a couple of files (.git and the rscript - 100% sure there is no `.Renviron` file there). Also, I have hidden files ticked (just to rule out another possible cause) – stevec Jul 10 '19 at 13:16
  • `Rscript` does indeed point to the actual `Rscript` binary. Are you saying it ignores `.Renviron` by default? – stevec Jul 10 '19 at 13:25
  • @user5783745 No, on the contrary. I’m saying that it *doesn’t* ignore `.Renviron` unless told to explicitly. Does the shortcut have any command line options saved in its configuration? – Konrad Rudolph Jul 10 '19 at 13:39
  • Okay cool. No, it's basically a fresh install of windows server 2019. I haven't tinkered with anything, just installed R/Rstudio, put an r script on there (from a cloned repo), made an `.Renviron` file and that's it. – stevec Jul 10 '19 at 13:46
  • 4
    Does this issue have a solution? I am experiencing the same problem on Windows 10 with a fresh install of R 4.02. – JBJ Aug 22 '20 at 12:50
  • 2
    I'm experiencing the same with R 4.1 on Windows 10 when trying to use Rscript from the command line. – s_baldur Jun 04 '21 at 10:33