Quarto Rendering library(cmdstanr)

Hi,

I’m new to using cmdstanr. I’m trying to save time when rendering my Quarto file by not having to fit the Stan model every time I render the document. Is there a way to do this?

I get the following error when I use

#| cache: true

or

saveRDS(fit_lin_mod, file = "fit_lin_mod.rds")
fit_lin_mod <- readRDS("fit_lin_mod.rds")

processing file: 4_stan_models.qmd
|… | 57% [unnamed-chunk-6] Error in read_cmdstan_csv():
! Assertion on ‘files’ failed: File does not exist: ‘C:/Users/olivi/AppData/Local/Temp/RtmpoBlpPk/linear_model-202507070840-1-61cef6.csv’.

The #| cache: true works the first time I render the document, but the error appears with the next times I try to render the document. I’m guessing it’s because cache or saveRDS doesn’t save all the data. Is there a way to get around this, I don’t want to have to wait minutes each time I render.

Thanks in advance for your help!

data_list <- list(
  N = nrow(grades),
  x8 = grades$y8,
  x9 = grades$y9,
  y10 = grades$y10
)

lin_mod <- cmdstan_model(stan_file = 'linear_model.stan')

fit_lin_mod <- lin_mod$sample(
  dat = data_list,
  chains = 4,
  parallel_chains = 4,
  iter_warmup = 2000,
  iter_sampling = 2000,
  adapt_delta = 0.99
  )

fit_lin_mod$cmdstan_diagnose()

fit_lin_mod$summary(
  variables = c(
    "mu8", "alpha9", "alpha10",
    "phi8", "phi9", "phi10",
    "beta1", "beta2", "beta3",
    "direct_effect", "indirect_effect", "total_effect")
  ) |>
  kable(digits = 2)

Does it work if you specify the output_dir argument when fitting the model? By default cmdstanr writes files to a temporary directory that maybe doesn’t exist the next time you render.

I’m not sure what you mean. Could you provide me with an example Quarto chunk based on my code?

Thank you,

Olivier

I think Jonah is referring to the argument that you would pass to the cmdstanr::sample function when you call it. By default, the function just places things into a temp directory (this is different than the Quarto cache) that disappears. With the output_dir specified, you can make it persistent, which may fix your issue.

1 Like

Yeah what @ssp3nc3r said is what I meant. When you do lin_mod$sample() try adding the argument output_dir and point it to a non-temporary location to store the files written by cmdstanr. Does that solve the problem?

Is this what you mean? It still takes a long time to render. Is there an example documented somewhere?

```{r}
#| output: FALSE
#| cache: true

# Create an output directory if it doesn't exist
if (!dir.exists("stan_output")) dir.create("stan_output")

fit_lin_mod <- lin_mod$sample(
  dat = data_list,
  chains = 4,
  parallel_chains = 4,
  iter_warmup = 2000,
  iter_sampling = 2000,
  adapt_delta = 0.99,
  output_dir = "stan_output",         # <- This is the fix
  save_warmup = FALSE                 # optional, to reduce file size
  )

Yeah that’s what I meant. So now it still takes a long time but doesn’t error? I haven’t tested this with quarto before, it was just my best guess as to what could help. @ssp3nc3r have you gotten caching to work with quarto?

You mentioned you also got an error using this. I forgot to mention that you should be able to avoid that error by using fit_lin_mod$save_object() instead of calling saveRDS() yourself. This calls saveRDS() for you but only after making sure everything that’s needed to be saved is available in memory:

There’s a section in the vignette that gives more details and suggestions about saving fitted model objects:

And the reasons for all this behavior are explained more here (essentially CmdStanR initially writes posterior draws to CSV and then reads them into memory):

Thank you! This is exactly what I was looking for.