基于R/RStudio中安裝包“無法與服務(wù)器建立連接”的解決方案
這幾天在用RStudio寫程序的時(shí)候突然出現(xiàn)無法從網(wǎng)絡(luò)直接安裝r包的問題,也無法從本地直接安裝。
通過多方嘗試找到了問題解決的辦法,現(xiàn)在記錄在這里。
程序報(bào)錯(cuò)如下:
> 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'
網(wǎng)絡(luò)上主流的解決方式是在tools-global options-packages中切換下載的鏡像,有很多人反映他們的問題通過這個(gè)方法得到了解決,但是我換過很多鏡像都沒有用。
另一種方式是修改文件,在安裝文件夾中找到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)}
這也是設(shè)置鏡像的方法,與方法一相同。對(duì)我也沒用。
之后了解到R中install.packages()是從ie瀏覽器中連接鏡像的,所以無法連接服務(wù)器很可能是在ie這一環(huán)節(jié)出現(xiàn)了問題。
打開電腦的ie瀏覽器看能不能正常上網(wǎng),如果顯示代理問題,在工具-internet選項(xiàng)-連接中將局域網(wǎng)設(shè)置里的代理服務(wù)器這一項(xiàng)去掉勾選。這樣就可以正常上網(wǎng)了。再檢查RStudio也能夠正常安裝r包了。
之后出現(xiàn)另外一個(gè)問題:
> 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
無法將安裝包從臨時(shí)目錄中轉(zhuǎn)移,原因是殺毒軟件可能禁止了這樣的操作。
在殺毒軟件的白名單中加入R安裝在的文件夾,重新啟動(dòng)RStudio問題即可解決。
或者比較麻煩一點(diǎn)的話就是在臨時(shí)目錄中解壓這個(gè)壓縮文件到R安裝目錄中的library文件夾中。
補(bǔ)充:R或RStudio下載包時(shí)出錯(cuò)解決方案
當(dāng)我們想要在R&RStudio中下載包時(shí)可能會(huì)出現(xiàn)如下類似錯(cuò)誤

在如下目錄中的Rprofile.site文件中對(duì)應(yīng)位置增加一條代碼即可:
options(download.file.method="libcurl")

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
R語言 解決安裝ggplot2報(bào)錯(cuò)的問題
這篇文章主要介紹了R語言 解決安裝ggplot2報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
R語言之反轉(zhuǎn)ggplot2中的圖例鍵順序方法詳解
在本教程中,我們將學(xué)習(xí)如何反轉(zhuǎn)ggplot2中圖例鍵的順序,文章通過詳細(xì)的示例代碼給大家介紹如何反轉(zhuǎn)ggplot2中的圖例鍵順序,感興趣的同學(xué)可以跟著小編一起來學(xué)習(xí)2023-05-05
使用R語言繪制3D數(shù)據(jù)可視化scatter散點(diǎn)圖實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了使用R語言繪制3D數(shù)據(jù)可視化scatter散點(diǎn)圖的實(shí)現(xiàn)步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
解決R語言安裝時(shí)出現(xiàn)輯程包不存在的問題
這篇文章主要介紹了解決R語言安裝時(shí)出現(xiàn)輯程包不存在的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04

