r-spatial / leafgl

R package for fast web gl rendering for leaflet
Other
263 stars 31 forks source link

Rendering plot for selected shape with addGlPolygons() #99

Open coppertank opened 1 week ago

coppertank commented 1 week ago

I have a dataset containing informations about municipalities and their relative shapes. The number of these municipalities are very high (+40000), so to render all my polygons in a short amount of time i need to use {leafgl}.

Printed the map with all the shapes, i want to be able to select a specific municipality and have a reactive plot specific for that municipality. Let's say, clicking on a shape, i want to have a bar plot (made with {echarts4r}) showing the population regarding municipality i chose.

This is my attempt:

library(shiny)
library(leaflet)
library(leafgl)
library(sf)
library(echarts4r)

test_grid <- data.frame(
    id = c("1", "2", "3"),
    population = c(20000, 40000, 60000),
    geometry = c("POLYGON ((-0.08946933215777712 51.47411466935998, -0.08969000802663779 51.47598131471286, -0.08781123839374265 51.476842086027, -0.08507730250047442 51.47619388479495, -0.08485681363623958 51.4743272330522, -0.08673553872787136 51.47346650705812, -0.08946933215777712 51.47411466935998))"   
                  ,"POLYGON ((-0.08651493322924536 51.47159981398401, -0.08673553872787136 51.47346650705812, -0.08485681363623958 51.4743272330522, -0.08212314240894439 51.47367901320982, -0.08190272389120813 51.47181231375328, -0.08378140444241272 51.47095163307104, -0.08651493322924536 51.47159981398401))" 
                  ,"POLYGON ((-0.08817291831604734 51.4688722666752, -0.0883936404299329 51.47073900106984, -0.08651493322924536 51.47159981398401, -0.08378140444241272 51.47095163307104, -0.08356086928788481 51.46908489229133, -0.08543953196103615 51.46822412468794, -0.08817291831604734 51.4688722666752))" )) %>% 
    st_as_sf(wkt = 3,crs = 4326)

ui <- fluidPage(
    leafglOutput("map"),
    echarts4rOutput("bar")
)

server <- function(input, output) {

  selected_municipality <- reactiveVal()

  observeEvent(input$map_glify_click, {
    selected_municipality(input$map_glify_click$id)
  })

    output$map <- renderLeaflet({
      leaflet(data = test_grid) %>% 
        addTiles() %>% 
        addGlPolygons(data = test_grid,
                      popup = paste0("clicked shape: ", test_grid$id),
                      weight = 5,
                      opacity = 1,
                      layerId = ~id,
                      fill = TRUE,
                      fillOpacity = .9,
                      fillColor = "dimgray",
        )
    })

    output$bar <- renderEcharts4r({
      req(selected_municipality())

      municipality_data <- test_grid %>% 
        filter(.$id == selected_municipality())

      municipality_data %>%
        e_charts(id) %>%
        e_bar(population) %>%
        e_title("Population of Selected Municipality")
    })
}

shinyApp(ui = ui, server = server)

However, when clicking on a shape, i get en error saying "subscript out of range", and i can't understand where this error comes from. Does anyone have an idea on how to fix this problem?

Furthermore, I wanted to ask if there was the possibility, for example by selecting first one polygon and then another, to have both information of the selected municipalities, as in the application in this link (https://hoga.shinyapps.io/healthdown/).

However, selecting the same polygon twice, it should be as if you hadn't selected anything.

trafficonese commented 22 hours ago

your app works fine for me, I dont see any problems/errors..

tim-salabim commented 22 hours ago

@coppertank I can reproduce your error. What's your sessionInfo()?