gpt4 book ai didi

r - 响应式(Reactive)过滤数据以生成 map

转载 作者:行者123 更新时间:2023-12-02 03:03:19 25 4
gpt4 key购买 nike

在使用 mapview 处理 Shiny 的 map 时,我对 react 感到困惑,并试图让我的 map 动态更新。这是一个可重现的示例,尽管是使用其他 SO 答案的原则设计的,但无法正常工作:

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#

library(shiny)
library(sf)
library(mapview)

# Define UI for application that draws a histogram
ui <- fluidPage(

# Application title
titlePanel("Test of Mapview Selective Viewing"),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("county", "County Name",
choices = c("All", levels(franconia$NAME_ASCI)),
selected = "All"
)

),

# Show a plot of the generated distribution
mainPanel(
mapviewOutput("mapPlot")
)
)
)

# Define server logic required to draw a histogram
server <- function(input, output) {

fran <- reactive({
f <- franconia
if(input$county != "All") f <- franconia %>% filter(NAME_ASCI == input$county)

f
})

output$mapPlot <- renderMapview({

#get the data
f <- isolate(fran())

#generate a map
mapview(f, zcol = "NAME_ASCI")
})
}

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

这可行,但 map 不会更新。我尝试放入一个操作按钮 - 并将 input$button 放在isolate语句之前,但是,这会导致整个事情抛出错误。

我要么想看所有的东西,要么想看一个县。

对于这里缺少/错误的内容有什么想法吗?我对 Shiny 和处理 react 有点陌生!

最佳答案

使用 renderMapview 似乎在某种程度上“不鼓励”(参见 https://github.com/r-spatial/mapview/issues/160#issuecomment-398130788; https://github.com/r-spatial/mapview/issues/58 )。您应该在 mapview 对象的 @map 属性上使用 renderLeaflet。这应该有效:

library(shiny)
library(sf)
library(mapview)
library(leaflet)

# Define UI for application that draws a histogram
ui <- fluidPage(

# Application title
titlePanel("Test of Mapview Selective Viewing"),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("county", "County Name",
choices = c("All", levels(franconia$NAME_ASCI)),
selected = "All"
)

),

# Show a plot of the generated distribution
mainPanel(
mapviewOutput("mapPlot")
)
)
)

# Define server logic required to draw a histogram
server <- function(input, output) {

fran <- reactive({
f <- franconia
if(input$county != "All") f <- f <- franconia[franconia$NAME_ASCI == input$county, ]

f
})

output$mapPlot <- renderLeaflet({

#get the data
f <- fran()

#generate a map
mapview(f, zcol = "NAME_ASCI")@map
})
}

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

(请注意,我还必须按照 @Kent Johnson 的建议删除 isolate 调用)

呵呵!

关于r - 响应式(Reactive)过滤数据以生成 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59585109/

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