2014-05-13 90 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