Exctracting value from a data frame using character coming from event$id in R

Viewed 12

Some context: I want to use the layerId (as row index) from the marker a click on the leaflet map to extract another value from a data frame. The data frame is "def_veh" and has columns named "ID_Fahrzeug" and "Postleitzahl".

So I set the layerId of my markers equal to ID_Fahrzeug, so that when I observe a click on a marker the event$id returns me the value of "ID_Fahrzeug" for this marker as a character.

In the showPopup function, I would like to use this returned value to extract the value from the data frame "def_veh" that is located in the column "Postleitzahl" in the same row as the event$id value in the column "ID_Fahrzeug". I want then to store this extracted value in the variable "codepost" to use it as input for a following function used to plot a chart.

Here is a code sample of what I tried to explain in words:

    #map function
  output$map <- renderLeaflet({
   leaflet(def_veh) %>% #creates a map based on the coordinates of damages
      addTiles() %>%
      addMarkers(lng= ~Laengengrad, lat = ~Breitengrad, clusterOptions = markerClusterOptions(), layerId= ~ID_Fahrzeug) %>% #creates cluster markers to groups the points on the map where a damage occurred
     setView(lng = 11.107353, lat = 50 , zoom = 7) #fits the map's boundaries on Germany (more or less)
  })
  
  # When map is clicked, show a popup with city info
  showPopup <- function(id_veh, Breitengrad_OEM, Laengengrad_OEM) {
    codeposte <- def_veh[def_veh$ID_Fahrzeug==id_veh,"Postleitzahl"]
    Plot <- Plot_Bar_Cart(codeposte)
    svg(filename= paste(folder,"plot.svg", sep = "/"),
        width = 500 * 0.005, height = 300 * 0.005)
    print(Plot)
    dev.off()

    content <- paste(popup_content,readLines(paste(folder,"plot.svg",sep="/")), collapse = "")

    leafletProxy("map") %>% addPopups(Laengengrad_OEM, Breitengrad_OEM, content, layerId = ID_Fahrzeug)
  }

  observe({
    leafletProxy("map") %>% clearPopups()
    event <- input$map_marker_click
    if (is.null(event))
      return()

    isolate({
      showPopup(event$id, event$lat, event$lng)
    })
  })

The problem is now, that when I run my shinyApp and click on a marker on the map, the windows closes by itself... This is the error message that appears: "Error in base::try(showPopup, silent = TRUE) : object 'showPopup' not found" Can someone help me to locate the problem? :)

0 Answers
Related