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?
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.
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 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.
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?
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):