Skip to content

Commit 5c2dbbd

Browse files
committed
Merge branch 'master' into laplace-sample
2 parents 467054f + 17678d5 commit 5c2dbbd

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

R/model.R

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,26 +530,24 @@ compile <- function(quiet = TRUE,
530530
cpp_options[["USER_HEADER"]] <- wsl_safe_path(absolute_path(user_header))
531531
stanc_options[["allow-undefined"]] <- TRUE
532532
private$using_user_header_ <- TRUE
533-
}
534-
else if (!is.null(cpp_options[["USER_HEADER"]])) {
535-
if(!is.null(cpp_options[["user_header"]])) {
533+
} else if (!is.null(cpp_options[["USER_HEADER"]])) {
534+
if (!is.null(cpp_options[["user_header"]])) {
536535
warning('User header specified both via cpp_options[["USER_HEADER"]] and cpp_options[["user_header"]].', call. = FALSE)
537536
}
538537

539538
user_header <- cpp_options[["USER_HEADER"]]
540539
cpp_options[["USER_HEADER"]] <- wsl_safe_path(absolute_path(cpp_options[["USER_HEADER"]]))
541540
private$using_user_header_ <- TRUE
542-
}
543-
else if (!is.null(cpp_options[["user_header"]])) {
541+
} else if (!is.null(cpp_options[["user_header"]])) {
544542
user_header <- cpp_options[["user_header"]]
545543
cpp_options[["user_header"]] <- wsl_safe_path(absolute_path(cpp_options[["user_header"]]))
546544
private$using_user_header_ <- TRUE
547545
}
548546

549547

550-
if(!is.null(user_header)) {
548+
if (!is.null(user_header)) {
551549
user_header <- absolute_path(user_header) # As mentioned above, just absolute, not wsl_safe_path()
552-
if(!file.exists(user_header)) {
550+
if (!file.exists(user_header)) {
553551
stop(paste0("User header file '", user_header, "' does not exist."), call. = FALSE)
554552
}
555553
}
@@ -689,8 +687,12 @@ compile <- function(quiet = TRUE,
689687
)
690688
)
691689
if (is.na(run_log$status) || run_log$status != 0) {
692-
stop("An error occured during compilation! See the message above for more information.",
693-
call. = FALSE)
690+
err_msg <- "An error occured during compilation! See the message above for more information."
691+
if (grepl("auto-format flag to stanc", run_log$stderr)) {
692+
format_msg <- "\nTo fix deprecated or removed syntax please see ?cmdstanr::format for an example."
693+
err_msg <- paste(err_msg, format_msg)
694+
}
695+
stop(err_msg, call. = FALSE)
694696
}
695697
if (file.exists(exe)) {
696698
file.remove(exe)
@@ -933,6 +935,28 @@ CmdStanModel$set("public", name = "check_syntax", value = check_syntax)
933935
#'
934936
#' @examples
935937
#' \dontrun{
938+
#'
939+
#' # Example of fixing old syntax
940+
#' # real x[2] --> array[2] real x;
941+
#' file <- write_stan_file("
942+
#' parameters {
943+
#' real x[2];
944+
#' }
945+
#' model {
946+
#' x ~ std_normal();
947+
#' }
948+
#' ")
949+
#'
950+
#' # set compile=FALSE then call format to fix old syntax
951+
#' mod <- cmdstan_model(file, compile = FALSE)
952+
#' mod$format(canonicalize = list("deprecations"))
953+
#'
954+
#' # overwrite the original file instead of just printing it
955+
#' mod$format(canonicalize = list("deprecations"), overwrite_file = TRUE)
956+
#' mod$compile()
957+
#'
958+
#'
959+
#' # Example of removing unnecessary whitespace
936960
#' file <- write_stan_file("
937961
#' data {
938962
#' int N;

man/model-method-format.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters {
2+
real x[3];
3+
}
4+
model {
5+
x ~ normal(0, 1);
6+
}

tests/testthat/test-model-compile.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ test_that("compile errors are shown", {
174174
)
175175
})
176176

177+
test_that("compile suggests using format to fix old syntax", {
178+
stan_file <- testing_stan_file("old_array_syntax")
179+
expect_error(
180+
cmdstan_model(stan_file),
181+
"To fix deprecated or removed syntax please see ?cmdstanr::format for an example.",
182+
fixed = TRUE
183+
)
184+
})
185+
177186
test_that("dir arg works for cmdstan_model and $compile()", {
178187
tmp_dir <- tempdir()
179188
tmp_dir_2 <- tempdir()

0 commit comments

Comments
 (0)