1

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.

kurpav00
  • 138
  • 6
  • 3
    This is a reasonable question, but I don't think it's going to be answerable. R was not designed to be robust in this scenario, and I think it would take quite a lot of digging to figure out exactly what's going wrong and how to work around it. (I'd be happy to be wrong.) – Ben Bolker Aug 02 '23 at 16:23
  • 2
    `detach`, often doesn't and doesn't clearly report that it hasn't detached that leads to this corrupted result, IMHO. – Chris Aug 02 '23 at 16:23
  • It may be worth trying some of the suggestions here to see if you get more reliable results. https://stackoverflow.com/questions/6979917/how-to-unload-a-package-without-restarting-r. – Jon Spring Aug 02 '23 at 18:00
  • @Jon Spring Unfortunately none of the solutions in that post (unloadNamespace, detach_package, devtools::unload, pkgload:::unload) work. – kurpav00 Aug 02 '23 at 18:29

0 Answers0