So, one issue that I was having with creating packages for my nimble functions is that they wanted to compile at *load* time. This is not good if I have lots of nimble functions, and users only need one.
To help with this, I created this:
```
memoiseNimbleCompilation <- function(nimbleF) {
# not sure why we need this, but nimble complains otherwise
nimbleF <- nimbleF
compiledFunction <- NULL
maybeCompiledFunction <- function(...) {
if (is.null(compiledFunction)) {
compiledFunction <<- compileNimble(nimbleF)
}
return(compiledFunction(...))
}
return(maybeCompiledFunction)
}
```
You can use it like this:
`theCompiledFunction <- memoiseNimbleCompilation(theUncompiledFunction)`
This makes it so that compilation is "lazy", and gets deferred until the moment a user attempts to call the function. Of course, this only works for nimble functions, but I suppose a similar thing could be arranged for models, or mcmc builds or what have you.