|
| 1 | +library(htmltools) |
| 2 | +library( leaflet) # I installed leaflet 1.0 with devtools::install_github("timelyportfolio/[email protected]") |
| 3 | +library(leaflet.extras) |
| 4 | +library(vueR) |
| 5 | + |
| 6 | +topojson <- readr::read_file('https://rawgit.com/TrantorM/leaflet-choropleth/gh-pages/examples/basic_topo/crimes_by_district.topojson') |
| 7 | + |
| 8 | +map <- leaflet() %>% |
| 9 | + setView(-75.14, 40, zoom = 11) %>% |
| 10 | + addProviderTiles("CartoDB.Positron") %>% |
| 11 | + addGeoJSONChoropleth( |
| 12 | + topojson, |
| 13 | + valueProperty = "{{selected}}", |
| 14 | + group = "choro" |
| 15 | + ) |
| 16 | + |
| 17 | +ui <- tagList( |
| 18 | + tags$div( |
| 19 | + id="app", |
| 20 | + tags$select("v-model" = "selected", |
| 21 | + tags$option("disabled value"="","Select one"), |
| 22 | + tags$option("incidents"), |
| 23 | + tags$option("dist_num") |
| 24 | + ), |
| 25 | + tags$span( |
| 26 | + "Selected: {{selected}}" |
| 27 | + ), |
| 28 | + tags$div(map) |
| 29 | +), |
| 30 | + tags$script( |
| 31 | +" |
| 32 | +var app = new Vue({ |
| 33 | + el: '#app', |
| 34 | + data: { |
| 35 | + selected: 'incidents' |
| 36 | + }, |
| 37 | + watch: { |
| 38 | + selected: function() { |
| 39 | + // uncomment debugger below if you want to step through |
| 40 | + //debugger; |
| 41 | +
|
| 42 | + // only expect one |
| 43 | + // if we expect multiple leaflet then we will need |
| 44 | + // to be more specific |
| 45 | + var instance = HTMLWidgets.find('.leaflet'); |
| 46 | + // get the map |
| 47 | + // could easily combine with above |
| 48 | + var map = instance.getMap(); |
| 49 | + // we set group name to choro above |
| 50 | + // so that we can easily clear |
| 51 | + map.layerManager.clearGroup('choro'); |
| 52 | +
|
| 53 | + // now we will use the prior method to redraw |
| 54 | + var el = document.querySelector('.leaflet'); |
| 55 | + // get the original method |
| 56 | + var addgeo = JSON.parse(document.querySelector(\"script[data-for='\" + el.id + \"']\").innerText).x.calls[1]; |
| 57 | + addgeo.args[7].valueProperty = this.selected; |
| 58 | + LeafletWidget.methods.addGeoJSONChoropleth.apply(map,addgeo.args); |
| 59 | + } |
| 60 | + } |
| 61 | +}); |
| 62 | +" |
| 63 | + ), |
| 64 | + html_dependency_vue(offline=FALSE) |
| 65 | +) |
| 66 | + |
| 67 | +browsable(ui) |
0 commit comments