首页 > 解决方案 > 尝试安装软件包时 contrib.url(repos, "Source) 中的 R 错误,但未指定 repo

问题描述

大家好,我在 R 中工作,试图使用 RMarkdown 运行一些代码,这是似乎给我带来问题的代码片段。

install.packages("car")
install.packages("bestNormalize")
library(readr)
library(dplyr)
library(leaps)
library(MASS)
library(car)
library(plyr)
library(alr4)

它很标准的东西。尝试运行代码时收到以下错误。

Error in contrib.url(repos, "source"):trying to use CRAN without setting a mirror Calls:
 <Anonymous> ... withVisible 
-> eval -> eval -> install.packages -> contrib.url Execution halted

我最近购买了一台新笔记本电脑并安装了 R。在我的旧笔记本电脑中,当我指定 install("package_name") 时,我从未收到此错误。发生这种情况是否有原因?我已经看到建议的解决方案以下列方式指定 repo:

install.packages("car", repos="http://cran.us.r-project.org")

但是我不满足于这个解决方案,因为我不想在每次尝试安装软件包时都指定存储库。我想也许找到某种方法将镜像设置为默认镜像,但不知道是否可以这样做。有任何想法吗?

标签: rinstallation

解决方案


我找到了解决方案。我做了以下事情:我通过运行以下命令找到并打开了我的 .RProfile:

file.edit(file.path("~", ".Rprofile")) 

然后我导航到https://cran.r-project.org并找到离我最近的镜子。然后在 .Rprofile 我写了以下命令,以便让 R studio 在打开时确认我的偏好

options(repos=structure(c(CRAN="https:theclosest_mirror_to_yourlocation.com")))

将其保存到 .Rprofile 并运行初始代码表明它是成功的。


推荐阅读