Rstudio server升级后,原先R包不兼容,如何快速解决

2020-12-01  本文已影响0人  Kevin_Hhui

考虑到当前服务器3.6.1的R版本可能不太符合后续生信分析的需求,因此,师兄将R升级到4.0
但是随之而来也有一个问题,就是原先已经安装的R包几乎都出现了不兼容的现象,提示需要重新安装。
如下:

library(devtools)
Loading required package: usethis
Error: package or namespace load failed for ‘usethis’:
package ‘usethis’ was installed before R 4.0.0: please re-install it

一开始,我想尝试新建一个新的库目录去解决,原因是我知道报错是因为R可能正在从r3.6.1库中读取包,而这正是导致错误的可能原因
方法如下:

##检查.libPaths
.libPaths()
[1] "/home/gzucm04/R/x86_64-pc-linux-gnu-library/3.6" "/usr/local/lib/R/site-library"                  
[3] "/usr/lib/R/site-library"                         "/usr/lib/R/library"

##但是依旧无效
library(devtools, lib.loc=.libPaths()[1])
Error: package ‘usethis’ 1.5.1 was found, but >= 1.6.0 is required by ‘devtools’

我也去Google搜索看看别人是如何解决的,三个比较好的帖子:
https://github.com/conda-forge/r-base-feedstock/issues/129
https://github.com/conda-forge/r-base-feedstock/pull/65
https://stackoverflow.com/questions/63390194/package-xxx-was-installed-before-r-4-0-0-please-re-install-it

我尝试了别人提出的可能的解决办法(但是这种方法对我来说依旧无效):

.libPaths(R.home("/home/gzucm04/R/x86_64-pc-linux-gnu-library/3.6" "/usr/local/lib/R/site-library"))
library(devtools)
Loading required package: usethis
Error: package or namespace load failed for ‘usethis’:
package ‘usethis’ was installed before R 4.0.0: please re-install it
Error: package ‘usethis’ could not be loaded

迫不得已,只能采取终极方法了,卸载全部R包,然后重装。

但是作为懒癌患者,让我一个个R包去卸载,然后重装,是不可能的。
于是,我采取了以下方法(代码还是比较容易看懂):

# 检查包库路径
.libPaths()

# 获取旧包名称
old_packages <- installed.packages(lib.loc = "/home/gzucm04/R/x86_64-pc-linux-gnu-library/3.6")
old_packages <- as.data.frame(old_packages)
list.of.packages <- unlist(old_packages$Package)

# 删除旧R包
remove.packages( installed.packages( priority = "NA" )[,1] )

# 重新安装所有R程序包
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
lapply(list.of.packages,function(x){library(x,character.only=TRUE)})
上一篇下一篇

猜你喜欢

热点阅读