R及R包安裝、更新、卸載、刪除

更新R

version     #查看現(xiàn)有R版本
install.packages("installr")
library("installr")
updateR()   
#安裝并加載installr包,并調(diào)用里面的函數(shù)updateR()

更新R包

  • 更新所有R包
    如果用的是Rstudio,要更新library里所有包,一種方法是點(diǎn)擊Tools,選擇Check for Package Updates即可。 另外也可使用update.packages()函數(shù)實(shí)現(xiàn)。
#檢查library里哪些包有更新版本
old.packages()
#更新library里所有包
update.packages()
  • 更新指定R包
    如果只想更新特定的包呢?再使用一次install.packages()就行了。也可以使用devtools::update_packages()函數(shù),這個包默認(rèn)會檢查指定包的依賴關(guān)系,并詢問是否需要更新依賴包的版本。
packageVersion("ggplot2")    #查看指定R包版本
install.packages("ggplot2")
#或
devtools::update_packages()

安裝R包

R包可以存放在不同的倉庫(repository)里,最常見的repo包括CRAN, github,以及生物er必需的Bioconductor。

  • R包安裝途徑一:從CRAN安裝。安裝前建議更改鏡像地址,若使用Rstudio,直接在Tools--global options--Packages里選擇即可。
getOption("repo")    #查看當(dāng)前鏡像,我把默認(rèn)鏡像改成了中科大的。
installed.packages()   #查看已安裝包
install.packages("packageName")    
  • R包安裝途徑二:從github安裝。從GitHub上安裝R包也有兩種方法:
    1)用devtoolsinstall_github()函數(shù)安裝。這種方法必須提供包名和對應(yīng)的GitHub倉庫名。
remotes::install_github("ropensci/rotl")     #github倉庫名/包名

2)用githubinstall包安裝。

install.packages("githubinstall")
library("githubinstall")
install_github('packageName')
  • R包安裝途徑三:從Bioconductor安裝。注意用以前的source命令安裝時(shí)會出現(xiàn)warning message:
source("https://bioconductor.org/biocLite.R")
Bioconductor version 3.8 (BiocInstaller 1.32.1), ?biocLite for help
Warning message:
'BiocInstaller' and 'biocLite()' are deprecated, use the 'BiocManager' CRAN package instead. 

因?yàn)锽ioconductor在R3.5版本以后已經(jīng)換用BiocManager()來安裝R包啦!

install.packages("BiocManager")
BiocManager::install("packageName", version = "3.2")   #用雙冒號起到臨時(shí)加載包,而不用library(),同時(shí)可以指定使用某包中的函數(shù)。

一個更新時(shí)的警告

在安裝R packagedplyr時(shí)遇到這么一個警告:

Warning message:
As of rlang 0.4.0, dplyr must be at least version 0.8.0.
x dplyr 0.7.8 is too old for rlang 0.4.4.
i Please update dplyr to the latest version.
i Updating packages on Windows requires precautions:
  <https://github.com/jennybc/what-they-forgot/issues/62> 

issue/62說的是什么呢?我好奇地打開看了看,這一看倒把我整蒙了,安裝、更新包的時(shí)候必須關(guān)閉所有已加載的R對話??

Please note that on Windows, it is very important to quit or restart all R processes before upgrading , because if any R process has loaded, it will keep the .dll file open and the installer will not be able to overwrite .dll. The error message when this happens is very easy to overlook, and the new version will be partially installed: the package description and R code will be updated but the compiled code (in .dll) will not.

也就是說,如果在當(dāng)前會話中某些.dll文件(動態(tài)鏈接庫文件)已經(jīng)被加載了,而你接下來要安裝或更新的包剛好包含了這些.dll文件,那么這些文件將不會被修改,這可能導(dǎo)致包的使用出現(xiàn)問題。
如果想要卸載特定加載的包,可以直接用detach()指定,以Rcpp為例:

detach("package:Rcpp", unload=TRUE)    
#detach: remove it from the search() path of available R objects; unload=T: to attempt to unload the namespace when a package is being detached.

如果要卸載所有已加載的包,可參考https://stackoverflow.com/questions/7505547/detach-all-packages-while-working-in-r 中的回答:

lapply(names(sessionInfo()$loadedOnly), require, character.only = TRUE)
invisible(lapply(paste0('package:', names(sessionInfo()$otherPkgs)), detach, character.only=TRUE, unload=TRUE, force=TRUE))

更簡單的方法,在Rstudio中選擇control+shift+F10,即可重啟R session。

刪除R包

remove.packages(pkgs, lib)




參考資料

[1] R語言包的安裝
[2] 不用biocLite安裝Bioconductor包
[3] R中雙冒號的作用?
[4] https://github.com/rstats-wtf/what-they-forgot/issues/62
[5] https://stackoverflow.com/questions/7505547/detach-all-packages-while-working-in-r
[6] https://stackoverflow.com/questions/44341788/difference-between-loading-and-attaching-in-r

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容