There are a few issues here. rCharts
overrides rMaps
when they are both loaded up. So the Leaflet$new
call is actually coming from the rCharts
package. Also it is not possible to use the addAssets
method that was utilised previously. It is necessary to change the libraries/leaflet/config.yml
file and add a
leaflet-kml.js
link. It is also necessary to download that file to libraries/leaflet/external/leaflet-kml.js
First we add the plugin to the rcharts leaflet javascript files
require(yaml)
leafletLib <- file.path(find.package("rCharts"), "libraries", "leaflet")
rMapsConfig <- yaml.load_file(file.path(leafletLib, "config.yml"))
# add a kml library
kmlLib <- readLines("http://harrywood.co.uk/maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js")
write(kmlLib, file.path(leafletLib, "external", "leaflet-kml.js"))
# add the library to config.yml
rMapsConfig$leaflet$jshead <- union(rMapsConfig$leaflet$jshead , "external/leaflet-kml.js")
write(as.yaml(rMapsConfig), file.path(leafletLib, "config.yml"))
Now we can look at using shiny
library(shiny)
library(rCharts)
library(rMaps)
runApp(
list(ui =fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput("obs", "Number of observations:", min = 0, max = 1000, value = 500)
),
mainPanel(
tabsetPanel(
tabPanel("Interactive", tags$style('.leaflet {height: 1000px;}'),
showOutput('mapPlot', 'leaflet'))
)
)
)
),
server = function(input, output,session) {
output$mapPlot <- renderUI({
map1 = Leaflet$new()
map1$setView(c(45.5236, -122.675), 13)
map1$tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
map1$addKML('leaflet/placemark.kml')
leafletLib <- file.path(find.package("rCharts"), "libraries", "leaflet")
sampleKml <- readLines('http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml')
write(sampleKml, file.path(leafletLib, 'placemark.kml'))
HTML(map1$html(chartId = "mapPlot"))})
})
)