简体   繁体   English

在 .Rprofile 和 .Renviron 中设置 R 的默认行为。 了解在何处做什么

[英]Setting R's default behaviour in .Rprofile and .Renviron. Understanding what to do where

I am trying to understand the use of the files .Renviron and .Rprofile.我试图了解文件 .Renviron 和 .Rprofile 的使用。 If I understand correctly, the .Rprofile is sort of a startup script, sourced as R code, that sets the options and environment variables that a user may want either all the time, or for a specific project.如果我理解正确的话, .Rprofile是一种启动脚本,来源于 R 代码,它设置用户可能一直想要的选项和环境变量,或者用于特定项目。 On the other hand .Renviron is loaded before .Rproject, and defines environment variables only.另一方面, .Renviron在 .Rproject 之前加载,并且仅定义环境变量。

By design I understand that R will load either the user or the project level .Renviron and .Rprofile files, but it won't load both user and project level files.根据设计,我知道 R 将加载用户或项目级别的 .Renviron 和 .Rprofile 文件,但它不会同时加载用户和项目级别的文件。 Essentially, R will only load the project specific .Rprofile and .Renviron files, provided they are defined.本质上,R 只会加载项目特定的 .Rprofile 和 .Renviron 文件,前提是它们被定义。 That said, some libraries and functions would be prudent to put in the user level .Rprofile, as I need it pretty much all the time (eg I use dplyr syntax a lot), while at the same time I'd like to load project specific libraries and functions as well.也就是说,将一些库和函数放在用户级别 .Rprofile 中是明智的,因为我几乎一直都需要它(例如,我dplyr使用dplyr语法),同时我想加载项目特定的库和函数。

The purpose of the .Renviron file is more elusive to me. .Renviron 文件的目的对我来说更难以捉摸。 From what I understand, its purpose is to store environment variables, such as passwords, API keys, etc. .据我了解,它的目的是存储环境变量,比如密码、API密钥等 However, I can also set environment variables in .Rprofile using Sys.setenv() .但是,我也可以使用Sys.setenv()在 .Rprofile 中设置环境变量。 For example, I have the environment variable set in a project's .Rprofile, to use parallelization with the package below:例如,我在项目的 .Rprofile 中设置了环境变量,以将并行化与以下包一起使用:

Sys.setenv(OMP_NUM_THREADS=parallel::detectCores())
library(OpenMx)

Since the .Renviron doesn't use code, my assumption is I could've put this line in a .Renviron file with the following syntax:由于 .Renviron 不使用代码,我的假设是我可以使用以下语法将此行放在 .Renviron 文件中:

OMP_NUM_THREADS=[number of cores]

However, I find little useful information on how to set environment variables in .Renviron, and what is advisable to put here.但是,我发现很少有关于如何在 .Renviron 中设置环境变量的有用信息,以及建议放在这里的内容。

My questions therefore are:因此,我的问题是:

  1. How can I load both user and project level .Renviron and .Rproject files when working in a project?在项目中工作时,如何加载用户和项目级别的 .Renviron 和 .Rproject 文件?
  2. What environment variables would I typically put in .Renviron?我通常会在 .Renviron 中放入哪些环境变量? (Any list or tutorials on how to set variables would be appreciated.) (任何关于如何设置变量的列表或教程将不胜感激。)
  3. In which cases would it be recommended to add environment variables to .Renviron over using Sys.setenv() in .Rprofile, and vice versa?在哪些情况下,建议在 .Rprofile 中使用Sys.setenv()向 .Renviron 添加环境变量,反之亦然?

However, I can also set environment variables in .Rprofile using Sys.setenv().但是,我也可以使用 Sys.setenv() 在 .Rprofile 中设置环境变量。

"Yes but" these can under standard POSIX behaviour not alter the running process for which the variables have to be set before. “是的,但是”这些可以在标准 POSIX 行为下不会改变之前必须设置变量的运行过程。

I just like you tried to get by for as long as I could with only ~/.Rprofile (or even just Rprofile.site for the whole machine) but eventually added things in .Renviron for我就像你试图通过~/.Rprofile (甚至整个机器的Rprofile.site )尽可能长的时间,但最终在.Renviron添加了一些东西

  • R_LIBS_USER to "" as I prefer not to have installations below ~ R_LIBS_USER改为""因为我不想在下面安装 ~
  • R_MAX_NUM_DLLS which has to be here R_MAX_NUM_DLLS必须在这里
  • plus a few tokens for services加上一些服务代币
  • plus a reticulate option加上一个reticulate选项
  • plus a R CMD check option加上R CMD check选项

so in some cases you do in fact have to use .Renviron (or Renvirob.site ).所以在某些情况下,您实际上必须使用.Renviron (或Renvirob.site )。

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

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