diff --git a/R/csv.R b/R/csv.R index 5fce18f4..d53dd037 100644 --- a/R/csv.R +++ b/R/csv.R @@ -138,7 +138,16 @@ read_cmdstan_csv <- function(files, files <- file.path(temp_storage, basename(files)) } format <- assert_valid_draws_format(format) - assert_file_exists(files, access = "r", extension = "csv") + csv_file_exists <- check_file_exists(files, access = "r", extension = "csv") + if (!isTRUE(csv_file_exists)) { + if (isTRUE(getOption("knitr.in.progress")) && + is.null(getOption("cmdstanr_output_dir")) && + any(grepl("csv$", files))) { + stop(paste0(csv_file_exists, "\n If this error happened when using Quarto cache,\n see `cmdstanr_output_dir` in `?cmdstanr_global_options`")) + } else { + stop(csv_file_exists) + } + } metadata <- NULL warmup_draws <- list() draws <- list() diff --git a/R/options.R b/R/options.R index 9e526ca7..85691bdc 100644 --- a/R/options.R +++ b/R/options.R @@ -27,7 +27,9 @@ #' * `cmdstanr_output_dir`: The directory where CmdStan should write its output #' CSV files when fitting models. The default is a temporary directory. Files in #' a temporary directory are removed as part of \R garbage collection, while -#' files in an explicitly defined directory are not automatically deleted. +#' files in an explicitly defined directory are not automatically deleted. Note +#' that Rmarkdown / Quarto cache does not store files in temporary directory, +#' and re-rendering with cache fails if explicit directory has not been defined. #' #' * `cmdstanr_verbose`: Should more information be printed #' when compiling or running models, including showing how CmdStan was called diff --git a/tests/testthat/test-csv.R b/tests/testthat/test-csv.R index 0922c0f0..19c1dd34 100644 --- a/tests/testthat/test-csv.R +++ b/tests/testthat/test-csv.R @@ -73,11 +73,17 @@ test_that("read_cmdstan_csv() fails for different variables", { test_that("read_cmdstan_csv() fails if the file does not exist", { csv_files <- c(test_path("resources", "csv", "model1-1-doesntexist.csv")) expect_error(read_cmdstan_csv(csv_files), - "Assertion on 'files' failed: File does not exist: 'resources/csv/model1-1-doesntexist.csv'.") + "File does not exist: 'resources/csv/model1-1-doesntexist.csv'") expect_error(read_cmdstan_csv(NULL), - "Assertion on 'files' failed: No file provided.") + "No file provided") expect_error(read_cmdstan_csv(character(0)), - "Assertion on 'files' failed: No file provided.") + "No file provided") + # if knitr rendering and using temp directory, there is an additional message + old_opt <- options(knitr.in.progress = TRUE, cmdstanr_output_dir = NULL) + expect_error(read_cmdstan_csv(csv_files), + "File does not exist: 'resources/csv/model1-1-doesntexist.csv'\n If this error happened when using Quarto cache,\n see `cmdstanr_output_dir` in `?cmdstanr_global_options`", + fixed = TRUE) + options(old_opt) }) test_that("read_cmdstan_csv() fails with empty csv file", {