From 7a9488236d4090b5f1efda07f24496f24790d6e5 Mon Sep 17 00:00:00 2001 From: jgabry Date: Wed, 13 Sep 2023 11:49:04 -0600 Subject: [PATCH 1/4] suggest format method for auto-formatting --- R/model.R | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/R/model.R b/R/model.R index 14871b91..ec822c28 100644 --- a/R/model.R +++ b/R/model.R @@ -529,26 +529,24 @@ compile <- function(quiet = TRUE, cpp_options[["USER_HEADER"]] <- wsl_safe_path(absolute_path(user_header)) stanc_options[["allow-undefined"]] <- TRUE private$using_user_header_ <- TRUE - } - else if (!is.null(cpp_options[["USER_HEADER"]])) { - if(!is.null(cpp_options[["user_header"]])) { + } else if (!is.null(cpp_options[["USER_HEADER"]])) { + if (!is.null(cpp_options[["user_header"]])) { warning('User header specified both via cpp_options[["USER_HEADER"]] and cpp_options[["user_header"]].', call. = FALSE) } user_header <- cpp_options[["USER_HEADER"]] cpp_options[["USER_HEADER"]] <- wsl_safe_path(absolute_path(cpp_options[["USER_HEADER"]])) private$using_user_header_ <- TRUE - } - else if (!is.null(cpp_options[["user_header"]])) { + } else if (!is.null(cpp_options[["user_header"]])) { user_header <- cpp_options[["user_header"]] cpp_options[["user_header"]] <- wsl_safe_path(absolute_path(cpp_options[["user_header"]])) private$using_user_header_ <- TRUE } - if(!is.null(user_header)) { + if (!is.null(user_header)) { user_header <- absolute_path(user_header) # As mentioned above, just absolute, not wsl_safe_path() - if(!file.exists(user_header)) { + if (!file.exists(user_header)) { stop(paste0("User header file '", user_header, "' does not exist."), call. = FALSE) } } @@ -690,8 +688,12 @@ compile <- function(quiet = TRUE, ) ) if (is.na(run_log$status) || run_log$status != 0) { - stop("An error occured during compilation! See the message above for more information.", - call. = FALSE) + err_msg <- "An error occured during compilation! See the message above for more information." + if (grepl("auto-format flag to stanc", run_log$stderr)) { + format_msg <- "\nTo fix deprecated or removed syntax please see ?cmdstanr::format for an example." + err_msg <- paste(err_msg, format_msg) + } + stop(err_msg, call. = FALSE) } if (file.exists(exe)) { file.remove(exe) @@ -929,6 +931,21 @@ CmdStanModel$set("public", name = "check_syntax", value = check_syntax) #' #' @examples #' \dontrun{ +#' +#' # Example of fixing old syntax +#' # real x[2] --> array[2] real x; +#' file <- write_stan_file(" +#' parameters { +#' real x[2]; +#' } +#' model { +#' x ~ std_normal(); +#' } +#' ") +#' mod <- cmdstan_model(file, compile = FALSE) +#' mod$format(canonicalize = TRUE) +#' +#' # Example of removing unnecessary whitespace #' file <- write_stan_file(" #' data { #' int N; From d29c52ef2b2a0ebe988ee0ae73689263708d0963 Mon Sep 17 00:00:00 2001 From: jgabry Date: Wed, 13 Sep 2023 12:09:20 -0600 Subject: [PATCH 2/4] add test --- R/model.R | 8 ++++++-- tests/testthat/resources/stan/old_array_syntax.stan | 6 ++++++ tests/testthat/test-model-compile.R | 9 +++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/resources/stan/old_array_syntax.stan diff --git a/R/model.R b/R/model.R index 279e338f..4d7f78da 100644 --- a/R/model.R +++ b/R/model.R @@ -686,8 +686,12 @@ compile <- function(quiet = TRUE, ) ) if (is.na(run_log$status) || run_log$status != 0) { - stop("An error occured during compilation! See the message above for more information.", - call. = FALSE) + err_msg <- "An error occured during compilation! See the message above for more information." + if (grepl("auto-format flag to stanc", run_log$stderr)) { + format_msg <- "\nTo fix deprecated or removed syntax please see ?cmdstanr::format for an example." + err_msg <- paste(err_msg, format_msg) + } + stop(err_msg, call. = FALSE) } if (file.exists(exe)) { file.remove(exe) diff --git a/tests/testthat/resources/stan/old_array_syntax.stan b/tests/testthat/resources/stan/old_array_syntax.stan new file mode 100644 index 00000000..aa34f52e --- /dev/null +++ b/tests/testthat/resources/stan/old_array_syntax.stan @@ -0,0 +1,6 @@ +parameters { + real x[3]; +} +model { + x ~ normal(0, 1); +} diff --git a/tests/testthat/test-model-compile.R b/tests/testthat/test-model-compile.R index 7da135dd..f86685a5 100644 --- a/tests/testthat/test-model-compile.R +++ b/tests/testthat/test-model-compile.R @@ -174,6 +174,15 @@ test_that("compile errors are shown", { ) }) +test_that("compile suggests using format to fix old syntax", { + stan_file <- testing_stan_file("old_array_syntax") + expect_error( + cmdstan_model(stan_file), + "To fix deprecated or removed syntax please see ?cmdstanr::format for an example.", + fixed = TRUE + ) +}) + test_that("dir arg works for cmdstan_model and $compile()", { tmp_dir <- tempdir() tmp_dir_2 <- tempdir() From b982c34f9a223fb19e4cea37f11f03bd6f96f473 Mon Sep 17 00:00:00 2001 From: jgabry Date: Wed, 13 Sep 2023 17:49:34 -0600 Subject: [PATCH 3/4] Update model-method-format.Rd --- man/model-method-format.Rd | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/man/model-method-format.Rd b/man/model-method-format.Rd index 57944b58..9c4e8846 100644 --- a/man/model-method-format.Rd +++ b/man/model-method-format.Rd @@ -46,6 +46,21 @@ model directly back to the file or prints it for inspection. } \examples{ \dontrun{ + +# Example of fixing old syntax +# real x[2] --> array[2] real x; +file <- write_stan_file(" +parameters { + real x[2]; +} +model { + x ~ std_normal(); +} +") +mod <- cmdstan_model(file, compile = FALSE) +mod$format(canonicalize = TRUE) + +# Example of removing unnecessary whitespace file <- write_stan_file(" data { int N; From 02b074dc3f53869ca475ea880f183432682e9380 Mon Sep 17 00:00:00 2001 From: jgabry Date: Fri, 15 Sep 2023 12:25:04 -0600 Subject: [PATCH 4/4] update doc based on reviewer feedback --- R/model.R | 9 ++++++++- man/model-method-format.Rd | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/R/model.R b/R/model.R index 4d7f78da..beace340 100644 --- a/R/model.R +++ b/R/model.R @@ -945,8 +945,15 @@ CmdStanModel$set("public", name = "check_syntax", value = check_syntax) #' x ~ std_normal(); #' } #' ") +#' +#' # set compile=FALSE then call format to fix old syntax #' mod <- cmdstan_model(file, compile = FALSE) -#' mod$format(canonicalize = TRUE) +#' mod$format(canonicalize = list("deprecations")) +#' +#' # overwrite the original file instead of just printing it +#' mod$format(canonicalize = list("deprecations"), overwrite_file = TRUE) +#' mod$compile() +#' #' #' # Example of removing unnecessary whitespace #' file <- write_stan_file(" diff --git a/man/model-method-format.Rd b/man/model-method-format.Rd index 9c4e8846..ad6b4e65 100644 --- a/man/model-method-format.Rd +++ b/man/model-method-format.Rd @@ -57,8 +57,15 @@ model { x ~ std_normal(); } ") + +# set compile=FALSE then call format to fix old syntax mod <- cmdstan_model(file, compile = FALSE) -mod$format(canonicalize = TRUE) +mod$format(canonicalize = list("deprecations")) + +# overwrite the original file instead of just printing it +mod$format(canonicalize = list("deprecations"), overwrite_file = TRUE) +mod$compile() + # Example of removing unnecessary whitespace file <- write_stan_file("