在 R 中设置永久的默认 CRAN 镜像
如何在 R 中永久设置特定的 CRAN 镜像?
我想将其永久设置在我的笔记本电脑中,这样当我执行 install.packages() 时,它就不会再次询问我选择哪个镜像。
How can I set a specific CRAN mirror permanently in R?
I want to set it permanently in my laptop so that when I do install.packages()
, it won't ask me again which mirror to choose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在.Rprofile中设置存储库,以便在每次启动R时恢复您的选择
编辑:更准确地说:
添加
到您的 >.Rprofile,位于用户的主目录中。
您可以在此处找到 CRAN 镜像列表。
示例:
或者,您可以在
Rprofile.site
中设置站点范围内的镜像。文件的位置由?Startup
给出:对于第一个选项,请执行
Sys.getenv("R_PROFILE")
操作,或者Sys.getenv("R_HOME")
或R.home()
> 对于第二个选项。在 macOS 上,第二个位置是/Library/Frameworks/R.framework/Resources/etc/
。该文件可能不存在,或者您可能会看到以下几行被注释掉:
因此请删除注释标记并将“http://my.local.cran”更改为正确的网站,例如:
You can set repos in your .Rprofile to restore your choice every time you start R
Edit: to be more precise:
Add
to your .Rprofile, located in the home directory of your user.
You can find a list of CRAN mirrors here.
Example:
Alternatively, you can set the mirror site-wide in your
Rprofile.site
. The location of the file is given by?Startup
:So do
Sys.getenv("R_PROFILE")
for the first option, orSys.getenv("R_HOME")
orR.home()
for the second option. On macOS, the location of the second is/Library/Frameworks/R.framework/Resources/etc/
.The file may not exist, or you may see the following lines commented out :
So remove the comment marks and change "http://my.local.cran" to the correct website, e.g.:
在一种情况下,上面建议的 .Rprofile 编辑不起作用。然而,下面的代码却做到了:
其中“YOUR FAVORITE MIRROR”是 URL,而不是名称。
编辑 .Rprofile 后重新启动 R。
ind = 0
将表明您只需要指定的存储库。其他存储库可以包含在addURLs =
选项中,并在字符向量中以逗号分隔。In one instance, the .Rprofile edit suggested above did not work. However, the following code did:
where "YOUR FAVORITE MIRROR" is the URL, not the name.
Restart R after editing the .Rprofile.
ind = 0
will indicate that you only want the named repository. Additional repositories can be included in theaddURLs =
option and are comma separated within the character vector.如果您尝试在 RStudio 中执行此操作,可以通过 RStudio UI(工具 -> 全局选项 -> 包)或使用文件
~/.config/rstudio/rstudio-prefs 来执行此操作。 json
并将以下内容放入https://cran.rstudio.com/
中。也许您已经设置了其他选项,因此您只需将
cran_mirror
添加到列表中即可。我当前系统(
RStudio Server 2022.02.2 Build 485
、Ubuntu 20.04.4 LTS
)上的完整文件如下所示:In case you are trying to do this in RStudio, you can either do it via the RStudio UI (Tools -> Global Options -> Packages) or use the file
~/.config/rstudio/rstudio-prefs.json
and put the following inside forhttps://cran.rstudio.com/
.Probably you already have other options set within so you can just add
cran_mirror
to the list.The full file on my current system (
RStudio Server 2022.02.2 Build 485
,Ubuntu 20.04.4 LTS
) looks like this: