What is the reason "Error in .C (" unlock solver ")" Error in deSolve package?

I used the deSolve package in the MCMC algorithm to evaluate parameters in ODE and wrote the functions used in the solver in C to speed up the algorithm. Sometimes, but not always, I get an Error in .C("unlock solver") error when running the ode function. I can successfully compile and link C files with commands

 system("R CMD SHLIB [insert-file-path]") dyn.load("[dll-file-path]") 

but when I try to solve the ODE using a dll file, an error occurs. Then even when I run a simple script like the one below, I get the same error. I think that the problem is related to the use of compiled code, but I don’t know how and cannot find links to this error.

 > require(deSolve) > initVal <- c(y=1) > times <- seq(0, 1, 0.001) > parms <- c(k=1) > model1 <- function(t, y, parms){ + with(as.list(c(y, parms)),{ + dy <- -k*y; + list(c(dy)) + }) + } > out <- ode(y=initVal, times=times, parms=parms, func=model1) Error in .C("unlock_solver") : "unlock_solver" not resolved from current namespace (deSolve) 

Partial Solution If I restart R and load the DLL using the dyn.load function dyn.load , but do not compile the code, the ode function ode run without error. This fixes my problem, but I still don't know why.

+4
source share
2 answers

Edit: REAL solution from Thomas Petzoldt in the R help list:

"[Error] occurs if the package with the deSolve package is loaded after the compiled model ... The solution is to load deSolve before [before loading the DLL], ideally at the very beginning of your script, and at least before that loading / unloading the DLL /. so "

If this does not work, the following may be (old answer):

I found a somewhat inelegant solution.

The problem is that the "unlock_solver" function in deSolve somehow does not execute correctly. You can download and reload the entire deSolve.so file, rather than restart R.

For this you can use something like:

 require(deSolve) # encounter error library.dynam.unload("deSolve", libpath=paste(.libPaths()[1], "//deSolve", sep="")) library.dynam("deSolve", package="deSolve", lib.loc=.libPaths()[1]) 

You will need to replace ".libPaths () [1]" wherever you set deSolve if it is not in the first element of your .libPaths variable.

This is something like a sledgehammer. I sent a request to the r-help list, asking if there is a way to change where R is looking for "unlock_solver", or just unload / reload the deSolve part.

+5
source

Make sure you have the following packages installed (at the beginning of your script) to compile the DLL file.

 packages <- c("deSolve","coda", "adaptMCMC") if(require(packages)){ install.packages(packages,dependencies = T) } ppp <- lapply(packages,require,character.only=T) 

First delete the current .dll file in your wdir

 c_compile <- "your_c_file" dyn.unload(paste0(c_compile,".dll")) # unload dll (Windows only) 

Then compile the C file and the .dll

 system(paste0("R CMD SHLIB ",c_compile,".c")) dyn.load(paste0(c_compile,".dll"))# Load dll (Windows only) 
0
source

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


All Articles