Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion R/csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")) &&
Copy link
Copy Markdown
Member

@jgabry jgabry Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It occurs to me that this is checking the option value at read time but what matters is what the option value was at run time.

If between the first render of the document and the later rerender if the user has since set cmdstanr_output_dir then this message will be suppressed.

I have an idea for how we can handle this. I will make some commits when I have time.

Copy link
Copy Markdown
Member

@jgabry jgabry Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just opened PR #1178, which takes a different approach to this that avoids this particular problem. Let me know what you think. One issue is that my version solves this problem but is more complicated. If we can figure out how to make your version work then I would prefer that to my version.

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()
Expand Down
4 changes: 3 additions & 1 deletion R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions tests/testthat/test-csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
Loading