避免被要求提供 CRAN 镜像?
Avoid being asked for CRAN mirror?
当从终端运行 R 时,我们有时会看到
--- Please select a CRAN mirror for use in this session ---
是否有一种通用、有效且令人难忘的方式来 install.packages("example")
而无需记住 url 或与对话框交互?
也就是说,有没有办法安装一个 R 包,它是:
- 通用(适用于 any/all R 安装,与操作系统无关)
- 不需要记忆url(s)
- 不需要与对话盒互动
- 不需要创建或编辑任何文件(例如
.Rprofile
)
最后,理想情况下,一种简短易记的方法(或无需查找即可轻松使用)。
这是一个理想解决方案的伪代码示例(其中 'force' 是“选择最明显的默认值并不惜一切代价继续前进!”的伪代码)
force(install.packages("example"))
如果您 运行 help("install.packages")
,您可以看到存储库的默认值为 repos = getOption("repos")
。如果您沿着这条路走到 help("getOption")
,它会提供更多见解。这是 repos
选项的内容。
repos:
URLs of the repositories for use by update.packages. Defaults
to c(CRAN="@CRAN@"), a value that causes some utilities to prompt for
a CRAN mirror. To avoid this do set the CRAN mirror, by something like
local({r <- getOption("repos"); r["CRAN"] <- "http://my.local.cran";
options(repos = r)}).
您可以通过进入 R 安装的 'etc/' 子目录来查看。那里有一个名为 'repositories' 的文件。虽然其他一些存储库(例如 R-Forge)具有默认 URL,但 CRAN 没有。它显示帮助文件引用的 @CRAN@
。
R 文档建议您某些实用程序(例如您在命令行中遇到的实用程序)会提示您提供镜像,除非明确设置该选项。该文档未指明替代解决方法。
之所以没有函数告诉它使用“最明显的默认值”,是因为实际上没有默认值。所以像你假设的force()
这样的方法是不可能的。
一个 编辑 更多信息:
您可以使用 utils
中的一些助手来设置 repos
选项。我不确定您的标准是否足够容易记住,但是有 chooseCRANmirror()
和 getCRANmirrors()
.
# this should work
chooseCRANmirror(ind = 1)
install.packages("example")
# or this clunky approach
install.packages("example", repos = getCRANmirrors()[1,"URL"])
但老实说,那时你最好记住 repos = https://cloud.r-project.org/
。
当从终端运行 R 时,我们有时会看到
--- Please select a CRAN mirror for use in this session ---
是否有一种通用、有效且令人难忘的方式来 install.packages("example")
而无需记住 url 或与对话框交互?
也就是说,有没有办法安装一个 R 包,它是:
- 通用(适用于 any/all R 安装,与操作系统无关)
- 不需要记忆url(s)
- 不需要与对话盒互动
- 不需要创建或编辑任何文件(例如
.Rprofile
)
最后,理想情况下,一种简短易记的方法(或无需查找即可轻松使用)。
这是一个理想解决方案的伪代码示例(其中 'force' 是“选择最明显的默认值并不惜一切代价继续前进!”的伪代码)
force(install.packages("example"))
如果您 运行 help("install.packages")
,您可以看到存储库的默认值为 repos = getOption("repos")
。如果您沿着这条路走到 help("getOption")
,它会提供更多见解。这是 repos
选项的内容。
repos:
URLs of the repositories for use by update.packages. Defaults to c(CRAN="@CRAN@"), a value that causes some utilities to prompt for a CRAN mirror. To avoid this do set the CRAN mirror, by something like local({r <- getOption("repos"); r["CRAN"] <- "http://my.local.cran"; options(repos = r)}).
您可以通过进入 R 安装的 'etc/' 子目录来查看。那里有一个名为 'repositories' 的文件。虽然其他一些存储库(例如 R-Forge)具有默认 URL,但 CRAN 没有。它显示帮助文件引用的 @CRAN@
。
R 文档建议您某些实用程序(例如您在命令行中遇到的实用程序)会提示您提供镜像,除非明确设置该选项。该文档未指明替代解决方法。
之所以没有函数告诉它使用“最明显的默认值”,是因为实际上没有默认值。所以像你假设的force()
这样的方法是不可能的。
一个 编辑 更多信息:
您可以使用 utils
中的一些助手来设置 repos
选项。我不确定您的标准是否足够容易记住,但是有 chooseCRANmirror()
和 getCRANmirrors()
.
# this should work
chooseCRANmirror(ind = 1)
install.packages("example")
# or this clunky approach
install.packages("example", repos = getCRANmirrors()[1,"URL"])
但老实说,那时你最好记住 repos = https://cloud.r-project.org/
。