Alex
Alex

Reputation: 19843

Tilde expansion in RStudio on Windows

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.

Upvotes: 5

Views: 3834

Answers (4)

Smerla
Smerla

Reputation: 231

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.

Upvotes: 0

user3799203
user3799203

Reputation: 514

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

Upvotes: 0

Josh O'Brien
Josh O'Brien

Reputation: 162411

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:/"

Upvotes: 4

Kevin Ushey
Kevin Ushey

Reputation: 21315

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.

Upvotes: 1

Related Questions