Rstan - How to prevent "Recompiling to avoid crashing R session"

Hi,

I am currently creating an R package that internally makes use of rstan model. As the rstan part is a optional extra we have rstan as a “suggests” package and opt to compile the model on the fly.

Unfortunately it appears that the model attempts to be re-compiled every time. From a similar question it was suggested that this occurs when the model is garbage collected so I attempt to circumvent this by storing the model in a internal environment within the package so that it persists between calls e.g.

R/mcmc.R

STAN_ENV <- new.env()

get_stan_model <- function() {
    model_file <- if (file.exists("inst/stan/MMRM.stan")) {
        "inst/stan/MMRM.stan"
    } else if (file.exists("stan/MMRM.stan")) {
        "stan/MMRM.stan"
    } else {
        system.file("stan/MMRM.stan", package = "rbmi")
    }
    STAN_ENV$model <- rstan::stan_model(
        file = model_file,
        auto_write = TRUE
    )
    STAN_ENV$model
}

However this hasn’t worked and rstan attempts to recompile each time the function is called. For example during our unit tests:

image

Any idea what I am doing wrong or can change to fix this ?

Session info for reference:

> sessionInfo()
R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default


locale:
[1] LC_COLLATE=German_Switzerland.utf8  LC_CTYPE=German_Switzerland.utf8    LC_MONETARY=German_Switzerland.utf8
[4] LC_NUMERIC=C                        LC_TIME=German_Switzerland.utf8    

time zone: Europe/Paris
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tibble_3.2.1   dplyr_1.1.4    rbmi_1.2.6     testthat_3.2.1

loaded via a namespace (and not attached):
 [1] gtable_0.3.4       ggplot2_3.4.4      QuickJSR_1.0.9     htmlwidgets_1.6.4  devtools_2.4.5     remotes_2.4.2.1   
 [7] inline_0.3.19      lattice_0.21-9     vctrs_0.6.5        tools_4.3.2        Rdpack_2.6         generics_0.1.3    
[13] stats4_4.3.2       parallel_4.3.2     mmrm_0.3.7         fansi_1.0.6        pkgconfig_2.0.3    Matrix_1.6-1.1    
[19] checkmate_2.3.1    desc_1.4.3         assertthat_0.2.1   RcppParallel_5.1.7 lifecycle_1.0.4    compiler_4.3.2    
[25] stringr_1.5.1      brio_1.1.4         munsell_0.5.0      codetools_0.2-19   httpuv_1.6.13      htmltools_0.5.7   
[31] usethis_2.2.2      later_1.3.2        pillar_1.9.0       urlchecker_1.0.1   ellipsis_0.3.2     cachem_1.0.8      
[37] StanHeaders_2.32.5 sessioninfo_1.2.2  nlme_3.1-163       mime_0.12          rstan_2.32.5       tidyselect_1.2.0  
[43] digest_0.6.34      stringi_1.8.3      purrr_1.0.2        rprojroot_2.0.4    fastmap_1.1.1      grid_4.3.2        
[49] colorspace_2.1-0   cli_3.6.2          magrittr_2.0.3     loo_2.6.0          pkgbuild_1.4.3     utf8_1.2.4        
[55] withr_2.5.2        scales_1.3.0       promises_1.2.1     backports_1.4.1    matrixStats_1.2.0  gridExtra_2.3     
[61] memoise_2.0.1      shiny_1.8.0        rbibutils_2.2.16   miniUI_0.1.1.1     profvis_0.3.8      rlang_1.1.3       
[67] Rcpp_1.0.12        xtable_1.8-4       glue_1.7.0         pkgload_1.3.3      rstudioapi_0.15.0  jsonlite_1.8.8    
[73] R6_2.5.1           fs_1.6.3 

Package source code (encase anyone wanted to look, as of the time of writing please note that this code is on a new feature branch as its not merged into main yet):