简体   繁体   中英

Making R installation self-contained/user-independent

I'm trying to get R to ignore c:\\users\\name\\documents and be completely self-contained/portable

Here's my directory structure:

.../R/R-2.1.2.2/...
.../R/r_user/
.../R/libs_site/

I updated my Rprofile.site as follows:

# Set the working directory
setwd( file.path( R.home() , ".." , "r_user" ) )

# set the home directory
Sys.setenv(HOME=file.path( R.home() , ".." , "r_user" )  )

# Set the site library folder
.Library.site = file.path( R.home() , ".." , "libs_site" )

when R launches, I run .libPaths() but I still see c:/users/...

So perhaps R isn't updating per the Sys.setenv above? Solutions?

In my experience, creating a file named Renviron.site did not work on all my Windows machines, whereas naming the same file .Renviron instead did work everywhere. Not sure why. So if you're having difficulty with the above suggestions, then try .Renviron for a name.

Please do not hesitate to critically comment about my suggestion, because while I am describing what has worked for me, it may have difficulties I am not aware of.

In my experience, the following has worked:

Both Windows and Linux Platform:

Setting R paths

Find out the default paths: .libPaths()

Set the path temporarily (in a R script)

.libPaths( "F:/Rlib" )

where F could be the letter associated with, say, a USB drive.

Query paths (both Windows and Linux):

Sys.getenv('R_LIBS_USER')
Sys.getenv('R_LIBS')
Sys.getenv('R_USER')
Sys.getenv('R_DOC_DIR')
Sys.getenv('HOME')

Try also:

normalizePath("~")

Try also:

getwd()
setwd(dir)

getwd returns an absolute filepath representing the current working directory of the R process

setwd(dir) is used to set the working directory to dir.

Windows (Tested on: 7x64)

Create an environment file named .Renviron place it in the working directory or home directory:

"C:/Users/username/Documents"

Some users have reported that the .Renviron file needed to be in "c:/users/username/" instead. If you're not sure where to place it, save the desktop history and see where the .Rhistory file is located. Then place your .Renviron file in the same location. To save history savehistory()

# Windows .Renviron file:
R_LIBS_USER="C:/R/library"
R_USER="C:/R"
R_DOC_DIR="C:/R"
HOME="C:"

Set global PATH My Computer / Properties / Advanced System Settings / Environment Variables --> user variables --> Path --> Edit c:\\R;c:\\R\\library;

Linux (Tested on kUbuntu 12.10)

Create an environment file named Renviron.site place it in:

/etc/R/

Query the paths to check that your system is reading the Renviron.site file.

# Linux Renviron.site file:
R_LIBS_USER="~/R/library"
R_USER="~/R"
R_DOC_DIR="~/R" 
#HOME="/home" # may not be needed

Remark: afaik the file is read from bottom to top, so HOME is defined at the bottom. In my setup ~ is correctly assigned to /home/ so I omit that last line anyway.

If you use RStudio, you may also want to add an rsession.conf file in the RStudio program directory. The following has worked for me:

# Windows 7:
r-libs-user="C:/R/library"
# Kubuntu 12:
# r-libs-user=~/R/%p-library/%v

You could create file Renviron.site in [your R installation path]\\etc with lines

HOME="${R_HOME}\..\r_user"
R_LIBS_SITE="${R_HOME}\..\libs_site"

which set second and third of your settings. First could be replaced by setwd(Sys.getenv("HOME")) .

I used the Rprofile.site file in [your R installation path]\\etc and added the following lines to make C:/R/library my default library location each time R is launched:

# set a site library
 .Library.site <- file.path("C:/R/library")
 .libPaths(.Library.site)

I tried the other answers here but none of them worked with R 2.13.1 on Windows 7 64.

adding this does the trick:

.Library.site = file.path( R.home() , ".." , "site-library" )
.libPaths(.Library.site)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM