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

trouble saving a mapview::sync map? #35

Open
cjtexas opened this issue Aug 5, 2016 · 4 comments
Open

trouble saving a mapview::sync map? #35

cjtexas opened this issue Aug 5, 2016 · 4 comments

Comments

@cjtexas
Copy link

cjtexas commented Aug 5, 2016

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

@tim-salabim
Copy link
Member

@timelyportfolio any ideas?

@timelyportfolio
Copy link
Contributor

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

@tim-salabim
Copy link
Member

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

@mtennekes
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants