shinygeojsonrchartsrmaps

Shiny renders a responsive rCharts leaflet map once, but is blank if you change the input variable


I am producing a Shiny App that produces a leaflet (rCharts) map depending on which bus route you pick. Everything renders perfectly at first glimpse, but if you change the route number, an empty map appears (not even a tilelayer). This isn't specific to the route number. For example, I can pick any route number to produce the first plot successfully, whereas the second plot, regardless of route number, is blank.

Has anyone come across this before? Is there a workaround?

Here is a simple example.

ui.R:

library(shiny)
library(rCharts)

shinyUI(fluidPage(
  titlePanel("Responsive Leaflet Map using rCharts"),

  sidebarLayout(
    sidebarPanel( "",
                  selectInput(
                    'route', 'Pick a bus route:',
                    choices = as.character(c("232","229"),
                                           selectize = FALSE)      
                  )
                  ),
    mainPanel("",
              chartOutput('map', 'leaflet')
              )
  )
))

server.R:

library(shiny)
library(rCharts)
library(RJSONIO)
library(rgdal)

shinyServer(function(input, output) {
  output$map <- renderMap({
    filename <- paste('json/',input$route,'.geojson',sep='')
    json <- fromJSON(file = filename)

    map3 <- Leaflet$new()
    map3$tileLayer(provide='Esri.WorldTopoMap')
    map3$setView(c(49.2494,-122.9797), zoom = 10)
    map3$set(dom = 'map')
    map3$fullScreen(TRUE)
    map3$geoJson(
      json,
      style = "#!
  {color: '#c93312'}!#")
    map3
  })
})

Thanks so much for any help you are able to provide.

C


Solution

  • The trick is to remove map3$set(dom = 'map'). Problem solved!