1

Question 1 I try to save a leaflet map created with rMaps here, into a html file.

L2 <- Leaflet$new()
L2$setView(c(29.7632836,  -95.3632715), 10)
L2$tileLayer(provider = "MapQuestOpen.OSM")
L2
library(htmlwidgets)

saveWidget(L2,"t.html")

But I got an error:

Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : 
  ‘width’ is not a valid field or method name for reference class “Leaflet”

Answer 1 Thanks to LukeA, we can use this syntaxe:

leaflet(width = "100%") %>% 
  addProviderTiles("MapQuestOpen.OSM") %>% 
  setView(-95.3632715, 29.7632836, zoom = 10) -> L2 

Question 2 But then, how to add addAssets and setTemplate:

# Add leaflet-heat plugin. Thanks to Vladimir Agafonkin
L2$addAssets(jshead = c(
  "http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"
))

# Add javascript to modify underlying chart
L2$setTemplate(afterScript = sprintf("
<script>
  var addressPoints = %s
  var heat = L.heatLayer(addressPoints).addTo(map)           
</script>
", rjson::toJSON(crime_dat)
))

L2

Answer 2 Again, thanks to LukeA, we have the answer:

L2$save(tf <- tempfile(fileext = ".html"),standalone=TRUE)

Question 3

In fact, my initial problem is that I can't write the code in rmarkdown, in order to generate directly the map in the html document.

Now I can save the map alone. But how to integrate it into the html document ? The rapide solution is to make an iframe. It there any other more elegant solutions ?

John Smith
  • 1,604
  • 4
  • 18
  • 45

1 Answers1

4

Although not a direct answer to your question, I suggest an alternative approach that yields a similar result:

library(leaflet)
library(htmlwidgets)
leaflet() %>% 
  addProviderTiles("MapQuestOpen.OSM") %>% 
  setView(-95.3632715, 29.7632836, zoom = 10) -> m 
saveWidget(m, tf <- tempfile(fileext = ".html"))

or, using rMaps:

library(plyr)
library(rCharts)
library(rMaps)
data(crime, package = 'ggmap')
crime_dat = ddply(crime, .(lat, lon), summarise, count = length(address))
crime_dat = toJSONArray(na.omit(unname(crime_dat)), json = F)

L2 <- Leaflet$new()
L2$setView(c(29.7632836,  -95.3632715), 10)
L2$tileLayer(provider = "MapQuestOpen.OSM")
# Add leaflet-heat plugin. Thanks to Vladimir Agafonkin
L2$addAssets(jshead = c(
  "http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"
))
# Add javascript to modify underlying chart
L2$setTemplate(afterScript = sprintf("
<script>
  var addressPoints = %s
  var heat = L.heatLayer(addressPoints).addTo(map)           
</script>
", rjson::toJSON(crime_dat)
))

L2$save(tf <- tempfile(fileext = ".html"),standalone=TRUE)
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Excellent ! Thank you. In fact, I wanted to write leafet in this way, and add leaflet(width = "100%"). Then it didn't work when I tried Leaflet$new(width="100%"). So I was wondering if the parameter width is since then left in the function. If so, how to "reinitiate" the leaflet map ? And the rmaps example, extra features are added. How to do it with pipelines ? L2$addAssets(jshead = c( "http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js" )) and L2$setTemplate() – John Smith Jun 22 '16 at 14:55
  • You can set the width in `leaflet()` - e.g. `leaflet(width="50%")`. Concerning some "extra features" (?) - please browse through https://rstudio.github.io/leaflet/ – lukeA Jun 22 '16 at 15:03
  • By "extra features", I mean those in the example of rmaps: addAssets, and setTemplate. They are not in leaflet homepage. – John Smith Jun 22 '16 at 15:15
  • I dunno how you could embed Leaflet.heat within the leaflet package. Maybe post a new question or directly ask the package author? Or use custom & static pre-calculated contours? – lukeA Jun 22 '16 at 20:01
  • I just looked into the github issues of `leaflet`: [this issue](https://github.com/rstudio/leaflet/pull/174) seems to address your needs. – lukeA Jun 22 '16 at 20:06
  • Thank you very much @lukeA. In fact, the code with `Leaflet$new()` is from the package rmaps. And It seems that the addHeatmap function is not yet officially in the leaflet package. I got `could not find function "addHeatmap"`. – John Smith Jun 22 '16 at 20:22
  • No, it's not part of `leatlet`, yet (?). Install it using `devtools::install_github("rstudio/leaflet#174")` if you want (didn't work for me). You could also look at how [@timelyportfolio](http://stackoverflow.com/users/1552388/timelyportfolio) implemented it. – lukeA Jun 22 '16 at 20:59
  • Thank you very much Luke. I also edited my question ;) – John Smith Jun 23 '16 at 06:49