I am planning to include stan models in R package and have a couple of questions:- How can I make sure that the binary versions of the package come along with a pre-compiled stan binary?
- Is there a recommended way to do this?
setwd("/path/to/inst/include/example-models")
models <- dir(getwd(), pattern = "stan$", full.names=TRUE, recursive = TRUE)
library(paralell)
options(mc.cores = 8) # or however many cores you have available
output <- mclapply(models, mc.silent = TRUE,
FUN = function(m) try(rstan::stan_model(m, auto_write = TRUE)))
- Should I use the command-line interface or the rstan interface?
- And which options make 100% sure that stan does not output anything (the open_progress=FALSE does not seem to be sufficient)
However, I did have in mind to deploy my own library, which uses some stan models behind the scenes to do its work. So from what I get from you, is that I could for example setup a script which compiles my model likestan_model <- stan("my_internal_library_model.stan", chains=0)
Is removing the compiled Stan object enough in order to unload the dll (which I assume each model represents)?
I just did what you suggested, but somehow stan_demo still wants to recompile things even though there is a rda file in place for each model. Any idea what went wrong and what I can do?
if(!file.exists(file.rda) ||
(mtime.rda <- file.info(file.rda)$mtime) < mtime ||
mtime.rda < as.POSIXct(packageDescription("rstan")$Date) ||
mtime.rda > rstan_load_time ||
!is(obj <- readRDS(file.rda), "stanmodel") ||
!is_sm_valid(obj) ||
!is.null(writeLines(obj@model_code, con = tf <- tempfile())) ||
md5 != tools::md5sum(tf)) {
# do nothing
}
else return(invisible(obj))