【问题标题】:Update datatable when zooming with leaflet (shiny)使用传单缩放时更新数据表(闪亮)
【发布时间】:2020-05-23 02:19:32
【问题描述】:

我正在构建一个闪亮的应用程序,其中包含一个带有标记的传单地图,以及一个带有旁边每个标记信息的表格。当我放大传单地图时,如何更新表格以仅显示传单地图上仍然可见的标记?

# Minimum Viable Example


library(shiny)
library(leaflet)
library(DT)
data(quakes)

# Define UI
ui <- fluidPage(
    # leaflet box
    column(
        leafletOutput("mymap"),
        width = 8
    ),
    #data table box
    column(
        DT::dataTableOutput("table"),
        width = 4
    )
)

# Define server logic 
server <- function(input, output) {

    # leaflet map
    output$mymap <- renderLeaflet({
        # Show first 20 rows from the `quakes` dataset
        leaflet(data = quakes[1:20,]) %>% addTiles() %>%
            addMarkers(~long, ~lat, popup = ~as.character(mag), label = ~as.character(mag))
    })

    # data table
    output$table = DT::renderDataTable({
        quakes
    })

}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny leaflet


    【解决方案1】:

    使用input$mymap_bounds 事件过滤数据。在您的示例中,添加library(dplyr) 并将output$table 更改为

        output$table = DT::renderDataTable({
          if (isTruthy(input$mymap_bounds)) {
            bounds = input$mymap_bounds
            quakes %>% filter(
              between(long, bounds$west, bounds$east),
              between(lat, bounds$south, bounds$north)
            )
          } else
            quakes
        })
    

    请注意,这会过滤整个 quakes 表,而不是您显示的 20 个项目。修改以适应。有关详细信息,请参阅此页面的事件部分: https://rstudio.github.io/leaflet/shiny.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 2017-08-07
      • 2018-11-17
      • 2017-05-30
      • 2019-11-11
      • 2020-03-07
      • 2022-01-09
      相关资源
      最近更新 更多