这里有几个问题。rCharts
覆盖rMaps
当它们都加载完毕时。所以Leaflet$new
电话实际上来自rCharts
包裹。而且也不可能使用addAssets
之前使用过的方法。有必要改变libraries/leaflet/config.yml
文件并添加一个leaflet-kml.js
关联。还需要将该文件下载到libraries/leaflet/external/leaflet-kml.js
首先我们将插件添加到 rcharts leaflet javascript 文件中
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"))
现在我们可以看看使用闪亮
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"))})
})
)