13

Currently:

path.expand("~")

Gives:

"C:\\Users\\trinker\\Documents"

I want it to yield:

"C:\\Users\\trinker"

The directory for the windows command prompt is C:\Users\trinker. This indicates that this is my windows home directory.

?path.expand tells me to look at the rw-FAQ (LINK). This gives information above my cognitive ability. I decided to try to experiment as below:

> Sys.getenv("R_USER")
[1] "C:\\Users\\trinker\\Documents"
> normalizePath("~")
[1] "C:\\Users\\trinker\\Documents"

> Sys.getenv("R_USER") <- "C:\\Users\\trinker"
Error in Sys.getenv("R_USER") <- "C:\\Users\\trinker" : 
  target of assignment expands to non-language object
> normalizePath("~") <- "C:\\Users\\trinker"
Error in normalizePath("~") <- "C:\\Users\\trinker" : 
  target of assignment expands to non-language object 

I saw:

Sys.setenv(...)
Sys.unsetenv(x)

But got scared I was messing with things I ought not be blindly messing with and decided to ask for guidance.

So again I would like to have ~ mean C:\\Users\\trinker\\ again (this was the default for my last PC) not the C:\\Users\\trinker\\Documents it is now.

Dason
  • 60,663
  • 9
  • 131
  • 148
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519

3 Answers3

12

To persistently reset the directory that "~" resolves to for all users, put the following in the file Renviron.site, located in $RHOME/etc/Renviron.site:

R_USER="C:/Users/trinker"

(If the file is not already there, you can just create it yourself.)


If a computer supports multiple R users, and each wants to set their own R_USER location, each can put the following in their own ".Rprofile" file:

Sys.setenv(R_USER = "C:/Users/trinker")

".Rprofile" is looked for in the user's home directory, which is returned by typing Sys.getenv("HOME"). See ?Startup and the R for Windows FAQ for more details.

(Thanks to @Dason for pointing out the .Rprofile option.)

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • @GSee -- Thanks. I'd simply mis-typed, and you caught it just as I did. – Josh O'Brien Jun 12 '12 at 21:08
  • 5
    Putting it in Renviron.site would affect all users on the computer right? Although I'm fairly sure Tyler is the only one using R on his computer this still seems less than adequate. Would adding a Sys.setenv call to .Rprofile be better? `Sys.setenv(R_USER = "C:/Users/trinker")` inside of .First should do it. – Dason Jun 12 '12 at 21:26
  • @Dason -- Yes, that's the better solution if you've got several R users on a single computer. Thanks for pointing that out. – Josh O'Brien Jun 12 '12 at 21:39
  • 1
    The first option did not work for me while the .Rprofile did. I assume this is because I have an .Rprofile I use and R uses this from my wd before searching for Renviron.site. +1 Dasona and Josh. – Tyler Rinker Jun 12 '12 at 23:40
  • @TylerRinker -- Or at least something like that. `?Startup` indicates that `Renviron.site` is read before any `.Rprofile` files (so settings in `.Rprofile` can overwrite settings from `Renviron.site`. In any case, glad this worked for you. – Josh O'Brien Jun 13 '12 at 00:00
0

Another option is to update the 'target' string in the shortcut you use to launch a new R session. To to that, right click the icon, and select "Properties" from the context menu. Select the "Shortcut" tab of the dialog box, and add one the following name/value pairs to the target field.

R_USER="c:\my\favorite\folder"
R_USER="%USERPROFILE%"

Now path.expand("~") will expand to c:\my\favorite\folder or to c:\users\my name\.

A couple of notes.

  • This will also change where R looks for user files such as .Rprofile,Rconsole, and user package library.
  • If you've pinned R to your taskbar, you need to right click the task bar Icon to find the shortcut icon which launches R from the taskbar (pictured below):

enter image description here

Jthorpe
  • 9,756
  • 2
  • 49
  • 64
0

Our sysadmin has setup the system in such a way that path.expand("~") or normalizePath("~") always lead to a mapped drive (\...). Creating a ".Renviron " file within the folder pointed by path.expand("~") with following content solved the problem for me :)

R_USER="C:/Users/animeshs"
HOME="C:/Users/animeshs"

So now

path.expand("~")

[1] "C:/Users/animeshs"

Ani
  • 59
  • 4