Shiny & Leaflet map: Troubles subsetting or rendering polygons with merged data

I have both a CSV with lots of sociodemographic about country districts, and the corresponding districts Shapefile. Both share a common ID field, so I can merge them. As i try to render the polygons with leaflet, the code crashes in the line specified below

Help is greatly appreciated!

###########Load Data
Districts <- read.csv("DistrictsData.csv")
Districts[is.na(Districts)] <- 0
Districts$id <- as.integer(Districts$id)


###########Load Shapefile
shapeFile <- readOGR("shapeFile.shp")
shapeFile <- spTransform(shapeFile, CRS("+proj=longlat +datum=WGS84 +no_defs"))
shapeFile$id <- as.integer(shapeFile$id)


###########Function to Turn Labels into Column Title
DataType <- function(x) {
  switch(x,
                  "Crime Rate" = "cr",
         "Median Income" = "mi",
         "% of homes with higher education attained" = "ed1",
         "Price of Square Meter in Commercial Areas" = "sqp1",
  )
}


###########SHINY UI
ui <- dashboardPage(
  dashboardHeader(title = "My city"),
  dashboardSidebar(
    sidebarMenu(
               selectInput("pType", "",
                            list("Crime Rate",
                                 "Median Income",
                                 "% of homes with higher education attained",
                                 "Price of Square Meter in Commercial Areas"
                                                          )),
               sliderInput(
                 "range",
                 "Min-Max",
                 min = 0,
                 max = 100,
                 value = 2,
                 step = 1
               )
      )),
  dashboardBody(
    tabBox(
      width = 12, title = tagList(shiny::icon("map"), "Created by ..."),
      tabPanel("Viewer",
               tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
               leafletOutput("map",height = "75vh"))
    )
  )
)


###########SHINY SERVER
server = function(input, output, session) {


### let's subset the CSV dataframe, leaving only the id field, and whatever field I picked before
  selected = reactive({
    selected <- Districts[,c("id", paste0(DataType(input$pType)))]
            
##Here I want to add the slider Min-Max filter, to only filter the variable I picked with ininput$pType, but I can't even put the basic app to work.

  })
    
### static part of the map
  output$map<- renderLeaflet({
    leaflet() %>% 
      addProviderTiles(providers$CartoDB.Positron, options = providerTileOptions(noWrap = TRUE))  %>% 
      setView(lng = -58.450034, lat = -34.599585, zoom = 12)
      
  })
  
### dynamic part of the map, where I merge the shapefile and the subsetted CSV

  observe({
    
      shapeFile@data <- left_join(shapeFile@data, selected(), by="id")
      leafletProxy("map", data = shapeFile) %>% ##IN THIS LINE THE LOG RETURNS ERROR
        addTiles() %>% 
        clearShapes() %>% 
        addPolygons(data = shapeFile, fillColor = ~pal(DataType(input$pType)), fillOpacity = 0.7, 
                    color = "white", weight = 2)
    })
  
  
        leafletOutput('map', height=2000)

}


shinyApp(ui, server)


This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.