简体   繁体   English

避免被要求提供 CRAN 镜像?

[英]Avoid being asked for CRAN mirror?

When running R from the terminal, we sometimes see从终端运行 R 时,我们有时会看到

--- Please select a CRAN mirror for use in this session ---

Is there a universal, efficient, and memorable way to install.packages("example") without needing to memorise urls or interact with a dialogue box?是否有一种通用、高效且令人难忘的方式来install.packages("example")而无需记住 url 或与对话框交互?

That is, is there a way to install an R package which is:也就是说,有没有办法安装 R package 这是:

  1. universal (works on any/all installations of R, irrespective of operating system)通用(适用于 R 的任何/所有安装,与操作系统无关)
  2. Requires no memorisation of url(s)无需记忆网址
  3. Requires no interaction with duologue box(es)不需要与双语盒交互
  4. Requires no files (eg .Rprofile ) to be created or edited不需要创建或编辑文件(例如.Rprofile

Lastly, ideally, a method which is short and memorable (or easy use without having to look it up).最后,理想情况下,一种简短且令人难忘的方法(或无需查找即可轻松使用)。

Here's a pseudo code example of an ideal solution (where 'force' is pseudocode for 'choose the most obvious defaults and press on at all costs!)这是一个理想解决方案的伪代码示例(其中'force'是'选择最明显的默认值并不惜一切代价按下'的伪代码!)

force(install.packages("example"))

If you run help("install.packages") , you can see that the default value for the repository is repos = getOption("repos") .如果您运行help("install.packages") ,您可以看到存储库的默认值为repos = getOption("repos") If you follow this trail to help("getOption") , it provides some more insight.如果您按照这条线索找到help("getOption") ,它会提供更多见解。 Here is what it says for the repos option.这是repos选项的内容。

repos:回购:

URLs of the repositories for use by update.packages. update.packages 使用的存储库的 URL。 Defaults to c(CRAN="@CRAN@"), a value that causes some utilities to prompt for a CRAN mirror.默认为 c(CRAN="@CRAN@"),该值会导致某些实用程序提示输入 CRAN 镜像。 To avoid this do set the CRAN mirror, by something like local({r <- getOption("repos"); r["CRAN"] <- "http://my.local.cran"; options(repos = r)}).为避免这种情况,请设置 CRAN 镜像,例如 local({r <- getOption("repos"); r["CRAN"] <- "http://my.local.cran"; options(repos = r )})。

You can see this by going into the 'etc/' subdirectory of your R installation.您可以通过进入 R 安装的“etc/”子目录看到这一点。 There is a file there called 'repositories'.那里有一个名为“存储库”的文件。 While some other repos (eg, R-Forge) have default URLs, CRAN does not.虽然其他一些存储库(例如,R-Forge)具有默认 URL,但 CRAN 没有。 It shows @CRAN@ as referenced by the help file.它显示了帮助文件引用的@CRAN@

The R documentation advises you that some utilities, such as what you are experiencing at the command line, will prompt you for a mirror, unless the option is explicitly set. R 文档建议您某些实用程序(例如您在命令行中遇到的)会提示您输入镜像,除非明确设置了该选项。 The documentation does not indicate an alternative work around.该文档并未指示替代解决方法。

The reason why there cannot be a function to tell it to use the "most obvious default" is that there is actually no default.之所以不能有一个 function 告诉它使用“最明显的默认值”,是因为实际上没有默认值。 So a method such as your hypothetical force() would not be possible.所以像你假设的force()这样的方法是不可能的。


An edit with a bit more info:包含更多信息的编辑

You can use a few helpers from utils to set the repos option.您可以使用utils中的一些帮助程序来设置repos选项。 I am not sure if it is easy enough to remember for your criteria, but there is chooseCRANmirror() and getCRANmirrors() .我不确定您的标准是否容易记住,但有chooseCRANmirror()getCRANmirrors()

# this should work
chooseCRANmirror(ind = 1)
install.packages("example")

# or this clunky approach
install.packages("example", repos = getCRANmirrors()[1,"URL"])

But honestly at that point you may be better off just remembering repos = https://cloud.r-project.org/ .但老实说,你最好记住repos = https://cloud.r-project.org/

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

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