gpt4 book ai didi

r - 以 Shiny 的方式过滤传单 map 数据

转载 作者:行者123 更新时间:2023-12-03 23:16:23 24 4
gpt4 key购买 nike

我在用传单 map 设置这个 Shiny 的东西时遇到了麻烦。我的 original post有两个问题,有人建议我写一个新帖子来解决我的第二个问题:按速度过滤后如何让 map 显示更新的数据 ;无论我更改“速度”还是 map 边界,我的表格都会更新,但传单 map 不会根据速度过滤器输入更新点。

可复制代码

library(shiny)
library(magrittr)
library(leaflet)
library(DT)

ships <-
read.csv(
"https://raw.githubusercontent.com/Appsilon/crossfilter-demo/master/app/ships.csv"
)

ui <- shinyUI(fluidPage(
titlePanel("Filter"),
sidebarLayout(
sidebarPanel(width = 3,
numericInput(
"speed_f", label = h5("Ship's Speed"), value = 100
)),
mainPanel(tabsetPanel(
type = "tabs",
tabPanel(
"Leaflet",
leafletOutput("leafletmap", width = "350px"),
dataTableOutput("tbl")
)
))
)
))

server <- function(input, output) {
in_bounding_box <- function(data, lat, long, bounds, speed) {
data %>%
dplyr::filter(
lat > bounds$south &
lat < bounds$north &
long < bounds$east & long > bounds$west &
speed > input$speed_f
)
}

output$leafletmap <- renderLeaflet({
leaflet() %>%
addProviderTiles("Esri.WorldImagery", group = "ESRI World Imagery") %>%
addCircleMarkers(
data = ships,
~ long ,
~ lat,
popup = ~ speed,
radius = 5 ,
stroke = FALSE,
fillOpacity = 0.8,
popupOptions = popupOptions(closeButton = FALSE)
)
})

data_map <- reactive({
if (is.null(input$leafletmap_bounds)) {
ships
} else {
bounds <- input$leafletmap_bounds
in_bounding_box(ships, lat, long, bounds, speed)
}
})

output$tbl <- DT::renderDataTable({
DT::datatable(
data_map(),
extensions = "Scroller",
style = "bootstrap",
class = "compact",
width = "100%",
options = list(
deferRender = TRUE,
scrollY = 300,
scroller = TRUE,
dom = 'tp'
)
)
})


}

shinyApp(ui = ui, server = server)

更新

进行以下更改 data = data_map()似乎有效,但有一个异常(exception):
  output$leafletmap <- renderLeaflet({
leaflet() %>%
addProviderTiles("Esri.WorldImagery", group = "ESRI World Imagery") %>%
addCircleMarkers(
data = data_map(), #### THIS LINE HAS CHANGED
~ long ,
~ lat,
popup = ~ speed,
radius = 5 ,
stroke = FALSE,
fillOpacity = 0.8,
popupOptions = popupOptions(closeButton = FALSE)
)
})

但是,传单 map 不允许我缩小由过滤点定义的区域。有没有解决的办法?

最佳答案

如果您只为 map 数据定义一个响应式(Reactive)并在 renderLeaflet 中使用它它应该允许您然后移出定义。您不需要更改任何其他函数或响应式(Reactive),只需添加新的响应式(Reactive)并对 renderLeaflet 进行一些更改即可。如下

map_data_react <- reactive({

ships %>% dplyr::filter(speed > input$speed_f)

})


output$leafletmap <- renderLeaflet({

ships_data <- map_data_react() # Add this

ships_data %>% leaflet() %>%
addProviderTiles("Esri.WorldImagery", group = "ESRI World Imagery") %>%
addCircleMarkers(
~ long , # Removed `data = data_map()`
~ lat,
popup = ~ speed,
radius = 5 ,
stroke = FALSE,
fillOpacity = 0.8,
popupOptions = popupOptions(closeButton = FALSE)
)
})

关于r - 以 Shiny 的方式过滤传单 map 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50128349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com