Search code examples
rshinyr-leaflet

Table on click in R Shiny leaflet is not shown


I wanna get a table clicking on the marker on leaflet map in Shiny app. But:

Warning: Error in ==: comparison (1) is possible only for atomic and list types
      [No stack trace available]

I get this error again and again. Here is my code. Something is wrong with my renderTable(), I guess.

botsad.final <- read_csv("https://raw.githubusercontent.com/Janzeero-PhD/Botanical-Garden-of-NULES/master/botsad_final.csv")

server <- function(input, output) {
  # create a reactive value that will store the click position
  data_of_click <- reactiveValues(clickedMarker=NULL)

  # Leaflet map
  output$map <- renderLeaflet({
    leaflet() %>% 
      addTiles(options = providerTileOptions(noWrap = TRUE)) %>%
      addCircleMarkers(data = botsad.final,
                       lng = ~ X, lat = ~ Y, radius = 3,
                       popup = paste0("<img src = ", botsad.final$link_4, " />"),
                       color="black",  fillColor="red", stroke = F, 
                       fillOpacity = 0.5,
                       label = botsad.final$species_5)
  })

  # store the click
  observeEvent(input$map_marker_click, {
    data_of_click$clickedMarker <- input$map_marker_click
  })

  # Make a barplot or scatterplot depending of the selected point
  output$table <- renderTable({
      return(
        subset(botsad.final %>%
              dplyr::select(3, 7:12), 
              id == data_of_click$clickedMarker$id
              )
      )
    })
}

ui <- fluidPage(
  br(),
  column(8, leafletOutput("map", height = "600px")),
  column(4, br(), br(), br(), br(), tableOutput("table")),
  br()
)

shinyApp(ui = ui, server = server)

Can the problem be related to some errors in my packages and R session? I seemed to get similar issue trying to find solutions for dplyr::select, while it didnt work without strict reference to the package. Herewith, library(tidyverse) was always working.


Solution

  • I there may be a few minor things to get this working.

    First, the select statement in dplyr removes your id variable. Looks like it's in column 14 and should be included if you want to filter/subset by id.

    Second, id needs to be included in the addCircleMarkers method so it can be accessed through data_of_click$clickMarker. Add layerId=botsad.final$id to include the id.

    Third, data_of_click is NULL before a marker is selected. Would check for NULL before rendering table.

    Let me know if this works and is what you had in mind.

    library(tidyverse)
    library(leaflet)
    
    botsad.final <- read_csv("https://raw.githubusercontent.com/Janzeero-PhD/Botanical-Garden-of-NULES/master/botsad_final.csv")
    
    server <- function(input, output) {
      # create a reactive value that will store the click position
      data_of_click <- reactiveValues(clickedMarker=NULL)
    
      # Leaflet map
      output$map <- renderLeaflet({
        leaflet() %>% 
          addTiles(options = providerTileOptions(noWrap = TRUE)) %>%
          addCircleMarkers(data = botsad.final,
                           lng = ~ X, lat = ~ Y, radius = 3,
                           popup = paste0("<img src = ", botsad.final$link_4, " />"),
                           color="black",  fillColor="red", stroke = F, 
                           fillOpacity = 0.5,
                           label = botsad.final$species_5,
                           layerId = botsad.final$id
          )
      })
    
      # store the click
      observeEvent(input$map_marker_click, {
        data_of_click$clickedMarker <- input$map_marker_click
      })
    
      # Make a barplot or scatterplot depending of the selected point
      output$table <- renderTable({
        if (is.null(data_of_click$clickedMarker)) {
          return(NULL)
        }
        return(
          subset(botsad.final %>%
                   dplyr::select(3, 7:12, 14), 
                     id == data_of_click$clickedMarker$id
          )
        )
      })
    }
    
    ui <- fluidPage(
      br(),
      column(8, leafletOutput("map", height = "600px")),
      column(4, br(), br(), br(), br(), tableOutput("table")),
      br()
    )
    
    shinyApp(ui = ui, server = server)