解决R/RStudio中安装包“无法与服务器建立连接”
程序员文章站
2024-02-15 19:30:58
...
这几天在用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文件夹中。