R Shiny / RStudio + KML 地图网络

2024-01-29

这是已发布问题的后续内容here https://stackoverflow.com/questions/23618813/cant-display-leaflet-html-through-r-shiny-404-error-how-to-integrate-kml-fil

使用开发的代码贾德里森 https://stackoverflow.com/users/1690692/jdharrison和讨论here https://github.com/ramnathv/rCharts/issues/108,这是一个最小的 ui.R:

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

和一个最小的服务器。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
  })     })

当我跑步时shiny::runApp()在我的服务器上RStudio或者在实时网站上,我得到一张空白地图,类似于我在上述解决方案之前在本地遇到的问题。

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

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

更新:我在本地尝试过并得到相同的结果。所以,我不确定这是否与我的服务器网络有关......


这里有几个问题。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"))})     
  })
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R Shiny / RStudio + KML 地图网络 的相关文章

随机推荐