2

在使用 Shiny using 处理地图时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 放在隔离语句之前,但是,这会导致整个事情抛出一个错误。

我想查看所有内容或查看单个县。

关于这里缺少/错误的任何想法?我对闪亮和处理反应物有点陌生!

4

4 回答 4

4

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

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)

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

于 2020-01-08T09:27:57.233 回答
1

把地图输出放在里面observe,去掉isolate. isolate正在阻止更新,observe让您删除它:

  observe({
    output$mapPlot <- renderMapview({
      f <- fran()
      mapview(f, zcol = "NAME_ASCI")
    })
  })
于 2020-01-03T20:58:11.617 回答
0

通常,您不应该将渲染输出嵌套在 observe中,您可以像这样拆分它们:

mapPlot <- reactive(mapview(fran(), zcol = "NAME_ASCI"))

output$mapPlot <- renderMapview(mapPlot())
于 2020-01-03T21:11:55.593 回答
0

@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)
于 2020-07-15T19:30:56.233 回答