重新安装老版R下已安装的R包

Linux, R

Posted by dulunar on May 19, 2021 | 访问量:

前言

Linux上的R是3.6.2版本的,我另外使用conda也搭建了一个R_env,以防止和以前安装的混了,最近自动更新了R_env的R到4.0.3版本,导致原来的R_env下的R包出现问题,提示重装:

Package XXX was installed before R 4.0.1: please re-install it

解决方案

R都更新了,那就把以前安装的尽量重装:

# 指定R_env下的R包的安装路径
> .libPaths("/home/luna/Desktop/Software/miniconda3/envs/Rlib/lib/R/library")

> .libPath()

# 清楚窗口抓取失败
> chooseCRANmirror(graphics=F)
 
# 抓取已安装的包的名字
> old_packages <- installed.packages()
> old_packages <- as.data.frame(old_packages)
> list.of.packages <- unlist(old_packages$Package)

# 清楚已安装的包 
> remove.packages( installed.packages( priority = "NA" )[,1] )

# 重新安装所有的包
> 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)})