简体   繁体   English

使R安装自包含/独立于用户

[英]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 我试图让R忽略c:\\ users \\ name \\ documents并完全独立/可移植

Here's my directory structure: 这是我的目录结构:

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

I updated my Rprofile.site as follows: 我更新了Rprofile.site如下:

# 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/... 当R启动时,我运行.libPaths(),但仍然看到c:/ users / ...

So perhaps R isn't updating per the Sys.setenv above? 因此,也许R不是按照上面的Sys.setenv更新? 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. 以我的经验,创建一个名为Renviron.site的文件并不能在我的所有Windows计算机上正常工作,而命名同一个文件。 Not sure why. 不知道为什么。 So if you're having difficulty with the above suggestions, then try .Renviron for a name. 因此,如果您对上述建议有困难,请尝试使用.Renviron作为名称。

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: Windows和Linux平台:

Setting R paths 设置R路径

Find out the default paths: .libPaths() 找出默认路径:.libPaths()

Set the path temporarily (in a R script) 临时设置路径(在R脚本中)

.libPaths( "F:/Rlib" )

where F could be the letter associated with, say, a USB drive. 其中F可能是与USB驱动器相关的字母。

Query paths (both Windows and Linux): 查询路径(Windows和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 getwd返回表示R进程当前工作目录的绝对文件路径

setwd(dir) is used to set the working directory to dir. setwd(dir)用于将工作目录设置为dir。

Windows (Tested on: 7x64) Windows(经过7x64测试)

Create an environment file named .Renviron place it in the working directory or home directory: 创建一个名为.Renviron的环境文件,将其放置在工作目录或主目录中:

"C:/Users/username/Documents"

Some users have reported that the .Renviron file needed to be in "c:/users/username/" instead. 一些用户报告说,.Renviron文件需要位于“ c:/ users / username /”中。 If you're not sure where to place it, save the desktop history and see where the .Rhistory file is located. 如果不确定将其放置在何处,请保存桌面历史记录并查看.Rhistory文件的位置。 Then place your .Renviron file in the same location. 然后,将您的.Renviron文件放在同一位置。 To save history savehistory() 保存历史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; 设置全局PATH我的电脑/属性/高级系统设置/环境变量->用户变量->路径->编辑c:\\ R; c:\\ R \\ library;

Linux (Tested on kUbuntu 12.10) Linux(在kUbuntu 12.10上测试)

Create an environment file named Renviron.site place it in: 创建一个名为Renviron.site的环境文件,并将其放在以下位置:

/etc/R/

Query the paths to check that your system is reading the Renviron.site file. 查询路径以检查系统是否正在读取Renviron.site文件。

# 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. 备注:afaik从底部到顶部读取文件,因此在底部定义了HOME。 In my setup ~ is correctly assigned to /home/ so I omit that last line anyway. 在我的设置中,〜已正确分配给/ home /,因此无论如何我都省略了最后一行。

If you use RStudio, you may also want to add an rsession.conf file in the RStudio program directory. 如果使用RStudio,则可能还需要在RStudio程序目录中添加一个rsession.conf文件。 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 您可以使用以下行在[your R installation path]\\etc创建文件Renviron.site

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")) . 首先可以用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: 我在[您的R安装路径] \\ etc中使用了Rprofile.site文件,并添加了以下几行,以使C:/ R / library成为每次启动R时的默认库位置:

# 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. 我在这里尝试了其他答案,但没有一个在Windows 7 64上与R 2.13.1一起使用。

adding this does the trick: 添加此功能就可以了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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