How do I create a Leaflet Proxy in observeEvent() for checkboxGroup in R Shiny

480 views Asked by At

I'm a bit new to R Shiny, and I'm trying to make a simple, dynamic web map in which common users can find where to recycle a variety of materials in Eastern Kentucky. In my sidebar panel in the UI, I made a checkboxGroup, so the user can filter through the recycling centers that allows for the recycling of the materials of their choosing (in this case, which centers recycle glass AND/OR aluminum AND/OR plastics). The checkbox shows up when you run the app, but I get a blank dashboard where the map should be. There's something wrong on the Server side of the app, when I try to make a proxy map in the observeEvent() function, but I'm stumped at what I'm doing wrong.

Here's a link to my data, named RE.csv: https://github.com/mallen011/Leaflet_and_Shiny/blob/master/Shiny%20Leaflet%20Map/csv/RE.csv

Here's the full, original Shiny app code: https://github.com/mallen011/Leaflet_and_Shiny/blob/master/Shiny%20Leaflet%20Map/app.R

Here's the data, read in R:

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)])
RE$popup <- paste("<p><h2>", RE$name,"</p></h2>",
              "<p>", RE$sector,"</p>",
              "<p>", RE$address,"</p>",
              "<p>", RE$phone,"</p>")

Here's the UI (dashboardSidebar is where the checkboxGroup input() is located):

ui <- dashboardPage(
 dashboardHeader(),
 dashboardSidebar(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"))),
       leafletOutput("map")
      )
       )

And here's the server:

server <- function(session, input, output) {
  output$map <- renderLeaflet({
                leaflet() %>% 
            addMarkers(data = RE,
                       lng = ~x, lat = ~y, 
                       label = l apply(RE$popup, HTML),
                       group = "recycle") %>% 

})

And this is the section I'm having trouble with in the server.r side. I'm unsure what I'm doing wrong, but I know it's something wrong with my observeEvent(). What I'm trying to accomplish is an observe event in which if the user checks glass in the checkbox group, then every recycling center that has the value "yes" for recycling glass will pop up. Just having a brain fart for how to go about getting this result.

observeEvent({
RE_click <- input$map_marker_click
if (is.null(RE_click))
  return()

if(input$RE_check == "Glass"){
  leafletProxy("map") %>% 
    clearMarkers() %>% 
    addMarkers(data = RE_click,
               lat = RE$y,
               lng = RE$x,
               popup = RE$popup)
}

}) }

shinyApp(ui = ui, server = server)

I'm sure the answer to my dilemma is a lot simpler than I'm making it out to be, but I'd appreciate any/all help. Stay safe out there! Thanks

0

There are 0 answers