donaldRwilliams / chkptstanr

Checkpoint Stan R
https://donaldrwilliams.github.io/chkptstanr/
Other
9 stars 5 forks source link

'stan_code_path' not found when resuming fitting #8

Open peclayson opened 2 years ago

peclayson commented 2 years ago

I think I might be losing my mind. I'm running into the following error when running the vignette on either Mac or Linux.

Error in cmdstanr::cmdstan_model(stan_file = stan_code_path, cpp_options = list(stan_threads = TRUE)) : 
  object 'stan_code_path' not found

The model from the vignette runs and finishes fine as long as I don't terminate it during fitting. However, if I stop during fitting and restart the fitting, I run into the error above.

The only thing that I can think of that I'm doing differently than the vignette is using Esc or crtl + C to terminate the fitting process. I don't believe (but I don't know for sure) that using Esc is functionally different than the Rstudio stop button... but the button doesn't show up in RStudio for me during the model fitting process.

Any recommendations on what I could try?

I will try not to post any more issues for the rest of the month... I just really want to use your package :)

Thanks, Peter

sessionInfo()

R version 4.1.2 (2021-11-01)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.4

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] brms_2.17.0          Rcpp_1.0.8.3         cmdstanr_0.5.2       chkptstanr_0.1.1    
[5] rstan_2.21.5         ggplot2_3.3.6        StanHeaders_2.21.0-7

loaded via a namespace (and not attached):
  [1] TH.data_1.1-0        minqa_1.2.4          colorspace_2.0-3     ellipsis_0.3.2      
  [5] ggridges_0.5.3       estimability_1.3     markdown_1.1         base64enc_0.1-3     
  [9] farver_2.1.0         DT_0.21              fansi_1.0.3          mvtnorm_1.1-3       
 [13] bridgesampling_1.1-2 codetools_0.2-18     splines_4.1.2        knitr_1.39          
 [17] shinythemes_1.2.0    bayesplot_1.8.1      projpred_2.1.1       jsonlite_1.8.0      
 [21] nloptr_2.0.3         shiny_1.7.1          compiler_4.1.2       emmeans_1.7.4-1     
 [25] backports_1.4.1      assertthat_0.2.1     Matrix_1.3-4         fastmap_1.1.0       
 [29] cli_3.3.0            later_1.3.0          htmltools_0.5.2      prettyunits_1.1.1   
 [33] tools_4.1.2          igraph_1.2.11        coda_0.19-4          gtable_0.3.0        
 [37] glue_1.6.2           reshape2_1.4.4       dplyr_1.0.9          posterior_1.2.1     
 [41] vctrs_0.4.1          nlme_3.1-153         crosstalk_1.2.0      tensorA_0.36.2      
 [45] xfun_0.31            stringr_1.4.0        ps_1.7.0             lme4_1.1-29         
 [49] mime_0.12            miniUI_0.1.1.1       lifecycle_1.0.1      gtools_3.9.2        
 [53] klippy_0.0.0.9500    MASS_7.3-54          zoo_1.8-9            scales_1.2.0        
 [57] colourpicker_1.1.1   promises_1.2.0.1     Brobdingnag_1.2-7    parallel_4.1.2      
 [61] sandwich_3.0-1       inline_0.3.19        shinystan_2.6.0      gamm4_0.2-6         
 [65] yaml_2.3.5           gridExtra_2.3        loo_2.4.1            stringi_1.7.6       
 [69] dygraphs_1.1.1.6     checkmate_2.0.0      boot_1.3-28          pkgbuild_1.3.1      
 [73] rlang_1.0.2          pkgconfig_2.0.3      matrixStats_0.61.0   distributional_0.3.0
 [77] evaluate_0.15        lattice_0.20-45      purrr_0.3.4          rstantools_2.1.1    
 [81] htmlwidgets_1.5.4    cowplot_1.1.1        processx_3.5.3       tidyselect_1.1.2    
 [85] plyr_1.8.6           magrittr_2.0.3       R6_2.5.1             generics_0.1.2      
 [89] multcomp_1.4-18      DBI_1.1.2            pillar_1.7.0         withr_2.5.0         
 [93] mgcv_1.8-38          xts_0.12.1           survival_3.2-13      abind_1.4-5         
 [97] tibble_3.1.7         crayon_1.5.1         utf8_1.2.2           rmarkdown_2.14      
[101] grid_4.1.2           data.table_1.14.2    callr_3.7.0          threejs_0.3.3       
[105] digest_0.6.29        xtable_1.8-4         httpuv_1.6.5         RcppParallel_5.1.5  
[109] stats4_4.1.2         munsell_0.5.0        shinyjs_2.1.0       
peclayson commented 2 years ago

This issue gave me an excuse to tinker with R code in a way that I haven't before. I typically write packages in MATLAB, not R. So, debugging libraries is new to me. I also read #3, which had similar issue.

I couldn't see in the script how stan_code_path would be set when isFALSE(check_for_model("model_threads.exe", path)) is FALSE, because it finds the model file.

I changed ....

if (isFALSE(check_for_model("model.stan", path))) {
        stan_code_path <- cmdstanr::write_stan_file(code = stan_code, 
            dir = paste0(path, "/stan_model"), basename = "model")
    }

to

if (isFALSE(check_for_model("model.stan", path))) {
        stan_code_path <- cmdstanr::write_stan_file(code = stan_code, 
            dir = paste0(path, "/stan_model"), basename = "model")
    } else if (check_for_model("model.stan", path) & isFALSE(exists("stan_code_path"))) {
        stan_code_path <- file.path(path, "stan_model", "model.stan")
    }

That seems to make things work for me. It's possible I am missing something on a larger scale than this one script though.

Again, I wonder whether having to use ctrl + C over the stop button in Rstudio has something to do with me getting this error. I'm not sure.

GGLuca commented 2 years ago

I am on Win10 and hitting the same issue. Rerunning the vignette model gives me:

Error in cmdstanr::cmdstan_model(stan_file = stan_code_path, cpp_options = list(stan_threads = TRUE)) : object 'stan_code_path' not found

Tinkering with different path settings has not helped.

I tried the same vignette code under WSL2/Ubuntu and the issue still persists with package version 0.1.1

Edit: Thanks for the package! I am sure it will help me insanely...

George

venpopov commented 8 months ago

This issue gave me an excuse to tinker with R code in a way that I haven't before. I typically write packages in MATLAB, not R. So, debugging libraries is new to me. I also read #3, which had similar issue.

I couldn't see in the script how stan_code_path would be set when isFALSE(check_for_model("model_threads.exe", path)) is FALSE, because it finds the model file.

I changed ....

if (isFALSE(check_for_model("model.stan", path))) {
        stan_code_path <- cmdstanr::write_stan_file(code = stan_code, 
            dir = paste0(path, "/stan_model"), basename = "model")
    }

to

if (isFALSE(check_for_model("model.stan", path))) {
        stan_code_path <- cmdstanr::write_stan_file(code = stan_code, 
            dir = paste0(path, "/stan_model"), basename = "model")
    } else if (check_for_model("model.stan", path) & isFALSE(exists("stan_code_path"))) {
        stan_code_path <- file.path(path, "stan_model", "model.stan")
    }

That seems to make things work for me. It's possible I am missing something on a larger scale than this one script though.

Again, I wonder whether having to use ctrl + C over the stop button in Rstudio has something to do with me getting this error. I'm not sure.

This worked for me as well. Is this project alive? It is an awesome idea, but without this fix it just doesn't work.