在R中设置默认的CRAN镜像永久 [英] Set default CRAN mirror permanent in R

查看:604
本文介绍了在R中设置默认的CRAN镜像永久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在R中永久设置特定的CRAN镜像?

我想将其永久设置在笔记本电脑中,这样当我执行install.packages()时,它不会再询问我要选择哪个镜子.

解决方案

您可以在 .Rprofile 中设置 repos ,以在每次启动R时恢复选择

更准确地说:

添加

options(repos=structure(c(CRAN="YOUR FAVORITE MIRROR")))

到您的 .Rprofile


或者,您可以在Rprofile.site中在整个站点范围内设置镜像.文件的位置由?Startup指定:

该文件的路径取自R_PROFILE的值 环境变量(波浪线扩展后).如果这个变量是 未设置,默认值为R_HOME/etc/Rprofile.site,如果 它存在(在工厂级"安装中不存在).

第一个选项使用Sys.getenv("R_PROFILE"),第二个选项使用Sys.getenv("R_HOME")R.home().在macOS上,第二个的位置是/Library/Frameworks/R.framework/Resources/etc/.

该文件可能不存在,或者您可能会看到以下几行已被注释掉:

# set a CRAN mirror
# local({r <- getOption("repos")
#       r["CRAN"] <- "http://my.local.cran"
#       options(repos=r)})

因此删除注释标记,然后将" http://my.local.cran 更改为正确的网站,例如:

 local({r <- getOption("repos")
       r["CRAN"] <- "http://cran.r-project.org"
       options(repos=r)})
 

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.

解决方案

You can set repos in your .Rprofile to restore your choice every time you start R

Edit: to be more precise:

Add

options(repos=structure(c(CRAN="YOUR FAVORITE MIRROR")))

to your .Rprofile


Alternatively, you can set the mirror site-wide in your Rprofile.site. The location of the file is given by ?Startup:

The path of this file is taken from the value of the R_PROFILE environment variable (after tilde expansion). If this variable is unset, the default is R_HOME/etc/Rprofile.site, which is used if it exists (which it does not in a 'factory-fresh' installation).

So do Sys.getenv("R_PROFILE") for the first option, or Sys.getenv("R_HOME") or R.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 :

# set a CRAN mirror
# local({r <- getOption("repos")
#       r["CRAN"] <- "http://my.local.cran"
#       options(repos=r)})

So remove the comment marks and change "http://my.local.cran" to the correct website, e.g.:

local({r <- getOption("repos")
       r["CRAN"] <- "http://cran.r-project.org"
       options(repos=r)})

这篇关于在R中设置默认的CRAN镜像永久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆