Question

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...

Était-ce utile?

La solution

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

new image

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top