Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors re-rendering quarto html with cached chunks and cmdstanr. #1012

Open
danmyles opened this issue Jul 19, 2024 · 3 comments
Open

Errors re-rendering quarto html with cached chunks and cmdstanr. #1012

danmyles opened this issue Jul 19, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@danmyles
Copy link

danmyles commented Jul 19, 2024

Describe the bug

Quarto fails to re-render to html following trivial changes to code chunks that use cmdstanr objects.

processing file: 01_data-exclusions.qmd
  |............................................... |  98% [model_1_postsum]    
Quitting from lines  at lines 616-637 [model_1_postsum] (01_data-exclusions.qmd)
Error in `read_cmdstan_csv()`:
! Assertion on 'files' failed: File does not exist: '/var/folders/ms/m90tgryx74362fqn7d65tl7w0000gn/T/Rtmp5yoGqf/model_8cda69ac3b06c9b214d4a12ede85f4f8-202407191622-1-7cf94c.csv'.
Backtrace:
 1. fit01$draws(variables = c("a_bar", "sigma", "p"), format = "df")
 2. private$read_csv_(...)
 3. cmdstanr::read_cmdstan_csv(...)
 4. cmdstanr:::assert_file_exists(files, access = "r", extension = "csv")
 5. checkmate::makeAssertion(files, res, .var.name, add)
 6. checkmate:::mstop(...)
                                                                                                                
Execution halted

To Reproduce

Turn on Quarto cache in the header:

...
execute:
   cache: true
...

Read in a stan model from file using the chunk options:

```{cmdstan file = "path/to/stan/model.stan", output.var = 'm01'}
``

Sample:

``` {r}
fit01 <- m01$sample( [...] )
``

Write some code to summarise the model output or whatever.

Render to html.

Make a small change to anything that calls the model fit object fit01. In my case it will happen if I just add an empty line after an unedited chunk.

Try to render to html again.

Error occurs.

Expected behavior
The document is rendered after making small changes without the need to clear the cache and refit all models etc,

Operating system
MacOs 14.0

CmdStanR version number
0.7.1

Additional context
None

@danmyles danmyles added the bug Something isn't working label Jul 19, 2024
@andrjohns
Copy link
Collaborator

This is because cmdstanr will use the R session temporary directory for the sampling outputs by default, when you re-render the temporary directory may have changed and so the sampling results are no longer there.

If you want to reuse the fit object across multiple runs then I'd suggest using the output_dir argument of $sample() so that the sampling results are always in an expected location:

fit01 <- m01$sample( [...], output_dir = "path/to/stanmodels")

Also, you should update your cmdstanr package, the current version is 0.8.1

@andrjohns
Copy link
Collaborator

@jgabry any chance you're familiar with quarto and caching? I wonder if there's a specific change we need to make to enable CmdStanFit objects to be cached with the draws (maybe some R6 trickery?)

@jgabry
Copy link
Member

jgabry commented Jul 19, 2024

@jgabry any chance you're familiar with quarto and caching? I wonder if there's a specific change we need to make to enable CmdStanFit objects to be cached with the draws (maybe some R6 trickery?)

So the issue is that the draws aren't in memory when the object is cached, right?

I think quarto relies on knitr to do the caching, and if I'm looking in the right place in knitr's source code it looks like knitr just uses base R's save function:

https://github.com/yihui/knitr/blob/fb1f4231d2df7b156e314686f39fc040ce807513/R/cache.R#L30

That's unfortunate because I was hoping we could just define a custom method for whatever function knitr was using to do the caching. But save isn't a generic function with methods so I don't think that's possible. Internally save calls functions like gzfile, xzfile, etc., and I don't think we can define custom methods for those either.

Maybe there's some other approach that I'm overlooking!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants