From c87879ad32b7acae136214ffca1bc0bad8433240 Mon Sep 17 00:00:00 2001 From: Malte Kyhos Date: Fri, 29 Jul 2022 00:58:25 +0200 Subject: [PATCH 1/2] Initial fix --- R/model.R | 12 +++++++++- tests/testthat/test-model-variables.R | 33 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/R/model.R b/R/model.R index 5d73ae383..dd26d1d43 100644 --- a/R/model.R +++ b/R/model.R @@ -653,7 +653,17 @@ variables <- function() { } assert_stan_file_exists(self$stan_file()) if (is.null(private$variables_) && file.exists(self$stan_file())) { - private$variables_ <- model_variables(self$stan_file(), self$include_paths(), allow_undefined = private$using_user_header_) + private$variables_ <- model_variables( + stan_file = self$stan_file(), + include_paths = { + if (length(self$exe_file()) > 0 && file.exists(self$exe_file())) { + self$include_paths() + } else { + private$precompile_include_paths_ + } + }, + allow_undefined = private$using_user_header_ + ) } private$variables_ } diff --git a/tests/testthat/test-model-variables.R b/tests/testthat/test-model-variables.R index 889bd3337..680bb7cdb 100644 --- a/tests/testthat/test-model-variables.R +++ b/tests/testthat/test-model-variables.R @@ -108,3 +108,36 @@ test_that("$variables() errors on no stan_file", { fixed = TRUE ) }) + +test_that("$variables() works with #includes, both pre and post compilation.", { + + data_code <- " + data { + int N; + } + " + model_code <- " + #include data.stan + parameters { + vector[N] y; + } + model { + y ~ std_normal(); + } + " + + model_file <- write_stan_file(code = model_code) + data_file <- write_stan_file(code = data_code, basename = "data.stan") + + mod <- cmdstan_model( + stan_file = model_file, + include_paths = dirname(data_file), + compile = FALSE + ) + + vars_pre <- mod$variables() + mod$compile() + vars_post <- mod$variables() + + expect_equal(vars_pre, vars_post) +}) From 86557d88919b3642e465bd6df8ec7659a1ec5c70 Mon Sep 17 00:00:00 2001 From: Malte Kyhos Date: Fri, 29 Jul 2022 01:12:03 +0200 Subject: [PATCH 2/2] Move logic in () query method --- R/model.R | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/R/model.R b/R/model.R index dd26d1d43..67d4bffe7 100644 --- a/R/model.R +++ b/R/model.R @@ -265,7 +265,11 @@ CmdStanModel <- R6::R6Class( invisible(self) }, include_paths = function() { - private$include_paths_ + if (length(self$exe_file()) > 0 && file.exists(self$exe_file())) { + return(private$include_paths_) + } else { + return(private$precompile_include_paths_) + } }, code = function() { if (length(private$stan_code_) == 0) { @@ -655,13 +659,7 @@ variables <- function() { if (is.null(private$variables_) && file.exists(self$stan_file())) { private$variables_ <- model_variables( stan_file = self$stan_file(), - include_paths = { - if (length(self$exe_file()) > 0 && file.exists(self$exe_file())) { - self$include_paths() - } else { - private$precompile_include_paths_ - } - }, + include_paths = self$include_paths(), allow_undefined = private$using_user_header_ ) }