BiocManager無法安裝R包
- 2020 年 3 月 30 日
- 筆記
每次開展R語言線下學習班,都需要重新發幾次:Windows電腦使用Rstudio會有多少錯誤呢 ,雖然大部分同學都是可以根據我們的教程順利解決問題,但是不幸的人各有各的不幸。一般來說就是Windows電腦的中文用戶名需要修改電腦系統的環境變量,R包下載等等。
但是今天有一個學員起初是下載R包無法聯網,所以失敗,根據我們的經驗當然是options(download.file.method = 'libcurl')就輕輕鬆鬆解決啦,不過這次居然是僅僅是解決了R自帶R包下載問題,使用BiocManager仍然是無法安裝R包,如下所示:

當然就得根據關鍵詞去搜索啦!
> BiocManager::install("KEGG.db",ask = F,update = F) 錯誤: Bioconductor version cannot be validated; no internet connection? 此外: Warning messages:
第一次搜索以為是http和https的問題
參考:https://stackoverflow.com/questions/33355444/r-when-trying-to-install-package-internetopenurl-failed
大概意思是讓我修改鏡像把所有的https都替換為http,步驟如下:
- using regular http mirrors instead of https
- update your CA certificate bundle to allow proper certificate validation
- setting the default download method to "libcurl" and see if that helps: options(download.file.method="libcurl")
但是嘗試了,失敗!
第二次嘗試以為是BiocManager的config文件
所以根據搜索結果使用:
config <- readLines("https://bioconductor.org/config.yaml")
這個時候的報錯讓我恍然大悟

其實真正的問題還是在聯網上面!也就是說options(download.file.method = 'libcurl')並沒有完全解決問題。
配置R聯網環境
繼續搜索R聯網環境,這個時候發現了url.method這個配置的解決方案;
options(download.file.method = 'libcurl') options(url.method='libcurl')
果然,現在在Windows電腦裏面R語言的安裝R包和下載文件就OK啦。有趣的是,你每次都需要複製粘貼上面兩行代碼,或者你把它寫入到你的R配置文件哦。
那麼這兩個參數到底是啥區別呢
首先看 download.file.method ,非常複雜:https://stat.ethz.ch/R-manual/R-devel/library/utils/html/download.file.html
Method to be used for downloading files. Current download methods are "internal", "wininet" (Windows only) "libcurl", "wget" and "curl", and there is a value "auto": see 『Details』 and 『Note』. The method can also be set through the option "download.file.method": see options().
然後看,也很複雜:https://stat.ethz.ch/R-manual/R-devel/library/base/html/options.html·
character string: the default method for url. Normally unset, which is equivalent to "default", which is "internal" except on Windows.
接下來就繼續安裝R包吧
使用管理員打開R哦,然後就
options()$repos options()$BioC_mirror options(BioC_mirror="https://mirrors.ustc.edu.cn/bioc/") options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) options()$repos options()$BioC_mirror # https://bioconductor.org/packages/release/bioc/html/GEOquery.html if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install("KEGG.db",ask = F,update = F) BiocManager::install(c("GSEABase","GSVA","clusterProfiler" ),ask = F,update = F) BiocManager::install(c("GEOquery","limma","impute" ),ask = F,update = F) BiocManager::install(c("org.Hs.eg.db","hgu133plus2.db" ),ask = F,update = F)
如果你還報錯,就看看周圍有沒有大神幫你解決吧!