3

I have been searching the web for weeks trying to find an example or a code for what I am trying to accomplish with my shiny app (shinydashboard). I’m new to r and I’m starting to think that what I am trying to do is not possible. I basically have a leaflet map with a county polygon (shapefile) and I want to use the click event on the polygon to open a related dataTable (species table) on a box() below the map. The polygon data is a shapefile containing the county name and county number id. The related dataTable contain the county name, county #id and names of species for each county (one-to-many relationship). I was thinking that some how I could use the observe function and county # id from the “map_shape_click” to render the table with the names of the species by county on a output box(). However I don’t know if that is even possible. So far I was able to create the map and use the click event to capture the county name on a box() ( see attached image). This forum is amazing and I have learn a lot from the postings. Thank everyone that contribute to the community. If you have any suggestions how I can accomplish this task please let me know, Thanks

JB

Example image

1 Answer 1

6

let see if I got it right..

You can get the desired result by capturing the info related to hte clicked polygon and then using the id to subset your table

library(raster)
library(shiny)
library(leaflet)
library(RColorBrewer)
library(DT)

#species per region
mydata<-data.frame(myID=c("Iburengerazuba", "Iburasirazuba","Umujyi wa 
Kigali","Umujyi wa Kigali", "Amajyaruguru", "Iburengerazuba", 
"Amajyaruguru", "Amajyaruguru"),
myspec=c("virginiana", "setosa", "barbosa", "pelosa",
"pudica","pudica","pudica","pudica"))

#load in shapefiles for state
states <- getData("GADM", country = "rwa", level = 1)

#define color palettes for states
statePal <- colorFactor("Dark2", states@data$NAME_1)


shinyApp(

  ui = fluidPage(
    leafletOutput('myMap', width = "100%"), 
    br(), 
    DT::dataTableOutput("mytable", width = "100%")
  ),

  server <- function(input, output, session){

    output$myMap <- renderLeaflet({
       leaflet() %>% 
        addTiles() %>% 
        addPolygons(data = states, 
                    fillColor = ~statePal(states@data$NAME_1), 
                    fillOpacity = 1, 
                    color = "white", 
                    stroke = T, 
                    weight = 1, 
                    layerId = states@data$NAME_1) 
    }) 

    observeEvent(input$myMap_shape_click, {

      #capture the info of the clicked polygon
      click <- input$myMap_shape_click

      #subset your table with the id of the clicked polygon 
      selected <- mydata[mydata$myID == click$id,]

      #if click id isn't null render the table
      if(!is.null(click$id)){
        output$mytable = DT::renderDataTable({
          selected
        }) 
      } 
    }) 
  }) 
4
  • Hello G. Cocca,
    – JCB
    Commented Dec 23, 2019 at 3:17
  • Hello G. Cocca, Thank you very much for the response...It is exactly what I was trying to accomplish. It work great. The section to subset the layer id was not clear to me before. I did get it done using a similar observe event but your code is much clear. Thank once again for your help! JB
    – JCB
    Commented Dec 23, 2019 at 3:27
  • Glad it was helpful!
    – G. Cocca
    Commented Dec 23, 2019 at 21:10
  • @G.Cocca please check this out stackoverflow.com/questions/59474422/… Commented Dec 25, 2019 at 11:12

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