如何从R Shiny&Leaflet的过滤器中排除NULL值?

问题描述

有没有办法从R Shiny和传单地图中的过滤器中排除NULL数据值,同时仍在地图上显示该点?

在此示例中,我需要支持以下用例:1)按总体大小过滤,但可以选择2)查看在“总体”中包含NULL值的网站。

以下示例说明了我要完成的工作(在这种情况下,我不确定如何构建具有NULL值的数据框)。

# ################################################################################################
# ################################################################################################
# # Sec 1a. Needed Libraries& Input Files

library(shiny)
library(shinydashboard)
library(shinyWidgets) # more options to work with shiny,like inputs
library(leaflet)
library(dplyr)

##The Data
Map_DF <- data.frame("Point_ID" = c("A","B","C","D"),"Latitude" = c(38.05,39.08,40.05,41.08),"Longitude" = c(-107.00,-107.05,-108.00,-108.50),"PointUse" = c("farm","house","well","house"),"Population" = c(1,2,NULL,4))  #Input data here would have NULL value.


################################################################################################
################################################################################################
#UI
ui <- dashboardPage(
    
    dashboardHeader(),dashboardSidebar(
        materialSwitch(inputId = "Null_Values",label = "Include Empty Values (Null)?",value = FALSE,status = "primary"),sliderInput(inputId = "PopInput",label = "Population",min = 1,max = 4,value = c(1,4))
    ),dashboardBody(
        fluidRow(leafletOutput(outputId = 'mapA'))
    )
)

################################################################################################
################################################################################################
server <- function(input,output,session) {
  
  observeEvent(input$Null_Values {
    if (input$Null_Values == TRUE) {
      #allow to view site on map
    }
  })
  
  
    ## The Filter
    filterdf <- reactive({
        Map_DF %>% 
            filter(
              (Population >= input$PopInput[1]),(Population <= input$PopInput[2])
              )
    })
    
    ## Base Map Creation
    output$mapA <- renderLeaflet({
        
        leaflet() %>%
            addProviderTiles(
                providers$Esri.DeLorme,options = providerTileOptions(
                    updateWhenZooming = FALSE,updateWhenIdle = TRUE)
            ) %>%
            setView(lng = -107.50,lat = 39.00,zoom = 6)
    })
    
    ## Update Map with Filter Selection
    observe({
        leafletProxy("mapA",session) %>% 
            clearMarkers() %>% 
            addCircleMarkers(
                data = filterdf(),radius = 10,color = "red",lat = ~Latitude,lng = ~Longitude,popupOptions(autoPan = FALSE),popup = ~paste("Population: ",filterdf()$Population))
    })
    
}

################################################################################################
################################################################################################
shinyApp(ui = ui,server = server)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)