Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

r - Change path.expand location (Win 7)

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:Usersrinker. 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...