-
-
Notifications
You must be signed in to change notification settings - Fork 872
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
Caching reference objects #2176
Comments
This comment was marked as outdated.
This comment was marked as outdated.
Okay, I have a better idea on how to implement this now. Basically, as you hinted in the SO posts, neither However, again, as you mentioned, users would need to call |
I agree that you should not have to deal with special cases. Here are some ideas for "terra" and some new things that I have implemented. Feedback appreciated.
As all objects could pass through, and a user could provide more specialized functions. That would still be a bit of additional work, but probably a nicer user experience than having to use unwrap on all terra objects in blocks after a cached block.
And now I see this:
And
This could work well with knitr caching if knitr can use Also, you have to load the package for this to work. It does not work like this in a clean session:
This works
Perhaps an ideal long-term solution would be for base to have a generic |
Thanks for these ideas! For now, I feel the best way might be that I provide an S3 generic function in knitr to process cached objects. Then other package authors like you can register the methods in their own packages. The default method of this function will be simply the identity function, i.e., |
Would the downside of that solution be that these packages then would need to depend on knitr? The same would be needed when creating the cache, unless you can use saveRDS. Using saveRDS would be great, I think because that is very general. |
I'll use You don't need to depend on knitr. Here is the trick we've been using to register S3 methods dynamically on load: https://github.com/rstudio/htmltools/blob/5fa01e7197143844be141dcc0cca85096059497d/R/tags.R#L22-L56 |
This comment was marked as duplicate.
This comment was marked as duplicate.
@rhijmans @yihui This PR adds registerS3method("knit_cache_preprocess", "terra", function(x) {
# do some modifications at here
}, envir = asNamespace("knitr"))
registerS3method("knit_cache_postprocess", "terra", function(x) {
# do some modifications at here
}, envir = asNamespace("knitr")) |
@atusy can you update the example above on how the new PR logic with hooks could solve this issue ? thanks! |
Is there a way, or could support be added, to tell knitr how to serialize objects of a particular class before caching, and how to restore them after caching? See this and this questions on stackoverflow for more context
The "terra" package uses S4 objects with one slot that has a reference to a C++ object (via a Rcpp module). These S4 objects can be serialized and unserialized with very little effort. For example, one can do
But can one use that mechanism with knitr caching? I suppose that it could be possible, in principle, to specify that objects of class "SpatRaster" should be passed to
serialize
before caching, and thatterra::rast
should be used when restoring the object. Whether this is easy to do in practice, I do not know.By filing an issue to this repo, I promise that
xfun::session_info('knitr')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('yihui/knitr')
.I understand that my issue may be closed if I don't fulfill my promises.
The text was updated successfully, but these errors were encountered: