2014-05-13 55 views
2

这是一个后续的贴here网络的R闪亮/ R +工作室与rMaps KML

使用jdharrison开发的代码和讨论here的问题,这里是一个最小的ui.R:

library(shiny);library(rCharts) 
shinyUI(fluidPage(
mainPanel(
    tabPanel("Interactive", tags$style('.leaflet {height: 1000px;}'), 
    showOutput('mapPlot', 'leaflet')) 
))    ) 

和最小server.R:

library(shiny);library(rCharts);library(rMaps) 
shinyServer(function(input, output,session) { 
    output$mapPlot <- renderMap({ 
    map1 = Leaflet$new() 
    map1$setView(c(45.5236, -122.675), 13) 
    map1$tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png") 
    map1$addAssets(css = NULL, jshead = 'http://harrywood.co.uk/maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js') 
    map1$addKML('leaflet/placemark.kml') 
    leafletLib <- file.path(find.package("rMaps"), "libraries", "leaflet") 
    sampleKml <- readLines('http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml') 
    write(sampleKml, file.path(leafletLib, 'placemark.kml')) 
    map1 
    })  }) 

当我RStudio或我的服务器上运行shiny::runApp()在现场网站上,我收到一张空白地图,类似于我在上述解决方案之前在本地出现的问题。

我确定这与KML文件的位置以及可能的文件权限有关,但我在使用KML文件时遇到了一些困难。

感谢您提供任何提示或资源。

更新:我在本地尝试并获得相同的结果。所以,我不确定它与我的服务器网络有什么关系......

回答

3

这里有几个问题。 rCharts覆盖rMaps当他们都加载。所以Leaflet$new电话实际上来自rCharts包。也不可能使用之前使用的addAssets方法。有必要更改libraries/leaflet/config.yml文件并添加一个 leaflet-kml.js链接。还需要该文件下载到libraries/leaflet/external/leaflet-kml.js

首先,我们添加插件到rcharts单张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"))})  
    }) 
) 

new image

+0

甜!我将不得不放弃所有这些,并使用togeojson与geoJSON一起使用,但是我已经有了使用KML工作的颜色和标签,非常感谢。 – ideamotor

+0

乐于助人。这是一个临时解决方案Ramnath正在研究rCharts/rMaps的下一次迭代,因此上述内容将不再必要。 – jdharrison

+0

酷,当然也支持他! – ideamotor