R irreproducibility check for package R

I came across an interesting error when checking the package vector, whether they need to be installed. Requiring and offloading the lme4 namespace gives an error the second time it is executed, but only when some other packages are checked in a specific order.

isInstalled <- function(package) # is a package installed and usable? { loaded <- package %in% .packages() out <- requireNamespace(package, quietly=F) if(!loaded) try(unloadNamespace(package), silent=F) out } isInstalled("car") # All return TRUE isInstalled("nnet") isInstalled("pbkrtest") isInstalled("lme4") isInstalled("nloptr") isInstalled("lme4") # FALSE (only after commands above) # no such symbol NLoptR_Optimize in package C:/__Rlibrary/nloptr/libs/x64/nloptr.dll library(nloptr) # now fails, too # Problem solved if nnet is checked before car (but not again after car) 

Am I doing something wrong in isInstalled ?

This may be due to the structure of the car dependencies. Simplified version: simplified car dependency diagram

 #install.packages(c("miniCRAN","igraph")) d <- miniCRAN::makeDepGraph(c("car", "nnet", "pbkrtest", "lme4","nloptr"), suggests=FALSE) plot(d) # for full dependency graph 
+5
source share
1 answer

R is not really designed to load and unload packages as desired. You can do this, but success is not guaranteed (although this often happens).

From ?detach :

If a package has a namespace, detaching it by default does not unload the namespace (and may not even work with unload = TRUE), and detaching does not unload any dynamically loaded compiled code (DLL) at all. In addition, registered S3 methods from the namespace will not be deleted. If you use the library in a package whose namespace is loaded, it adds the export of the already loaded namespace. Therefore, disconnecting and reinstalling the package may not update some or all of the components of the package and is impractical.

From ?devtools::unload :

This function attempts to completely unload a package, including unloading its namespace, deleting S4 class definitions, and unloading any loaded DLLs. Unfortunately, S4 classes are not really designed for clean unloading, so we need to manually modify the class dependency graph to make it work - this works on the cases for which we tested, but there may be others. Likewise, automatic unloading of DLLs is best checked for simple scripts (especially using useDynLib (pkgname) and may not work in other cases. If you encounter a failure, please write an error report to http://github.com/hadley / devtools / issues .

+7
source

Source: https://habr.com/ru/post/1269903/


All Articles