From 404c1e5d871259e72729615fb95db695d21ede86 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Wed, 8 Apr 2026 21:29:16 +0300 Subject: [PATCH 1/3] feat: provide guidance if tmp csv file is not found in Quarto rendering --- R/csv.R | 10 +++++++++- tests/testthat/test-csv.R | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/R/csv.R b/R/csv.R index 5fce18f4..3e72d076 100644 --- a/R/csv.R +++ b/R/csv.R @@ -138,7 +138,15 @@ 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")) && + any(grepl("/tmp", files))) { + stop(paste0(csv_file_exists, "\n To avoid this error 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/tests/testthat/test-csv.R b/tests/testthat/test-csv.R index 0922c0f0..7ba6f47b 100644 --- a/tests/testthat/test-csv.R +++ b/tests/testthat/test-csv.R @@ -73,11 +73,11 @@ 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") }) test_that("read_cmdstan_csv() fails with empty csv file", { From 5c8ef1b2650009dbf05af5e4d2cd4b1abb36adfa Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Thu, 9 Apr 2026 15:04:25 +0300 Subject: [PATCH 2/3] fix: improve when to show the Quarto cache message --- R/csv.R | 5 +++-- R/options.R | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/R/csv.R b/R/csv.R index 3e72d076..d53dd037 100644 --- a/R/csv.R +++ b/R/csv.R @@ -141,8 +141,9 @@ read_cmdstan_csv <- function(files, csv_file_exists <- check_file_exists(files, access = "r", extension = "csv") if (!isTRUE(csv_file_exists)) { if (isTRUE(getOption("knitr.in.progress")) && - any(grepl("/tmp", files))) { - stop(paste0(csv_file_exists, "\n To avoid this error when using Quarto cache,\n see `cmdstanr_output_dir` in `?cmdstanr_global_options`")) + 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) } 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 From 543980cd87f0b9828382562504c376be6d836a29 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Thu, 9 Apr 2026 15:05:22 +0300 Subject: [PATCH 3/3] test: add test for Quarto cache message --- tests/testthat/test-csv.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/testthat/test-csv.R b/tests/testthat/test-csv.R index 7ba6f47b..19c1dd34 100644 --- a/tests/testthat/test-csv.R +++ b/tests/testthat/test-csv.R @@ -78,6 +78,12 @@ test_that("read_cmdstan_csv() fails if the file does not exist", { "No file provided") expect_error(read_cmdstan_csv(character(0)), "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", {