I was unpleasantly surprised to find out that if I freshly install and R package and it gets corrupted, it cannot be reinstalled while the R session is running, as demonstrated here:
# create a new folder for package installation, just for sake of cleanliness
dir.create("libraries")
.libPaths("libraries")
# install a package, here sigclust as an example
install.packages("sigclust",lib="libraries")
# everything works fine
library(sigclust)
detach("package:sigclust",unload=T)
# now simulate package damage, for example by overwriting the contents of the file libraries/sigclust/R/sigclust.rdb
# so for example open file in notepad and delete the contents manually
system2("open","libraries/sigclust/R/sigclust.rdb")
# does not load, which was expected
library(sigclust)
Error: package or namespace load failed for 'sigclust' in get0(".packageName", env, inherits = FALSE):
lazy-load database 'C:/Users/Paul/Documents/libraries/sigclust/R/sigclust.rdb' is corrupt
In addition: Warning message:
In get0(".packageName", env, inherits = FALSE) :
internal error -3 in R_decompress1
# remove and reinstall package
remove.packages("sigclust",lib="libraries")
install.packages("sigclust",lib="libraries")
# still does not work, even after reinstall
library(sigclust)
Error: package or namespace load failed for 'sigclust' in get0(".packageName", env, inherits = FALSE):
lazy-load database 'C:/Users/Paul/Documents/libraries/sigclust/R/sigclust.rdb' is corrupt
In addition: Warning message:
In get0(".packageName", env, inherits = FALSE) :
internal error -3 in R_decompress1
So my questions are: How is it possible that removing and reinstalling a package does not fix its corruption? Is there some hidden cache that keeps reinstalling the corrupted version of the sigclust.rdb file instead of downloading a fresh copy from the online repository? How to purge it?
I have tried purging the temp folder:
file.remove(list.files(tempdir(),full.names=T,recursive=T))
But it does not help. Except this, I was unable to find any clues on why this is happening.
Note: I know restarting the R session does solve the issue, but I would like to know a way how to purge whatever is keeping the corrupt version of the package in the memory on the fly, without restarting R.