R Shiny / R Studio + rMaps 与 KML 的网络

问题描述 投票:0回答:1

这是针对此处

发布的问题的后续行动

使用jdarrison开发的代码和讨论here,这是一个最小的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 文件一起使用时遇到了一些困难。

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

r networking shiny kml r-leaflet
1个回答
3
投票

这里有几个问题。当它们都加载时,

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"))})     
  })
)

new image

© www.soinside.com 2019 - 2024. All rights reserved.