Sam Sam - 9 days ago 4
R Question

Leaflet/shiny: cannot draw reactive polygons

Having looked through the Shiny integration example on the Leaflet for R page, I am having trouble subsetting and displaying some polygons for display in my shiny app.

At the moment, i'm getting a app, with sidebar, but the main display is simply "Error:Don't know how to get path data from object of class reactive"

The idea is to pick a country polygon from GB (3 countries) and display it alone, depending on the drop down choice;

require(shiny)
require(rgdal)
require(rgeos)
require(leaflet)

cont <- readOGR(".\\mypath\\mypolygons.shp", "mypolygons", stringsAsFactors=FALSE)

ui <- fluidPage(

titlePanel("My page"),

sidebarLayout(
sidebarPanel(
selectInput("countryInput", "Country:", choices = c('England','Scotland','Wales'))
),
mainPanel(
leafletOutput("mymap")
)
)
)

server <- function(input, output, session) {

pols <- eventReactive(input$countryInput,{cont[substr(cont@data$code,1,1)==substr(input$countryInput,1,1),]})

output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data = pols)
})
}


shinyApp(ui, server)


error:

Warning: Error in polygonData.default: Don't know how to get path data from object of class reactive
Stack trace (innermost first):
83: polygonData.default
82: polygonData
81: derivePolygons
80: addPolygons
79: function_list[[k]]
78: withVisible
77: freduce
76: _fseq
75: eval
74: eval
73: withVisible
72: %>%
71: func [#6]
70: output$mymap
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: <Promise>

Sam Sam
Answer

fix:

firstly i changed;

pols <- eventReactive(input$countryInput,{cont[substr(cont@data$code,1,1)==substr(input$countryInput,1,1),]})

to;

pols <- reactive({cont.sim[substr(cont.sim@data$gssCode,1,1)==substr(input$countryInput,1,1),]

and the 'addPolygons line was missing the open/close brackets after the variable name 'pols'

addPolygons(data = pols)

becomes

addPolygons(data = pols())