Skip to content

Commit 9ad2918

Browse files
authored
Merge pull request #682 from MKyhos/680-mod-variables-w-includes-precompile
`mod$variables` works w includes in precompile state (fix #680)
2 parents 22b391e + 86557d8 commit 9ad2918

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

R/model.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,11 @@ CmdStanModel <- R6::R6Class(
265265
invisible(self)
266266
},
267267
include_paths = function() {
268-
private$include_paths_
268+
if (length(self$exe_file()) > 0 && file.exists(self$exe_file())) {
269+
return(private$include_paths_)
270+
} else {
271+
return(private$precompile_include_paths_)
272+
}
269273
},
270274
code = function() {
271275
if (length(private$stan_code_) == 0) {
@@ -653,7 +657,11 @@ variables <- function() {
653657
}
654658
assert_stan_file_exists(self$stan_file())
655659
if (is.null(private$variables_) && file.exists(self$stan_file())) {
656-
private$variables_ <- model_variables(self$stan_file(), self$include_paths(), allow_undefined = private$using_user_header_)
660+
private$variables_ <- model_variables(
661+
stan_file = self$stan_file(),
662+
include_paths = self$include_paths(),
663+
allow_undefined = private$using_user_header_
664+
)
657665
}
658666
private$variables_
659667
}

tests/testthat/test-model-variables.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,36 @@ test_that("$variables() errors on no stan_file", {
108108
fixed = TRUE
109109
)
110110
})
111+
112+
test_that("$variables() works with #includes, both pre and post compilation.", {
113+
114+
data_code <- "
115+
data {
116+
int N;
117+
}
118+
"
119+
model_code <- "
120+
#include data.stan
121+
parameters {
122+
vector[N] y;
123+
}
124+
model {
125+
y ~ std_normal();
126+
}
127+
"
128+
129+
model_file <- write_stan_file(code = model_code)
130+
data_file <- write_stan_file(code = data_code, basename = "data.stan")
131+
132+
mod <- cmdstan_model(
133+
stan_file = model_file,
134+
include_paths = dirname(data_file),
135+
compile = FALSE
136+
)
137+
138+
vars_pre <- mod$variables()
139+
mod$compile()
140+
vars_post <- mod$variables()
141+
142+
expect_equal(vars_pre, vars_post)
143+
})

0 commit comments

Comments
 (0)