5

When working on a unix system, the ~ expands my directory to my unix home. When on my windows computer, I would like ~ to expand to the drive that is mapped and points to the Unix home. I am using RStudio for coding on the windows computer and it expands the ~ to something that isn't helpful and I am having trouble changing it. I have played with the environment variables and PATH but cannot get it to point to what I want. Any ideas?

UPDATE:

Per Josh's answer. Changing the R_USER environment variable in windows, prior to starting RStudio yields on startup:

Error: invalid version specification ‘NA’
In addition: Warning message:
In utils:::packageDescription(packageName, fields = "Version") :
  no package 'rstudio' was found

Changing it manually every time after RStudio startup is possible using this answer but I would like to avoid doing that.

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
Alex
  • 19,533
  • 37
  • 126
  • 195
  • Setting `R_USER = "C:/"` in my `~/.Renviron` file works just fine. (i.e. with that set, launching Rstudio and then typing `path.expand("~")` yields `"C:/"`...) – Josh O'Brien Nov 13 '14 at 21:41
  • interesting: let me try that. i just added an `R_USER` variable to the system through windows directly and launched RStudio. that resulted in the error above – Alex Nov 13 '14 at 21:45
  • @JoshO'Brien: your proposed solution results in the error above – Alex Nov 13 '14 at 22:10
  • ... on your box, not on mine ;). I probably won't be able to help any further, since I can't recreate the issues you're having on my own machine. It does look to be related, though, to the location of your Rstudio package, which seems to be missed once `R_USER` is reset. Maybe try setting `R_USER` from within your `.Rprofile`, *after* a line that explicitly loads Rstudio? – Josh O'Brien Nov 13 '14 at 22:15
  • ok will give that a shot. it's odd that RStudio would behave differently unless our versions are very different. i am on the newest version. is this the one you are using? – Alex Nov 13 '14 at 22:27
  • I've got version `0.98.994`, which was current a couple of months ago, but not any longer. (Looks like they're currently at `0.98.1091`). – Josh O'Brien Nov 13 '14 at 22:46

4 Answers4

3

To change the value of ~ from its default, you need to set R_USER before your first call to path.expand() et al. (This is documented in ?path.expand.)

Try this:

 ## R
 Sys.getenv("R_USER")
 # [1] "C:\\Users\\Josh"
 Sys.setenv(R_USER="C://")
 path.expand("~")
 # [1] "C://"

To set the starting value of "R_USER" for all of your R/Rstudio sessions, just add a line like the following to your ~/.Renviron or $R_HOME/etc/Renviron.site or wherever you prefer. (As always, see ?Startup for the full set of options.):

R_USER = "C:/"
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • Is that equivalent to setting the environment variable before starting R/RStudio? – Matthew Lundberg Nov 13 '14 at 21:29
  • @MatthewLundberg -- Yes, if I understand you correctly. (See my edit for one example of how to do that.) – Josh O'Brien Nov 13 '14 at 21:51
  • @JoshO'Brien: i haven't tried it yet but could you explain why this is different than simply setting the windows env variable? is it the order of operations? ie RStudio loads the Renviron.site last so that its able to load all of the other stuff it needs first? – Alex Nov 13 '14 at 21:55
  • This does not work: I get the same issue as pointed to in the update – Alex Nov 13 '14 at 22:06
  • The `Renviron.site` file should be in `$R_HOME/etc/` not `$R_HOME`. SO won't allow edit because it's less than 6 characters. – seasmith Nov 19 '17 at 19:47
  • @seasmith Good catch -- now fixed. Thanks for alerting me! – Josh O'Brien Nov 20 '17 at 02:01
  • I think changing value of R_USER will not work, but change the value of HOME will work~ it is dangerous to change it for the system, the safe way is to change inside ~/.Renviron – cloudscomputes Feb 07 '18 at 06:13
0

What is your .libPaths() after starting RStudio? I note that if I start RStudio on Windows without setting R_USER, I get:

> .libPaths()
[1] "C:/Users/Kevin/Documents/R/win-library/3.1"
[2] "C:/Program Files/R/R-3.1.0/library

The first library path is automatically generated by RStudio, and it installs rstudio and manipulate packages there automatically on startup (because it may not have permissions to write to the default, 'system' library).

However, if R_USER is set, then that library path is not set, and this appears to cause problems with the installation of these packages.

I believe you should be able to work around this by also setting the R_LIBS_USER environment variable accordingly -- it should be set to somewhere that RStudio has permissions to write to. See ?libPaths for more information on how that might be appropriately set.

Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88
0

Consider using ${} in Renviron.site to call Windows environment variables. To make R_USER point to each user's document folder, you can put the following in Renviron.site:

R_USER=C:/Users/${USERNAME}/Documents
user3799203
  • 444
  • 1
  • 4
  • 15
0

I have a similar situation and for my needs it was easiest to redefine the HOME-parameter with the line below:

Sys.setenv(HOME="path/to/desired/tilde-folder/")
path.expand("~") # Check that it worked.

Update: Just realised that this solution does not appear to work on my Windows machine, only on a Linux server I was using.

Smerla
  • 174
  • 9