在使用mapview
处理闪亮的地图时,我被反应器弄糊涂了,并试图让我的地图动态更新。这是一个可重现的示例,尽管它是使用其他 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)
这有效,但地图不会更新。我尝试放入一个操作按钮 - 并将input$button放在隔离语句之前,但是,这会导致整个事情抛出错误。
我想要么看到所有东西,要么看到一个县。
关于这里缺少/错误的任何想法?我对闪亮和处理反应有点陌生!
使用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
电话(
哼!
将地图输出放在observe
内并摆脱isolate
。isolate
阻止更新,observe
允许您将其删除:
observe({
output$mapPlot <- renderMapview({
f <- fran()
mapview(f, zcol = "NAME_ASCI")
})
})
通常,您不应该将渲染输出嵌套在观察器中,您可以像这样拆分它们:
mapPlot <- reactive(mapview(fran(), zcol = "NAME_ASCI"))
output$mapPlot <- renderMapview(mapPlot())
@ibusett是正确的,但这是包括子集在内的工作代码(他们的代码仅适用于输入== All(。
编辑:他们更新了他们的代码以匹配我的常规子集,但您可以在 sf 对象上使用 dplyr::filter(正如他们提到的(。
library(sf)
library(mapview)
library(leaflet)
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[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)