今天解決解決了一個R包安裝的問題,并且硬著頭皮把install.packages和download.file的說明從頭到位看了一遍,應該再也沒有一個R包安裝能為難到我了。
問題實例
問題描述
能夠用瀏覽器訪問鏡像站點,但是在安裝R包時遇到如下問題,
# CRAN
Warning in install.packages :
unable to access index for repository https://mirrors.ustc.edu.cn/CRAN/src/contrib:
cannot open URL 'https://mirrors.ustc.edu.cn/CRAN/src/contrib/PACKAGES'
Warning in install.packages :
unable to access index for repository https://mirrors.ustc.edu.cn/CRAN/src/contrib:
cannot open URL 'https://mirrors.ustc.edu.cn/CRAN/src/contrib/PACKAGES'
Warning in install.packages :
package ‘ggtree’ is not available (for R version 3.5.1)
Warning in install.packages :
unable to access index for repository https://mirrors.ustc.edu.cn/CRAN/bin/windows/contrib/3.5:
cannot open URL 'https://mirrors.ustc.edu.cn/CRAN/bin/windows/contrib/3.5/PACKAGES'
# Bioconductor
Error: Bioconductor version cannot be validated; no internet connection?
In addition: Warning messages:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘GSEABase’
2: In file(con, "r") : InternetOpenUrl failed: '??'
3: In file(con, "r") : InternetOpenUrl failed: 'on'
解決思路
第一步,確認R能否真的能夠下載數(shù)據(jù)。檢索到R用download.file進行文件下載,
download.file(url = "https://upload-images.jianshu.io/upload_images/2013053-6e5c996e3a0d4c93.png",
destfile = "test.png")
發(fā)現(xiàn)無法直接下載內(nèi)容,證明R在連接網(wǎng)絡時出現(xiàn)了問題
trying URL 'https://upload-images.jianshu.io/upload_images/2013053-6e5c996e3a0d4c93.png'
Error in download.file(url = "https://upload-images.jianshu.io/upload_images/2013053-6e5c996e3a0d4c93.png", :
cannot open URL 'https://upload-images.jianshu.io/upload_images/2013053-6e5c996e3a0d4c93.png'
In addition: Warning message:
In download.file(url = "https://upload-images.jianshu.io/upload_images/2013053-6e5c996e3a0d4c93.png", :
InternetOpenUrl failed:
第二步,根據(jù)報錯信息, "InternetOpenUrl failed"進行檢索,找到一種解決思路,也就是指定R訪問網(wǎng)絡的方法為libcurl
download.file(url = "https://upload-images.jianshu.io/upload_images/2013053-6e5c996e3a0d4c93.png",
destfile = "test.png", methods="libcurl")
能夠解決問題。
深入學習install.packages()
為了讓自己能夠更好解決R包安裝問題,需要深入學習install.packages的原理(BiocManager::install本質(zhì)上也是調(diào)用install.packages)。
先仔細閱讀install.packages()的參數(shù):
pkgs: 默認是包名,比如說"Matrix", 會自動從CRAN上檢索對應的包,然后進行下載。如果你希望指定安裝本地包,或者一個具體的網(wǎng)絡地址,參考代碼如下:
# from url resource
install.packages("https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/3.5/Matrix_1.2-15.zip", repos=NULL)
# from local
install.packages("~/../Desktop/Matrix_1.2-15.zip", repos = NULL)
lib: R包放在那里,和.libPaths()有關(guān)repos: 鏡像地址,當設置為NULL時,就可以安裝本地包,或一個URLs。contriburl: 這個參數(shù)不常用,一般是你自己搞了一個本地鏡像點時使用,該參數(shù)會覆蓋掉reops. 與type="both"不兼容method: R包下載的方法,默認參數(shù)是default. 對于file://會調(diào)用internal,對于ftps://會調(diào)用libcurl,對于http://,https://,ftp://, windows默認使用wininet,對于Unix類系統(tǒng),默認使用libcurl. 注意:如果Windows上用capabilities("libcurl")返回時TRUE, 那么也可以用libcurl, Unix類系統(tǒng)無法使用wininet.available: 可以是avaiable.packages返回的鏡像點中可用R包,也可以設置為NULL(這時函數(shù)內(nèi)部會自動調(diào)用avaiable.packages). 與type="both"不兼容destdir: 下載的R包存放位置,NULL表示放在臨時文件夾中,在關(guān)閉R后會被刪除。dependencies:默認是NA,表示c("Depends", "Imports", "LinkingTo"), TRUE表示對于要安裝的R包是c("Depends", "Imports", "LinkingTo", "Suggests")依賴,依賴的依賴是c("Depends", "Imports", "LinkingTo", "Suggests"). 注意: 對于二進制包,都會忽略"LinkingTo"type: 下載的是二進制包("binary")還是源代碼"source". 如果設置為"binary", 依舊會先去檢查該軟件包最新的版本是否只有源代碼,可用options(install.packages.check.source = "no")關(guān)閉。當設置為"source"時,只有不含"C/C++/Fortran"代碼的R包可以被編譯,如果R包中有C/C++/Fortran代碼,那么Windows就需要安裝Rtools。注意: 在Windows編譯R包時,有一小部分需要設置INSTALL_opts = "--force-biarch"或INSTALL_opts = "--merge-multiarch", 建議后者。configure.args: 該參數(shù)只在源代碼編譯時使用,會傳入R CMD INSTALL中configure.vars: 該參數(shù)只在源代碼編譯時使用, 類似于configure.args, 效果是在運行configure前設置環(huán)境變量。clean: 在R CMD INSTALL中加入--clean參數(shù),用于清除臨時中間文件。Ncpus: 編譯時用多少CPU,加快編譯速度。verbose: 是否輸出安裝時的信息。libs_only: 是否只安裝64位或者32位的動態(tài)鏈接庫INSTALL_opts: 源代碼編譯時R CMD INSTALL的額外傳入?yún)?shù),例如c("--html", "--no-multiarch", "--no-test-load").quiet: 安靜模式,降低輸出的信息量keep_outputs: 是否在當前工作目錄下保留源代碼編譯后的輸出文件。...的額外的參數(shù)來自于download.file, 主要就是cache=TRUE表示服務端緩存。默認是"TRUE",如果是http://和https://更建議用cacheOK=FALSE`, 避免一些報錯。
關(guān)于Secure URLs
對于https://或者ftps://這類URL,R在下載數(shù)據(jù)時會嘗試對網(wǎng)站的證書進行驗證。通常會調(diào)用操作系統(tǒng)安裝的CA root certificates完成。
對于Windows系統(tǒng),method="libcurl"時可能會出現(xiàn)問題,也就是Windows系統(tǒng)不提供有效的CA certificate bundle, 也就是說默認情況下,Windows的certificates是沒有被驗證過的。也就是Sys.getenv("CURL_CA_BUNDLE")返回結(jié)果為空,建議Sys.setenv(CURL_CA_BUNDLE=file.path(Sys.getenv("R_HOME"),"/etc/curl-ca-bundle.crt"))打開驗證。
可以從https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt下載curl-ca-bundle.crt的備份。
關(guān)于代理
wininet調(diào)用系統(tǒng)中的Internet Option處理代理(proxy). 或者用Sys.setenv設置環(huán)境變量http_proxy,ftp_proxy
