本文介绍了被动过滤数据以生成地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 mapview 处理一张闪亮的地图时,我一直被反应性弄糊涂,并试图让我的地图动态更新.这是一个无法正常工作的可重现示例,尽管是使用其他 SO 答案中的原则设计的:

## 这是一个闪亮的网络应用程序.您可以通过单击运行该应用程序# 上面的运行应用程序"按钮.## 在此处了解有关使用 Shiny 构建应用程序的更多信息:## http://shiny.rstudio.com/#图书馆(闪亮)图书馆(SF)图书馆(地图视图)# 为绘制直方图的应用程序定义 UIui

这有效,但地图不会更新.我试过放入一个动作按钮 - 并将 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)图书馆(SF)图书馆(地图视图)图书馆(传单)# 为绘制直方图的应用程序定义 UIui 

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

HTH!

In working with a map in shiny using mapview, I've been flummoxed by reactives and trying to make my map dynamically update. Here is a reproducible example that doesn't work properly, despite being designed using principles from other SO answers:

#
# 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)

This works, but the map will not update. I've tried putting in an action button - and putting input$button before the isolate statement, but, that causes the whole thing to throw an error.

I want to either see everything or a single county.

Any thoughts on what is missing/wrong here? I'm somewhat new to shiny and dealing with reactives!

解决方案

Usage of renderMapview seems to be somehow "discouraged" (see https://github.com/r-spatial/mapview/issues/160#issuecomment-398130788; https://github.com/r-spatial/mapview/issues/58). You should instead use renderLeaflet on the @map attribute of the mapview object. This should work:

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)

(Note that I also had to remove the isolate call as suggested by @Kent Johnson)

HTH!

这篇关于被动过滤数据以生成地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 18:56