Hello nimble-users,
would like to ask for input on the following:
I have a model that is inside a function that run in a "loop-ed" fashion where all files created by nimble goes into a dirName randomly created prior to setting up model. At the end of this function my preferred plan is to delete the folder and all related files then start the whole process again in the next loop. As a side effect parts of the files/objects created appear to "stuck" and prevents the model compilation in the subsequent loop. Have tried clearCompiled() but no success. here is the process as inside the function:
looping_function <- function(...) {
#some calculations here....
model_code <- nimbleCode({})
constants <- list(#constants here")
obs_data <- list(#constants here")
inits <- list(#constants here")
model_nimb <- nimbleModel(model_code,data = obs_data,inits = inits,constants = constants,buildDerivs = TRUE)
set.seed(runif(1,10,10000))
nimblefolder<-paste0("NM","1",password(n = 5, numbers = TRUE, case = TRUE,special = F))
dir.create(nimblefolder)
Cmodel <- compileNimble(model_nimb,dirName=file.path(getwd(), nimblefolder))
conf <- configureMCMC(Cmodel)
mcmc <- buildMCMC(conf)
Cmcmc <- compileNimble(mcmc, project = Cmodel,dirName=file.path(getwd(), nimblefolder))
final <- runMCMC(Cmcmc, niter = 5000, nburnin =500, samplesAsCodaMCMC = TRUE)
#some calculations here....
clearCompiled(Cmcmc)
clearCompiled(Cmodel)
sapply(list.files(file.path(getwd(), nimblefolder), "\\.dll$", full.names = TRUE), function(dll) {
try(dyn.unload(dll), silent = TRUE)})
unlink(file.path(getwd(), nimblefolder), recursive = T)
gc()
invisible(return(NULL))
}
and the loop over the data frame intervals rows:
for (ii in 1:nrow(intervals)) {
x <- intervals[ii, ]
looping_function (...)
}
the 1st loop runs fine, but then getting this error message as the second loop around proceeds, the break happens at (as far as I can tell):
Cmodel <- compileNimble(model_nimb,dirName=file.path(getwd(), nimblefolder))
error message:
Error in FUN(X[[i]], ...) :
no such symbol RNimble_Ptr_ManualFinalizer in package C:/.../app/shiny/NM2FYWXh/dynamicRegistrations_09_05_05_32_27.dll
Calls: <Anonymous> ... $ -> $.DLLInfo -> getNativeSymbolInfo -> lapply -> FUN
NM2FYWXh in this case is the name of the folder that was created in the 1st loop that gets deleted appropriately at the end of loop yet the system "holds on" to dynamicRegistrations_09_05_05_32_27.dll from that 1st loop.
my question is that are there some best practices to do the clean up so all parts and pieces of the model gets removed to prevent the break? Interrupting R session and start a new one is not an option for me.
All the following nimble "components" are subject to change from loop to loop: model_code, constant , obs_data , inits, conf and of course nimblefolder.
appreciate all your thoughts,
thanks,
Andras