R 反应式过滤数据以生成地图

R 反应式过滤数据以生成地图,r,shiny,reactive-programming,r-mapview,R,Shiny,Reactive Programming,R Mapview,在使用mapview处理shiny中的地图时,我被reactives弄糊涂了,并试图使我的地图动态更新。以下是一个重复的示例,尽管使用了其他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 wi

在使用mapview处理shiny中的地图时,我被reactives弄糊涂了,并试图使我的地图动态更新。以下是一个重复的示例,尽管使用了其他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)
这是可行的,但地图不会更新。我尝试过在action按钮中添加一个,并将input$按钮放在isolate语句之前,但是,这会导致整个操作抛出一个错误

我想看到所有的东西或者一个县

你有什么想法吗?我对shiny和反动派有些陌生

将贴图输出放在内部观察并去除隔离。隔离正在阻止更新,观察允许您删除它:

  observe({
    output$mapPlot <- renderMapview({
      f <- fran()
      mapview(f, zcol = "NAME_ASCI")
    })
  })
将贴图输出放在内部观察并去除隔离。隔离正在阻止更新,观察允许您删除它:

  observe({
    output$mapPlot <- renderMapview({
      f <- fran()
      mapview(f, zcol = "NAME_ASCI")
    })
  })
一般来说,你,你可以这样把他们分开:

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

output$mapPlot <- renderMapview(mapPlot())
一般来说,你,你可以这样把他们分开:

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

output$mapPlot <- renderMapview(mapPlot())
renderMapview的使用似乎在某种程度上受到了阻碍。您应该改为在mapview对象的@map属性上使用RenderLablet。这应该起作用:

有光泽的图书馆 图书馆 图书馆地图视图 图书馆 为绘制直方图的应用程序定义UI uirenderMapview的使用似乎在某种程度上受到了阻碍。您应该改为在mapview对象的@map属性上使用RenderLablet。这应该起作用:

有光泽的图书馆 图书馆 图书馆地图视图 图书馆 为绘制直方图的应用程序定义UI
ui@ibusett是正确的,但这是工作代码,包括子集。他们的代码仅适用于input==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)

@ibusett是正确的,但这是工作代码,包括子集。他们的代码仅适用于input==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)

不知道-仍然没有更新。不知道该说什么,当我替换你的输出$mapPlot时它对我有效不知道-仍然没有更新。不知道该说什么,当我替换你的输出$mapPlot时它对我有效没有活动-反应上下文,我得到的操作是不允许的。当我尝试这个的时候。这可能是一个mapview错误;如果我将应用程序更改为直接使用传单,则以这种方式编写时会起作用。我在此处提交了一个地图视图问题:@KentJohnson。那很有趣,谢谢。我不知道是不是在renderMapView中,expr@SmokeyShakers就是我的猜测。但我不知道如何修复它。如果没有活动-反应上下文,我将无法执行操作。当我尝试这个的时候。这可能是一个mapview错误;如果我将应用程序更改为直接使用传单,则以这种方式编写时会起作用。我在此处提交了一个地图视图问题:@KentJohnson。那很有趣,谢谢。我不知道是不是在renderMapView中,expr@SmokeyShakers就是我的猜测。但我不知道如何修复它。请注意,该过滤器在sf对象上的工作方式与您编写的方式不同。我发布了一个小补丁,但你们应该得到最好的答案。改为:f@CarlosMercado过滤器适用于sf对象-。。。如果它是dplyr::filter。。。谢谢你指出这一点!我使用你的建议进行了修改,因为它更一般。请注意,过滤器对sf对象的工作方式与你编写的不同。我发布了一个小补丁,但你们应该得到最好的答案。改为:f@CarlosMercado过滤器适用于sf对象-。。。如果它是dplyr::filter。。。谢谢你指出这一点!我修改了你的建议,因为它更一般。