mapview icon indicating copy to clipboard operation
mapview copied to clipboard

trouble saving a mapview::sync map?

Open cjtexas opened this issue 8 years ago • 4 comments

Tried syncing maps and that works but the resulting object doesn't seem to play nice with anything. I can't export the map or embed in shiny or anything else. Am I missing something?

Thanks!

library(mapview)
library(htmlwidgets)

mapOSM <- leaflet() %>%
  addTiles() %>% 
  setView(lng = -99.84375, lat = 38.82259, zoom = 10)

mapESRI <- leaflet() %>%
  addTiles(
    urlTemplate = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
  ) %>% 
  setView(lng = -99.84375, lat = 38.82259, zoom = 10)

mapSync <- mapview::sync(mapOSM, mapESRI)
class(mapSync)

[1] "shiny.tag.list" "list"

htmlwidgets::saveWidget(mapSync, file = "MapCompare.html")

Error in system.file(config, package = package) : 'package' must be of length 1

cjtexas avatar Aug 05 '16 15:08 cjtexas

@timelyportfolio any ideas?

tim-salabim avatar Aug 05 '16 15:08 tim-salabim

htmlwidgets::saveWidget is designed to save a single htmlwidget, so fails with mapSync since it is composed of more than one htmlwidget. To save mapSync we will have to use htmltools, which unfortunately as far as I am aware does not contain a simple save_tags-type function. Here is what I used last year for my posts on buildingwidgets.com.

save_tags <- function (tags, file, selfcontained = F, libdir = "./lib") 
{
  if (is.null(libdir)) {
    libdir <- paste(tools::file_path_sans_ext(basename(file)), 
                    "_files", sep = "")
  }
  htmltools::save_html(tags, file = file, libdir = libdir)
  if (selfcontained) {
    if (!htmlwidgets:::pandoc_available()) {
      stop("Saving a widget with selfcontained = TRUE requires pandoc. For details see:\n", 
           "https://github.com/rstudio/rmarkdown/blob/master/PANDOC.md")
    }
    htmlwidgets:::pandoc_self_contained_html(file, file)
    unlink(libdir, recursive = TRUE)
  }
  return(file)
}

Then you can do something like.

save_tags(mapSync, "index.html", selfcontained=TRUE)

Perhaps I should see if this could be incorporated officially in htmltools. cc @jcheng5 @wch

timelyportfolio avatar Aug 05 '16 16:08 timelyportfolio

Nice! @timelyportfolio would it be possible to wrap sync and latticeView into a widget? Then saveWidget should work as expected, shouldn't it?

tim-salabim avatar Aug 06 '16 09:08 tim-salabim

@tim-salabim is it already possible to save interactive facets? I still get the same error as above when using htmlwidgets::saveWidget directly.

mtennekes avatar May 27 '18 16:05 mtennekes