如何自动在R中加载软件包? [英] How to load packages in R automatically?
问题描述
您能建议我一种自动在R中加载软件包的方法吗?我的意思是,我想在R中启动会话,而无需多次使用library('package name')
.假设我下载了我下次启动R时要使用的所有软件包.
Could you suggest me a way for loading packages in R automatically? I mean, I want to start a session in R without needing to use library('package name')
several times. Suppose I downloaded all packages I'll want to use the next time I start R.
推荐答案
将library(foo)
放入.Rprofile
文件或设置R_DEFAULT_PACKAGES
:请参见?Rprofile
...
Put library(foo)
in your .Rprofile
file or set R_DEFAULT_PACKAGES
: see ?Rprofile
...
尤其是(因为?Rprofile
很长且可能令人生畏):
In particular (because ?Rprofile
is long and potentially intimidating):
如果您要的软件包与默认软件包不同, 开始,在".Rprofile"中插入对选项"的调用,或者 "Rprofile.site"文件.例如,‘options(defaultPackages = character())"在启动时不会附加任何额外的软件包(仅 基本"包)(或将"R_DEFAULT_PACKAGES = NULL"设置为环境 运行R之前的变量).使用"options(defaultPackages =")"或 ‘R_DEFAULT_PACKAGES ="强制使用R 系统默认值.
If you want a different set of packages than the default ones when you start, insert a call to ‘options’ in the ‘.Rprofile’ or ‘Rprofile.site’ file. For example, ‘options(defaultPackages = character())’ will attach no extra packages on startup (only the ‘base’ package) (or set ‘R_DEFAULT_PACKAGES=NULL’ as an environment variable before running R). Using ‘options(defaultPackages = "")’ or ‘R_DEFAULT_PACKAGES=""’ enforces the R system default.
由于您可能要做要加载所有默认软件包,然后再加载其他软件包(而不是不加载某些默认软件包),因此可以放
Since you probably do want all of the default packages loaded, and then extra ones in addition (rather than, say, not loading some of the default packages), you can either put
library("mypackage1")
library("mypackage2")
[etc.]
或使用options(defaultPackages=...)
:
options(defaultPackages=c(getOption("defaultPackages"),
"mypackage1","mypackage2", ... [etc.]))
在您的.Rprofile
中,将所需的程序包追加为标准默认值.
in your .Rprofile
to append your desired packages to the standard defaults.
编辑(摘录自评论)重新使它可以在 Rstudio :
http://rstudio.org/docs/using/workspaces 建议Rstudio执行.Rprofile
并然后执行R Startup [ http://stat.ethz.ch/R-manual/R-patched/library/base/html/Startup.html ]"(与?Rprofile
相同).是否查看Rprofile.site
还是模棱两可的.
edit (copied from comment) re getting this to work in Rstudio:
http://rstudio.org/docs/using/workspaces suggests that Rstudio executes .Rprofile
and then "Performs the other actions described in R Startup [ http://stat.ethz.ch/R-manual/R-patched/library/base/html/Startup.html ]" (which is the same as ?Rprofile
). It is ambiguous whether it looks at Rprofile.site
or not.
编辑#2 :根据下面的评论,它确实适用于Rstudio的最新版本.
edit #2: according to comment below, it does work with a recent version of Rstudio.
这篇关于如何自动在R中加载软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!