在 R 中设置永久的默认 CRAN 镜像

发布于 2024-12-20 13:40:31 字数 101 浏览 1 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆° 2024-12-27 13:40:31

您可以在.Rprofile中设置存储库,以便在每次启动R时恢复您的选择

编辑:更准确地说:

添加

options(repos=c(CRAN="THE URL OF YOUR FAVORITE MIRROR"))

到您的 >.Rprofile,位于用户的主目录中。

您可以在此处找到 CRAN 镜像列表。

示例

# add this line to ~/.Rprofile, restart your R session so it takes effect
options(repos=c(CRAN="https://cran.r-project.org"))

或者,您可以在 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"] <- "https://cran.r-project.org"
       options(repos=r)})

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

Edit: to be more precise:

Add

options(repos=c(CRAN="THE URL OF YOUR FAVORITE MIRROR"))

to your .Rprofile, located in the home directory of your user.

You can find a list of CRAN mirrors here.

Example:

# add this line to ~/.Rprofile, restart your R session so it takes effect
options(repos=c(CRAN="https://cran.r-project.org"))

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"] <- "https://cran.r-project.org"
       options(repos=r)})
梦中的蝴蝶 2024-12-27 13:40:31

在一种情况下,上面建议的 .Rprofile 编辑不起作用。然而,下面的代码却做到了:

utils::setRepositories(ind = 0, addURLs = c(CRAN = "YOUR FAVORITE MIRROR"))

其中“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:

utils::setRepositories(ind = 0, addURLs = c(CRAN = "YOUR FAVORITE MIRROR"))

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 the addURLs = option and are comma separated within the character vector.

鲸落 2024-12-27 13:40:31

如果您尝试在 RStudio 中执行此操作,可以通过 RStudio UI(工具 -> 全局选项 -> 包)或使用文件 ~/.config/rstudio/rstudio-prefs 来执行此操作。 json 并将以下内容放入 https://cran.rstudio.com/ 中。

{
    "cran_mirror": {
        "name": "Global (CDN)",
        "host": "RStudio",
        "url": "https://cran.rstudio.com/",
        "country": "us",
        "ok": 1,
        "secondary": ""
    }
}

也许您已经设置了其他选项,因此您只需将 cran_mirror 添加到列表中即可。

我当前系统(RStudio Server 2022.02.2 Build 485Ubuntu 20.04.4 LTS)上的完整文件如下所示:

{
    "initial_working_directory": "~",
    "margin_column": 120,
    "scroll_past_end_of_document": true,
    "highlight_r_function_calls": true,
    "rainbow_parentheses": true,
    "posix_terminal_shell": "bash",
    "default_project_location": "~",
    "jobs_tab_visibility": "shown",
    "source_with_echo": true,
    "save_workspace": "never",
    "load_workspace": false,
    "always_save_history": false,
    "data_viewer_max_columns": 500,
    "cran_mirror": {
        "name": "Global (CDN)",
        "host": "RStudio",
        "url": "https://cran.rstudio.com/",
        "country": "us",
        "ok": 1,
        "secondary": ""
    }
}

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 for https://cran.rstudio.com/.

{
    "cran_mirror": {
        "name": "Global (CDN)",
        "host": "RStudio",
        "url": "https://cran.rstudio.com/",
        "country": "us",
        "ok": 1,
        "secondary": ""
    }
}

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:

{
    "initial_working_directory": "~",
    "margin_column": 120,
    "scroll_past_end_of_document": true,
    "highlight_r_function_calls": true,
    "rainbow_parentheses": true,
    "posix_terminal_shell": "bash",
    "default_project_location": "~",
    "jobs_tab_visibility": "shown",
    "source_with_echo": true,
    "save_workspace": "never",
    "load_workspace": false,
    "always_save_history": false,
    "data_viewer_max_columns": 500,
    "cran_mirror": {
        "name": "Global (CDN)",
        "host": "RStudio",
        "url": "https://cran.rstudio.com/",
        "country": "us",
        "ok": 1,
        "secondary": ""
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文