Update deckgl map in R shiny when clicked on bar

主宰稳场 提交于 2021-02-10 07:38:05

问题


Trying to make a shiny app with these properties.

Default view is aggregate layer. There will be number of columns/bars. On click of a column, the view is updated to show the non-aggregated version of data.

It is something like to know which bar is getting clicked and filtering the data with respect to it.

I have written the below code, which does the aggregate layer. However not sure how to get the "clicked_on_this_bar" details. Here if we get, user clicked on bar of "Area A", then we can just filter orig_df with area A and update the view. How to proceed in this ?

rm( list = ls() )

library( shiny ) ; library( shinydashboard ) ; library( dplyr ) ; library( deckgl )

ui <- basicPage( deckglOutput( 'map', height = '800px' ) )

server <- function(input, output) {

aggregate_df = data.frame( 'area' = LETTERS[1:2], 'count' = 110:111, median_lon = c( 3.39, 3.41 ),
                         
                         median_lat = c( 49.7, 49.4 ), color = c("#0080FF", "#FF0080") )

orig_df = data.frame( 'area' = rep( c( 'A', 'B' ), c( 3, 2 ) ), count = 10:14, lon = c( 3.45, 3.39, 3.19, 3.41, 3.92 ),
                    
                    lat = c( 48, 49.7, 52, 49.4, 51.5 ), color = c("#0080FF", "#FF0080", "#FF3030", "#00EE00", '#FF7F24') )                    


output$map = renderDeckgl({

deckgl( zoom = 7, pitch = 35, latitude = 48.29507, longitude = 4.0731520 ) %>%
  
  add_column_layer(
    data = aggregate_df,
    getPosition = ~median_lon + median_lat,
    getElevation = ~count,
    getFillColor = ~color,
    getTooltip = "Area: {{area}}, Count: {{count}}",
    elevationScale = 100
  ) %>% add_basemap()

})


# observeEvent({
#   
#   deckgl_proxy( 'map' ) %>%
#     
#     add_column_layer(
#       data = orig_df %>% filter( area == click[['A']] ),
#       getPosition = ~lon + lat,
#       getElevation = ~count,
#       getFillColor = ~color,
#       getTooltip = "Area: {{area}}, Count: {{count}}",
#       elevationScale = 50
#     ) %>% update_deckgl(it = "works")
#   
# })

}

shinyApp(ui, server)

回答1:


You were almost there. What you have to do, is to listen to the input$map_onclick reactive, which, beside other infos, returns an object slot with the relevant info. This object is generated as soon as you click on any of your bars and here's the structure of the reactive:

List of 1
 $ map_onclick:List of 3
  ..$ lng   : num 3.4
  ..$ lat   : num 49.4
  ..$ object:List of 5
  .. ..$ area      : chr "B"
  .. ..$ count     : int 111
  .. ..$ median_lon: num 3.41
  .. ..$ median_lat: num 49.4
  .. ..$ color     : chr "#FF0080"

With this piece of information it is rather straight forward to adapt your code (I never worked with deckgl though, so I simply used your commented code - adapt if needed)

observeEvent(input$map_onclick, {
        deckgl_proxy( 'map' ) %>%

          add_column_layer(
            data = orig_df %>% filter( area == input$map_onclick$object[["area"]] ),
            getPosition = ~lon + lat,
            getElevation = ~count,
            getFillColor = ~color,
            getTooltip = "Area: {{area}}, Count: {{count}}",
            elevationScale = 50
          ) %>% update_deckgl(it = "works")
   })

What remains to be done I guess is to add some logic to get from the detailed map, but you could easily add a flag to the data from which you can derive at which layer (aggregated or original) you are and based on that run a different logic.



来源:https://stackoverflow.com/questions/62614164/update-deckgl-map-in-r-shiny-when-clicked-on-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!