Skip to content

Commit a5f93f1

Browse files
committed
Informative error message if temp csv files are not found during Quarto rendering
Fixes #1012
1 parent 801b2b4 commit a5f93f1

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

R/fit.R

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,11 +1432,29 @@ CmdStanMCMC <- R6::R6Class(
14321432
if (!length(self$output_files(include_failed = FALSE))) {
14331433
stop("No chains finished successfully. Unable to retrieve the draws.", call. = FALSE)
14341434
}
1435-
csv_contents <- read_cmdstan_csv(
1436-
files = self$output_files(include_failed = FALSE),
1437-
variables = variables,
1438-
sampler_diagnostics = sampler_diagnostics,
1439-
format = format
1435+
csv_contents <- tryCatch(
1436+
read_cmdstan_csv(
1437+
files = self$output_files(include_failed = FALSE),
1438+
variables = variables,
1439+
sampler_diagnostics = sampler_diagnostics,
1440+
format = format
1441+
),
1442+
error = function(e) {
1443+
err_msg <- conditionMessage(e)
1444+
if (isTRUE(getOption("knitr.in.progress")) &&
1445+
isTRUE(self$runset$args$using_tempdir) &&
1446+
grepl("File does not exist:", err_msg, fixed = TRUE)) {
1447+
stop(
1448+
paste0(
1449+
err_msg,
1450+
"\n If this error happened when using Quarto or Rmarkdown caching,\n",
1451+
" see `cmdstanr_output_dir` in `?cmdstanr_global_options`"
1452+
),
1453+
call. = FALSE
1454+
)
1455+
}
1456+
stop(e)
1457+
}
14401458
)
14411459
private$inv_metric_ <- csv_contents$inv_metric
14421460
private$metadata_ <- csv_contents$metadata

R/options.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#' CSV files when fitting models. The default is a temporary directory. Files in
2929
#' a temporary directory are removed as part of \R garbage collection, while
3030
#' files in an explicitly defined directory are not automatically deleted.
31+
#' Note that using caching with Rmarkdown or Quarto does not store files in a
32+
#' temporary directory, so we recommend setting `cmdstanr_output_dir` to avoid
33+
#' failures when re-rendering.
3134
#'
3235
#' * `cmdstanr_verbose`: Should more information be printed
3336
#' when compiling or running models, including showing how CmdStan was called

tests/testthat/test-fit-mcmc.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,35 @@ test_that("draws() method returns draws_array (reading csv works)", {
9090
expect_equal(posterior::variables(draws_beta_alpha), c("beta[1]", "beta[2]", "beta[3]", "alpha"))
9191
})
9292

93+
test_that("draws() errors with quarto cache guidance for fits created with temp output", {
94+
# https://github.com/stan-dev/cmdstanr/issues/1012
95+
# https://github.com/stan-dev/cmdstanr/pull/1176
96+
97+
fit <- testing_fit("logistic", method = "sample", seed = 123, chains = 1)
98+
csv_files <- fit$output_files()
99+
100+
# Simulate a later cached re-render: the fit object still points to the temp
101+
# output files, which don't exist anymore
102+
unlink(csv_files, force = TRUE)
103+
withr::local_options(list(
104+
# Even if cmdstanr_output_dir is now set to a non-temp directory,
105+
# it was not set when the fit was created so we should still get the error message
106+
# that mentions quarto caching
107+
cmdstanr_output_dir = test_path("resources"),
108+
knitr.in.progress = TRUE
109+
))
110+
111+
expect_error(
112+
fit$draws(),
113+
paste0(
114+
"Assertion on 'files' failed: File does not exist: '", csv_files[[1]], "'.\n",
115+
" If this error happened when using Quarto or Rmarkdown caching,\n",
116+
" see `cmdstanr_output_dir` in `?cmdstanr_global_options`"
117+
),
118+
fixed = TRUE
119+
)
120+
})
121+
93122
test_that("inv_metric() method works after mcmc", {
94123
x <- fit_mcmc_1$inv_metric()
95124
expect_length(x, fit_mcmc_1$num_chains())

0 commit comments

Comments
 (0)