워라밸 중독자

[R] 사용자 설치 패키지 모두 삭제 본문

코딩, 딱 지금이 시작할때! (코딱지)

[R] 사용자 설치 패키지 모두 삭제

Ufungi 2023. 1. 12. 22:49

R 패키지에 문제가 생겼을 때 단순히 R을 지우고 재설치하곤 합니다. 

그러나 이 방법으로는 해결되지 않는 경우가 많습니다.

이럴 때는 사용자가 설치한 모든 패키지를 삭제하는 방법도 써볼만합니다.

아래에 그 코드를 공유합니다.

# create a list of all installed packages
 ip <- as.data.frame(installed.packages())
 head(ip)
 
# if you use MRO, make sure that no packages in this library will be removed
 ip <- subset(ip, !grepl("MRO", ip$LibPath))
 
# we don't want to remove base or recommended packages either\
 ip <- ip[!(ip[,"Priority"] %in% c("base", "recommended")),]
 
# determine the library where the packages are installed
 path.lib <- unique(ip$LibPath)
 
# create a vector with all the names of the packages you want to remove
 pkgs.to.remove <- ip[,1]
 head(pkgs.to.remove)
 
# remove the packages
 sapply(pkgs.to.remove, remove.packages, lib = path.lib)