1. 程式人生 > >解決R/RStudio中安裝包“無法與伺服器建立連線”

解決R/RStudio中安裝包“無法與伺服器建立連線”

  這幾天在用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資料夾中。