安装R包的问题

Problems with installation R packages

我是 Windows 用户。几周前,我安装了 R 和 Rstudio 以及许多软件包。今天有消息说没有安装新包

Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/src/contrib:
  cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/src/contrib/PACKAGES'

我重新安装了 R,但这并没有解决问题。

install.packages 中的警告:

  InternetOpenUrl failed: 'Can not connect to server'

这在 R 和 RStudio 中只是偶尔出现一次。 RStudio 更改了很多设置,"repos" 选项就是其中之一。在Windows上添加了以下内容

编辑:不是 RStudio 添加了这个额外的存储库。该存储库由 Brian Ripley 博士友情提供,用于由于某种原因无法在 CRAN 上提供的软件包(许可证,不是开箱即用,需要额外的软件,......)。这个在设置里叫"CRANextra":

> getOption("repos")
                                CRAN                            CRANextra 
         "https://cran.rstudio.com/" "http://www.stats.ox.ac.uk/pub/RWin" 
attr(,"RStudio")
[1] TRUE

因此 RStudio 在 Windows 上 运行 时尝试访问特定的存储库,但该存储库过去有一些连接问题;它并不总是可以访问,如果不能访问,则会发出您报告的警告。

您可以通过重置此选项来停止此警告:

options(repos = "https://cran.rstudio.com") # or a repo of your choice.

这允许您在没有警告的情况下安装软件包:

> install.packages("fortunes")
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/fortunes_1.5-4.zip'
Content type 'application/zip' length 202721 bytes (197 KB)
downloaded 197 KB

package ‘fortunes’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\Joris\AppData\Local\Temp\Rtmpu0febg\downloaded_packages

即使显示此警告,仍会从 rstudio CRAN 镜像安装包。警告是 reported as a bug,RStudio 承诺会尽快解决它。

编辑:R FAQ (last paragraph) 中有关 CRANextra 存储库的更多信息:

Some CRAN packages that do not build out of the box on Windows, require additional software, or are shipping third party libraries for Windows cannot be made available on CRAN in form of a Windows binary packages. Nevertheless, some of these packages are available at the “CRAN extras” repository at https://www.stats.ox.ac.uk/pub/RWin/ kindly provided by Brian D. Ripley. Note that this repository is a default repository for recent versions of R for Windows.

同时,在 R Studio 修复错误的同时,一个暂时的解决方案是编辑您的 "Rprofile.site" 文件并添加此行

options(repos = getOption("repos")["CRAN"])

这样,每次启动 R 会话时,CRANextra 存储库 ("http://www.stats.ox.ac.uk/pub/RWin") 都会自动删除。请记住在错误解决后删除或评论#这一行。

我遇到了类似的问题,对我有用的修复是,在 RStudio 中,我打开了工具 -> 全局选项 -> 包 -> 主 CRAN 存储库 -> 设置全局。

还要确保您 没有使用 installed.packages 由于 RStudio 输入建议,很多人都会遇到这种情况。 您需要使用 install.packages("")