Networking for R Shiny / R Studio + rMaps with KML

依然范特西╮ 提交于 2019-12-08 02:05:41

问题


This is a follow-up for the question posted here

Using the code developed by jdharrison and the discussion here, here is a minimal ui.R:

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

And a minimal 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
  })     })

When I run shiny::runApp() on my server with RStudio or on a live Website I get a blank map, similar to the issue I had locally prior to the aforementioned solution.

I'm sure this has something to do with the location of the KML file and perhaps file permissions, but I'm having some difficulty getting it to work -with- the KML file.

Thank you for any tips or resources you may have.

UPDATE: I tried it locally and get the same result. So, I'm not sure it has something to do with my server networking...


回答1:


There are a few issues here. rCharts overrides rMaps when they are both loaded up. So the Leaflet$new call is actually coming from the rCharts package. Also it is not possible to use the addAssets method that was utilised previously. It is necessary to change the libraries/leaflet/config.yml file and add a leaflet-kml.js link. It is also necessary to download that file to libraries/leaflet/external/leaflet-kml.js

First we add the plugin to the rcharts leaflet javascript files

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

Now we can look at using shiny

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



来源:https://stackoverflow.com/questions/23637902/networking-for-r-shiny-r-studio-rmaps-with-kml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!