mikialynn
mikialynn

Reputation: 61

Calling on a specific column from a reactive expression

I understand the syntax of taking your Spatial Polygon Data Frame and building a subset using a reactive expression. For instance, allowing a slider as a user input to narrow the range of data you want to display:

ui <- fluidPage(  
sliderInput("range", "Scores", min(SPDF@data$Total_Score), max(SPDF@data$Total_Score),value = range(SPDF@data$Total_Score), step = 0.5)
)

server <- function(input, output) {

final.dat <- reactive({SPDF[SPDF@data$Total_Score >= input$range[1] & SPDF@data$Total_Score <= input$range[2],]
})
}

And I know I can then assign final.dat() as the data in subsequent renderings: eg output$map <- renderLeaflet({leaflet(final.dat())}). However, I'm trying to use leafletProxy to update my map, and I'm having trouble filling my polygons and building a new legend based on the new subset. I cannot refer to the desired column of my Spatial Polygon Data Frame using the SPDF@data$column syntax anymore because it's now the function final.dat(). I've tried assigning final.dat() a variable and then using the $column syntax, but it errors (see code below). I've also tried replacing the SPDF$Column with SPDF@data$Column and with just ~Column, and all versions result in an error. Or the map just never plots.

Errors include: Warning: Error in pal2: unused argument (Total_Score) ERROR: [on_request_read] connection reset by peer

How do you refer to a column once you've used a reactive expression and created a function in place of your Spatial Polygon Data Frame? Specifically, when referring to the column for the fillColor argument in addPolygon() and the values argument in addLegend(). Can anyone see why my code below is not producing a map (just the slider)?

FULL CODE:

ui <- fluidPage(  
leafletOutput("map"),
sliderInput("range", "Scores", min(SPDF@data$Total_Score),  
max(SPDF@data$Total_Score),value = range(SPDF@data$Total_Score), step = 0.5)
)

server <- function(input, output) {
# Color palette
pal <- colorNumeric(palette = "YlOrRd", domain = SPDF$Total_Score)

# Initial Map
output$map <- renderLeaflet({leaflet() %>% 
  addTiles() %>%
  addPolygons(data=SPDF,
    fillColor = ~pal(Total_Score),
    weight = 0.5,
    opacity = 1,
    color = "white") %>%
  addLegend(position = "bottomleft",pal = pal, opacity = 0.7, values = SPDF$Total_Score, title = "<strong>Total Score</strong>")
})

# Subset data
final.dat <- reactive({
SPDF[SPDF@data$Total_Score >= input$range[1] & SPDF@data$Total_Score <= input$range[2],]
})

# New color palette
pal2 <-  reactive({ 
SPDF <- final.dat()  
colorNumeric(palette = "YlOrRd", domain = SPDF$Total_Score)
})

# Leaflet Proxy to update map
observe({
SPDF <- final.dat() 
leafletProxy("map", data = SPDF) %>%
  clearShapes() %>%
  clearControls() %>%
  addPolygons(
    fillColor = ~pal2(Total_Score),
    weight = 0.5,
    opacity = 1,
    color = "white") %>%
  addLegend(position = "bottomleft",pal = pal2, opacity = 0.7, values = SPDF$Total_Score, title = "<strong>Total Score</strong>")
})
}
shinyApp(ui = ui, server = server) 

Upvotes: 2

Views: 662

Answers (0)

Related Questions