mardi 31 mars 2020

R Shiny Leaflet - how to make a CheckboxGroup for binary data

I posted a similar question here: How do I create a Leaflet Proxy in observeEvent() for checkboxGroup in R Shiny . But I'm a little desperate for answers, so I thought I'd rephrase my question and post it again. I've scoured the internet for answers and can't seem to find what I'm looking for. Apologies for the double posting.

Here's my issue. I have a dataset here: https://github.com/mallen011/Leaflet_and_Shiny/blob/master/Shiny%20Leaflet%20Map/csv/RE.csv

It's recycling centers in Kentucky. It's set up so each recyclable material is a column, and each row i.e. recycling center is listed as yes/no as to whether each center actually recycles said material. Here's an example of what the data looks like, in case you can't access the csv. Top row is the header column. Sorry for the formatting:

  • Name___________________GL______AL_____PL
  • Bath Community Recycling___Yes_____No____Yes
  • Ted & Sons Scrap Yard______No______No____Yes

Now I have the csv visualized on a R shiny dashboard app like here using Leaflet: https://github.com/mallen011/Leaflet_and_Shiny/blob/master/Shiny%20Leaflet%20Map/re_map.png But I want to add a control in which users can filter through where they can recycle their goods, namely, I want to use checkboxGroupInput() in R shiny so users can check materials and have recycling centers populate the map. For example, if a person wants to know where to recycle their glass, they can check "glass" in the checkbox group, and all recycling centers that allow glass recycling pop up.

So in R Shiny, I've read my recycling data csv (RE.csv):

RE <- read.csv("C:/Users/username/Desktop/GIS/Shiny Leaflet Map/csv/RE.csv")

RE$y <- as.numeric(RE$y)
RE$x <- as.numeric(RE$x)

RE.SP <- SpatialPointsDataFrame(RE[,c(7,8)], RE[,-c(7,8)])

Here's my UI that puts the checkboxGroupInput() in the sidebar():

ui <- dashboardPage(
  skin = "blue",
  dashboardHeader(titleWidth = 400, title = "Controls"),
  dashboardSidebar(width = 400
                  #here's the checkboxgroup, it calls the columns for glass, aluminum and plastic from the RE.csv, all of which have binary values of yes/no
                  checkboxGroupInput(inputId = "RE_check", 
                                     label = h3("Recycleables"), 
                                      choices = list("Glass" = RE$GL, "Aluminum" = RE$AL, "Plastic" = RE$PL),
                                      selected = 0)
                   ),
  dashboardBody(
    fluidRow(box(width = 12, leafletOutput(outputId = "map"))),
    tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
    leafletOutput("map")
  )
)

And now for the trouble I'm having: What do I put into my server so it observes each of these events? This is what I have for the event in which a user checks "glass", and I have no idea how wrong or how right it is. I just know it's not working. I'm trying to use "if" statements, so only values that equal "yes" populate the map. But currently, the map in the dashboard is blank no matter what I do, although the checkbox group input seems to work.

server <- function(session, input, output) {
 observeEvent({
    RE_click <- input$map_marker_click
    if (is.null(RE_click))
      return()

    if(input$RE$GL == "Yes"){
      leafletProxy("map") %>% 
        clearMarkers() %>% 
        addMarkers(data = RE_click,
                   lat = RE$y,
                   lng = RE$x)
      return("map")
    }
  })

Here's my output leaflet map too, in case that matters:

 output$map <- renderLeaflet({
    leaflet() %>% 
      setView(lng = -83.5, lat = 37.6, zoom = 8.5)  %>% 
      addProviderTiles("Esri.WorldImagery") %>% 
      addProviderTiles(providers$Stamen.Toner, group = "Toner") %>% 
      addPolygons(data = counties,
                  color = "green",
                  weight = 1,
                  fillOpacity = .1,
                  highlight = highlightOptions(
                    weight = 3,
                    color = "green",
                    fillOpacity = .3)) %>% 
      addMarkers(data = RE,
                 lng = ~x, lat = ~y, 
                 label = lapply(RE$popup, HTML),
                 group = "recycle",
                 clusterOptions = markerClusterOptions(showCoverageOnHover = FALSE)) %>% 
    addLayersControl(baseGroups = c("Esri.WorldImagery", "Toner"),
                     overlayGroups = c("recycle"),
                     options = layersControlOptions(collapsed = FALSE))
  })
}

I'm new to R Shiny if that's not obvious. I'd really appreciate any and all help. All my code is publicly available on my GitHub for download: https://github.com/mallen011/Leaflet_and_Shiny

Thanks and stay safe!




Aucun commentaire:

Enregistrer un commentaire