0

I'm trying to add different icon to the markers which I'm trying to show. I have different category like this

category <- data() %>%
  dplyr::select(category) %>%
  distinct()

and it looks something like this

$ category: chr "Traffic" "Livelihood" "Waste" ....

I have a variable for logos for choosing different icon for the category

logos <- awesomeIconList(
  "Pothole" = makeAwesomeIcon(
    icon = "road",
    markerColor = "black"
  ),
  "Garbage Collection" = makeAwesomeIcon(
    icon = "trash",
    markerColor = "green"
  ),
  "Air Quality" = makeAwesomeIcon(
    icon = "cloud",
    markerColor = "blue"
  )
)

and I have written logic in observe function which I separated from the main logic and I don't why this is not working for me. This was working when I was not using observe function and was not using leaflet proxy. When I was using it in the renderLeaflet function then it was working.

I don't know what can I do here and from the error I didn't understand much about the logic

observe({
    filtered_data <- bqdata() %>%
      dplyr::filter(
        if ("All" %in% input$category) {
          category != ""
        } else {
          category %in% input$category
        }
      )
    
    proxy <- leafletProxy("layer_data")
    if (input$cluster) {
      proxy %>% addAwesomeMarkers(
        lat = filtered_data$lat,
        icon = ~logos[category],
        lng = filtered_data$long,
        popup = paste0(
          "<b>Title: </b>", filtered_data$title, "<br>",
          "<b>Type: </b>", filtered_data$type, "<br>",
          "<b>Category: </b>", filtered_data$category, "<br>",
          "<b>Status: </b>", filtered_data$status, "<br>",
          "<b>Description: </b>", filtered_data$description, "<br>",
          "<b>Address: </b>", filtered_data$address, "<br>",
          "<b>City Name: </b>", filtered_data$city, "<br>",
          "<b>State Name: </b>", filtered_data$state, "<br>"
        ),
        clusterOptions = markerClusterOptions()
      )
    } else {
      proxy %>% clearMarkerClusters()
    }
  })
2
  • please make a reproducible example, otherwise you will unlikely get a useful answer Commented Sep 1, 2022 at 14:39
  • On further inspection it looks like your variable reference in the icon parameter is what is causing issues. Try icon = logos[filtered_data$category], I don't think you need the ~.
    – c-petersen
    Commented Dec 14, 2022 at 19:24

1 Answer 1

0

You have 2 variables being referenced, but no data defined for the addAwesomeMarkers function to reference variables in (~ notation). The leaflet functions like addMarkers have a data.frame to be defined with the "data" parameter or else passed from a previous function. If you define "data" using your data.frame, "filtered_data" and then just reference the variables in the data.frame using "~" to denote they are from the data.frame.

proxy %>% 
  addAwesomeMarkers(
    data = filtered_data
    lat = ~lat,
    lng = ~long,
    clusterOptions = markerClusterOptions()
  )

The problem I mentioned in my comment above is due to the way you referenced logos which using your notation should be:

icon = logos[filtered_data$category]

Or if you define the data parameter as I mention above:

icon = logos[~category]

Not the answer you're looking for? Browse other questions tagged or ask your own question.