首页 > 编程技术 > R语言

基于R/RStudio中安装包“无法与服务器建立连接”的解决方案

发布时间:2021-5-6 13:51

这几天在用RStudio写程序的时候突然出现无法从网络直接安装r包的问题,也无法从本地直接安装。

通过多方尝试找到了问题解决的办法,现在记录在这里。

程序报错如下:

> install.packages('REmap')
Warning in install.packages :
 unable to access index for repository https://cran.rstudio.com/src/contrib:
 cannot open URL 'https://cran.rstudio.com/src/contrib/PACKAGES'
Warning in install.packages :
 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'
Warning in install.packages :
 unable to access index for repository https://cran.rstudio.com/src/contrib:
 cannot open URL 'https://cran.rstudio.com/src/contrib/PACKAGES'
Warning in install.packages :
 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'
Warning in install.packages :
 package ‘REmap' is not available (for R version 3.4.1)
Warning in install.packages :
 unable to access index for repository https://cran.rstudio.com/bin/windows/contrib/3.4:
 cannot open URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/PACKAGES'
Warning in install.packages :
 unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/3.4:
 cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/3.4/PACKAGES'
Warning: unable to access index for repository https://cran.rstudio.com/src/contrib:
 cannot open URL 'https://cran.rstudio.com/src/contrib/PACKAGES'
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'

网络上主流的解决方式是在tools-global options-packages中切换下载的镜像,有很多人反映他们的问题通过这个方法得到了解决,但是我换过很多镜像都没有用。

另一种方式是修改文件,在安装文件夹中找到Rprofile.site文件,我的路径是D:\Program Files\R\R-3.4.1\etc,用记事本打开,在里面添加语句:  

# set a CRAN mirror
local({r <- getOption(“repos”)
r[“CRAN”] <- “http://mirrors.tuna.tsinghua.edu.cn/CRAN/”
options(repos=r)}

这也是设置镜像的方法,与方法一相同。对我也没用。

之后了解到R中install.packages()是从ie浏览器中连接镜像的,所以无法连接服务器很可能是在ie这一环节出现了问题。

打开电脑的ie浏览器看能不能正常上网,如果显示代理问题,在工具-internet选项-连接中将局域网设置里的代理服务器这一项去掉勾选。这样就可以正常上网了。再检查RStudio也能够正常安装r包了。

之后出现另外一个问题:  

> install.packages('sp')
trying URL 'https://cran.wu.ac.at/bin/windows/contrib/3.4/sp_1.2-5.zip'
Content type 'application/zip' length 1538503 bytes (1.5 MB)
downloaded 1.5 MB

package ‘sp' successfully unpacked and MD5 sums checked
Warning in install.packages :
 unable to move temporary installation ‘D:\Program Files\R\R-3.4.1\library\file4041367c44\sp' to ‘D:\Program Files\R\R-3.4.1\library\sp'

The downloaded binary packages are in
 C:\Users\asus\AppData\Local\Temp\RtmpWOz6Xu\downloaded_packages

无法将安装包从临时目录中转移,原因是杀毒软件可能禁止了这样的操作。

在杀毒软件的白名单中加入R安装在的文件夹,重新启动RStudio问题即可解决。

或者比较麻烦一点的话就是在临时目录中解压这个压缩文件到R安装目录中的library文件夹中。

补充:R或RStudio下载包时出错解决方案

当我们想要在R&RStudio中下载包时可能会出现如下类似错误

在如下目录中的Rprofile.site文件中对应位置增加一条代码即可:

options(download.file.method="libcurl")

修改好后,再次下载即可成功!

以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。如有错误或未考虑完全的地方,望不吝赐教。

标签:[!--infotagslink--]

您可能感兴趣的文章: