r-spatial / mapview

Interactive viewing of spatial data in R
https://r-spatial.github.io/mapview/
GNU General Public License v3.0
516 stars 91 forks source link

Some `mapview` usage prevents caching chunk on knitr #473

Open cderv opened 6 months ago

cderv commented 6 months ago

Initially, this comes from a report in Quarto: https://github.com/quarto-dev/quarto-cli/issues/9058

Here is a reprex with mapview

---
title: Test Document
output: html_document
---

```{r}
library(mapview)
library(plainview)
mapview(poppendorf[[5]], legend = TRUE, layer.name = "T3")

You can run this once (using knit button to run in a background R session), which will cache the chunk. But on subsequent runs, you'll get an error. 

Error: ! path for html_dependency not found: C:/Users/chris/AppData/Local/Temp/RtmpyErHiA/data_stars779843bf736_754d19 Backtrace: ▆

  1. └─rmarkdown::render("C:/Users/chris/Documents/test.Rmd", encoding = "UTF-8")
  2. └─output_format$pre_processor(...)
  3. └─rmarkdown (local) base(...)
  4. └─rmarkdown:::html_extras_for_document(...)
  5. └─rmarkdown (local) dependency_resolver(all_dependencies)
  6. └─base::lapply(dependencies, validate_html_dependency)
  7. └─rmarkdown (local) FUN(X[[i]], ...)
  8. └─rmarkdown:::stop2("path for html_dependency not found: ", file)

What happens is that it will try to load what is cached, and as it is using another R session temp directory for one of its dependency.

This comes from https://github.com/r-spatial/mapview/blob/f35e32f0f992f6ab9b4ca650415f8f495d05f369/R/raster.R#L124-L127

and so from leafem : https://github.com/r-spatial/leafem/blob/6d6831352038b8f7462ff7afa698050c4e46fb5e/R/imagequery.R#L77

https://github.com/r-spatial/leafem/blob/6d6831352038b8f7462ff7afa698050c4e46fb5e/R/utils.R#L65-L80

as makepathStars is created the file in a temporary directory.

I opened here because it was easier for me to build a mapview example, and I don't know if there could be other HTML dependencies in mapview with the same problem

FWIW, This is related to another issue we had with leaflet and leaflet.providers that did not support caching with knitr due to same usage of HTML dependency being in temporary directory

Hope this helps understand the issue. Happy to clarify or discuss any feedback you may have on this.