今天解決解決了一個(gè)R包安裝的問(wèn)題,并且硬著頭皮把install.packages和download.file的說(shuō)明從頭到位看了一遍,應(yīng)該再也沒(méi)有一個(gè)R包安裝能為難到我了。
問(wèn)題實(shí)例
問(wèn)題描述
能夠用瀏覽器訪(fǎng)問(wèn)鏡像站點(diǎn),但是在安裝R包時(shí)遇到如下問(wèn)題,
# 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èn)R能否真的能夠下載數(shù)據(jù)。檢索到R用download.file進(jìn)行文件下載,
download.file(url = "https://upload-images.jianshu.io/upload_images/2013053-6e5c996e3a0d4c93.png",
destfile = "test.png")
發(fā)現(xiàn)無(wú)法直接下載內(nèi)容,證明R在連接網(wǎng)絡(luò)時(shí)出現(xiàn)了問(wè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ù)報(bào)錯(cuò)信息, "InternetOpenUrl failed"進(jìn)行檢索,找到一種解決思路,也就是指定R訪(fǎng)問(wèn)網(wǎng)絡(luò)的方法為libcurl
download.file(url = "https://upload-images.jianshu.io/upload_images/2013053-6e5c996e3a0d4c93.png",
destfile = "test.png", methods="libcurl")
能夠解決問(wèn)題。
深入學(xué)習(xí)install.packages()
為了讓自己能夠更好解決R包安裝問(wèn)題,需要深入學(xué)習(xí)install.packages的原理(BiocManager::install本質(zhì)上也是調(diào)用install.packages)。
先仔細(xì)閱讀install.packages()的參數(shù):
pkgs: 默認(rèn)是包名,比如說(shuō)"Matrix", 會(huì)自動(dòng)從CRAN上檢索對(duì)應(yīng)的包,然后進(jìn)行下載。如果你希望指定安裝本地包,或者一個(gè)具體的網(wǎng)絡(luò)地址,參考代碼如下:
# 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: 鏡像地址,當(dāng)設(shè)置為NULL時(shí),就可以安裝本地包,或一個(gè)URLs。contriburl: 這個(gè)參數(shù)不常用,一般是你自己搞了一個(gè)本地鏡像點(diǎn)時(shí)使用,該參數(shù)會(huì)覆蓋掉reops. 與type="both"不兼容method: R包下載的方法,默認(rèn)參數(shù)是default. 對(duì)于file://會(huì)調(diào)用internal,對(duì)于ftps://會(huì)調(diào)用libcurl,對(duì)于http://,https://,ftp://, windows默認(rèn)使用wininet,對(duì)于Unix類(lèi)系統(tǒng),默認(rèn)使用libcurl. 注意:如果Windows上用capabilities("libcurl")返回時(shí)TRUE, 那么也可以用libcurl, Unix類(lèi)系統(tǒng)無(wú)法使用wininet.available: 可以是avaiable.packages返回的鏡像點(diǎn)中可用R包,也可以設(shè)置為NULL(這時(shí)函數(shù)內(nèi)部會(huì)自動(dòng)調(diào)用avaiable.packages). 與type="both"不兼容destdir: 下載的R包存放位置,NULL表示放在臨時(shí)文件夾中,在關(guān)閉R后會(huì)被刪除。dependencies:默認(rèn)是NA,表示c("Depends", "Imports", "LinkingTo"), TRUE表示對(duì)于要安裝的R包是c("Depends", "Imports", "LinkingTo", "Suggests")依賴(lài),依賴(lài)的依賴(lài)是c("Depends", "Imports", "LinkingTo", "Suggests"). 注意: 對(duì)于二進(jìn)制包,都會(huì)忽略"LinkingTo"type: 下載的是二進(jìn)制包("binary")還是源代碼"source". 如果設(shè)置為"binary", 依舊會(huì)先去檢查該軟件包最新的版本是否只有源代碼,可用options(install.packages.check.source = "no")關(guān)閉。當(dāng)設(shè)置為"source"時(shí),只有不含"C/C++/Fortran"代碼的R包可以被編譯,如果R包中有C/C++/Fortran代碼,那么Windows就需要安裝Rtools。注意: 在Windows編譯R包時(shí),有一小部分需要設(shè)置INSTALL_opts = "--force-biarch"或INSTALL_opts = "--merge-multiarch", 建議后者。configure.args: 該參數(shù)只在源代碼編譯時(shí)使用,會(huì)傳入R CMD INSTALL中configure.vars: 該參數(shù)只在源代碼編譯時(shí)使用, 類(lèi)似于configure.args, 效果是在運(yùn)行configure前設(shè)置環(huán)境變量。clean: 在R CMD INSTALL中加入--clean參數(shù),用于清除臨時(shí)中間文件。Ncpus: 編譯時(shí)用多少CPU,加快編譯速度。verbose: 是否輸出安裝時(shí)的信息。libs_only: 是否只安裝64位或者32位的動(dòng)態(tài)鏈接庫(kù)INSTALL_opts: 源代碼編譯時(shí)R CMD INSTALL的額外傳入?yún)?shù),例如c("--html", "--no-multiarch", "--no-test-load").quiet: 安靜模式,降低輸出的信息量keep_outputs: 是否在當(dāng)前工作目錄下保留源代碼編譯后的輸出文件。...的額外的參數(shù)來(lái)自于download.file, 主要就是cache=TRUE表示服務(wù)端緩存。默認(rèn)是"TRUE",如果是http://和https://更建議用cacheOK=FALSE`, 避免一些報(bào)錯(cuò)。
關(guān)于Secure URLs
對(duì)于https://或者ftps://這類(lèi)URL,R在下載數(shù)據(jù)時(shí)會(huì)嘗試對(duì)網(wǎng)站的證書(shū)進(jìn)行驗(yàn)證。通常會(huì)調(diào)用操作系統(tǒng)安裝的CA root certificates完成。
對(duì)于Windows系統(tǒng),method="libcurl"時(shí)可能會(huì)出現(xiàn)問(wèn)題,也就是Windows系統(tǒng)不提供有效的CA certificate bundle, 也就是說(shuō)默認(rèn)情況下,Windows的certificates是沒(méi)有被驗(yàn)證過(guò)的。也就是Sys.getenv("CURL_CA_BUNDLE")返回結(jié)果為空,建議Sys.setenv(CURL_CA_BUNDLE=file.path(Sys.getenv("R_HOME"),"/etc/curl-ca-bundle.crt"))打開(kāi)驗(yàn)證。
可以從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設(shè)置環(huán)境變量http_proxy,ftp_proxy
