I have just updated my R version from 4.1.1 to 4.2.2. My problem is that RStudio (2022.07.2) is not appearing to process the .Renviron file correctly.
In other words, I get the following in the console:
> path.expand("~")
[1] "C:/Users/username/Documents"
> Sys.getenv("home")
[1] "C:/Users/username/OneDrive - Co Inc/Documents"
I suspect that a part of the problem was that version 4.1.1 was moved to a OneDrive folder by my company's IT department whereas 4.2.2 is not on a OneDrive folder.
The problem with RStudio not recognising ~
is that I refer to ~
numerous times in my existing code to point to the OneDrive directory and its numerous subfolders.
One thing that is odd is that I get the following when I run R directly.
> path.expand("~")
[1] "C:/Users/username/OneDrive - Co Inc/Documents"
I have searched my c: drive for .Renviron files and can only find 2:
- Located in
C:/Users/username/OneDrive - Co Inc
, which I presume was created for 4.1.1. - Located in
C:/Users/username
, which I presume was created for 4.2.2.
Both of these version include the following lines:
RUSER=C:/Users/username/OneDrive - Co Inc/Documents
HOME=C:/Users/username/OneDrive - Co Inc/Documents
I've also checked both versions' copy of the .Rprofile files. Both include these lines:
Sys.setenv(HOME="C:/Users/username/OneDrive - Co Inc/Documents")
Sys.setenv(R_USER="C:/Users/username/OneDrive - Co Inc/Documents")
A workaround is to start every R script with the Sys.setenv(HOME="C:/Users/username/OneDrive - Co Inc/Documents")
, though that would seem like it defeats the purpose of .Renviron.
I've spent literally hours googling to find a solution, which I suspect is related to migrating from a version inside OneDrive to one outside of OneDrive. Pages that I've studied include (but not limited to):
- https://support.posit.co/hc/en-us/articles/360047157094-Managing-R-with-Rprofile-Renviron-Rprofile-site-Renviron-site-rsession-conf-and-repos-conf
- Cannot locate .Rprofile file
- Tilde expansion in RStudio on Windows
- RStudio home directory doesn't exist
- Difference between tilde expansion in RStudio and R
- Tilde "~" expression not functioning in "R"?
CodePudding user response:
The documents say that on Windows, path.expand("~")
will give the contents of the R_USER
environment variable. The Windows FAQ also mentions that R looks at the HOME
environment variable if R_USER
is not set. If you are getting different results in RStudio vs. R, it must be because of some difference in those environment variables. The way to see their settings is to start a new session and run
Sys.getenv("HOME")
Sys.getenv("R_USER")
If you don't see any differences in the results between the two systems, then something very strange is going on, but I think you will. So the next step is to figure out where the difference came from.
You mention two .Renviron
files containing settings for RUSER
and HOME
. If that's not a typo on the first one, then those files aren't setting R_USER
(it's not the same as RUSER
) and HOME
looks the same,
so you need to look somewhere else for where R_USER
is coming from.
There are several ways to set environment variables in Windows. I've never used Windows 11 so it's possible things have changed, but the basic methods in previous versions are these:
- Set system-wide or user-specific environment variables in the "Settings".
- Set session-specific environment variables in a command shell.
- (Maybe, I forget...) Set shortcut-specific environment variables.
In R, there are also several different places to look for .Renviron
or .Rprofile
or one of the system versions of those files: see the ?Startup
help page for the gory details. Somewhere in there you should find the bad setting that RStudio is using.