2

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:

  1. Located in C:/Users/username/OneDrive - Co Inc, which I presume was created for 4.1.1.
  2. 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):

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
James N
  • 315
  • 2
  • 9
  • I'm wondering if the Rstudio sessions are still using R 4.1.1? – IRTFM Dec 10 '22 at 00:48
  • What does `Sys.getenv("R_USER")` show you in RStudio? That's what the help page says `path.expand("~")` should be giving you. And does the source for `path.expand` look different in the two cases? – user2554330 Dec 10 '22 at 00:58
  • 2
    BTW, some of the links you posted are to really old articles. I wouldn't expect R and RStudio to be the same today as they were 6 or 7 years ago. – user2554330 Dec 10 '22 at 01:02
  • @user2554330, R returns `C:/Users/username/OneDrive - Co Inc/Documents` with 'Sys.getenv("R_USER")`. I'm not sure what you mean by the second question; how do I check the source for `path.expand`. – James N Dec 10 '22 at 02:52
  • @IRTFM, I'm alternating the R version in RStudio (using Global Options). It's still working fine with 4.1.1; the issues are when I attempt to run RStudio with 4.2.2. – James N Dec 10 '22 at 02:55
  • @user2554330, yes, they are old articles, which reflects that there aren't many. I found, for instance, that I overlooked the references to using `setwd()` as I work entirely in projects. I'm very grateful for your help with this. – James N Dec 10 '22 at 02:58
  • @JamesN. I don't think you are addressing my question. What happens when you start up a session. What is the version of R that "appears" on startup? Or, if you are deep in a session and the stupid, brain-dead Rstudio limitation on how much history and output is retained is in effect, what is the output of `R.version`? – IRTFM Dec 10 '22 at 03:03
  • Thanks @IRTFM. I was going off the RStudio startup console message. Here's the output of 'R.version' (thanks - I didn't know that command) ``'platform x86_64-w64-mingw32 arch x86_64 os mingw32 crt ucrt system x86_64, mingw32 status major 4 minor 2.2 year 2022 month 10 day 31 svn rev 83211 language R version.string R version 4.2.2 (2022-10-31 ucrt) nickname Innocent and Trusting``` Each time I run the commands in my OP, I'm running them as soon as I open RStudio. – James N Dec 10 '22 at 05:33
  • @JamesN, to see the source for `path.expand` just type that at the console. The standard R source is very short, just a call to `.Internal(path.expand(path))`. – user2554330 Dec 10 '22 at 08:07
  • Thanks @user2554330. The only difference between opening RStudio with R version 4.1.1 and 4.2.2 is the bytecode. Version 4.2.2's response is: > path.expand function (path) .Internal(path.expand(path)) whereas version 4.1.1 response is > path.expand function (path) .Internal(path.expand(path)) I'm unfamiliar with what the bytecode means and whether this difference is significant. – James N Dec 10 '22 at 09:07

1 Answers1

2

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.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Oh dearie me! It was the "RUSER" typo in the .Renviron file for version 4.2.2. I don't know how many times I looked at that line! Thank you @user2554330. I'm feeling a bit embarrassed for this mistake. Thank you, again, for your persistence! – James N Dec 10 '22 at 09:55