简体   繁体   English

在R中使用Leaflet和Shiny过滤地图-多个值属性

[英]Filter map using Leaflet and Shiny in R - multiple value attributes

I am trying to filter points on a map based on attributes. 我正在尝试基于属性过滤地图上的点。 It works fine as long as one attribute contains only one value. 只要一个属性仅包含一个值,它就可以正常工作。 In case that one attribute has multiple values (ie "water;sand") I am not able to map this point based on the filter. 如果一个属性具有多个值(即“水;沙”),则无法基于过滤器映射此点。

Here is my minimum example: 这是我的最小示例:

library(data.table)
mydat <- data.table( londd=c(20, 38, 96, 32),
                     latdd=c(60, 56, 30, 31),
                     art=c("mountain", "water,sand", "sand", "forest"),
                     anwendung=c("a","b","c","d"))

#Set up ui
ui <- shinyUI(fluidPage(
sidebarPanel(h5("", width=2),
     checkboxGroupInput(inputId="ArtFlag", label=h4("Art des Bodens"), 
        choices=setNames(object=c("mountain", "water", "sand", "forest"),
                           nm=c("mountain", "water", "sand", "forest"))),
     checkboxGroupInput(inputId="AnwendungFlag", label=h4("Anwendung"), 
        choices=setNames(object=c("a","b","c","d"),
                           nm=c("a","b","c","d"))),
position="left"),
#App mainPanel content and styles
mainPanel(fluidRow(leafletOutput(outputId="lmap")))
              )
            )
          )

#Set up server
server <- function(input, output){
#Build leaflet map
lmap <- leaflet(data=mydat)%>%
addProviderTiles("Stamen.TonerLite", options = providerTileOptions(noWrap = TRUE)) %>%
fitBounds(~min(londd), ~min(latdd), ~max(londd), ~max(latdd))

#Filter data
datFilt <- reactive(mydat[art%in%input$ArtFlag & anwendung%in%input$AnwendungFlag])

#Add markers based on selected flags
observe({
  if(nrow(datFilt())==0) {print("Nothing selected");leafletProxy("lmap") %>% clearShapes()}
    else{ #print(paste0("Selected: ", unique(input$InFlags & input$InFlags2)))

    leafletProxy("lmap", data=datFilt())%>%clearShapes()%>%
    addCircleMarkers(lng=~londd, lat=~latdd,
                     clusterOptions=markerClusterOptions(), weight=3,
                     color="#33CC33", opacity=1, fillColor="#FF9900", 
                     fillOpacity=0.8)%>% clearShapes()
        }
     })

   output$lmap <- renderLeaflet(lmap)
}

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

I am sorry if my coding is not well formatted. 很抱歉,我的编码格式不正确。 I am still a beginner. 我还是一个初学者。

Thanks a lot, 非常感谢,

Christina 克里斯蒂娜(Christina)

Well, I'm not 100% sure this solves what you're looking for, so please leave a comment if it doesn't. 好吧,我不是100%肯定能解决您要寻找的问题,因此,如果不能解决问题,请发表评论。 Your problem is in your reactive statement - you don't have a checkbox for "water,sand", so you'll never get a match for checkbox B. To get a match for checkbox B regardless of if water or sand was selected, I tried this, using grepl to get a pattern match, rather than an exact match. 您的问题出在您的reactive陈述中-您没有“水,沙”复选框,因此您永远不会获得与复选框B的匹配。要获得与复选框B的匹配,无论是否选择了水或沙子,我尝试过使用grepl进行模式匹配,而不是完全匹配。 Try changing your datFilt call to this: 尝试将您的datFilt调用更改为此:

  #Filter data
  datFilt <- reactive({

    filterName <- ifelse(length(input$ArtFlag) == 0, 'none', input$ArtFlag)

    mydat[grepl(filterName, art) & anwendung%in%input$AnwendungFlag]
    })

(This is probably not the most efficient code to do this, but hopefully it will either work or get you closer to what you're looking for. In a perfect world, it might be easier to code for this if the structure of your data was different, but not sure if that's an option for you or not) (这可能不是执行此操作的最有效的代码,但是希望它可以起作用或使您更接近所要查找的内容。在理想的情况下,如果数据的结构更容易为此编写代码是不同的,但不确定这是否适合您)

Sam's answer is incorrect - you cannot compare two vectors like this in grepl 山姆的答案不正确-您无法在grepl比较两个向量

You need to concatenate and filter instead: 您需要连接并过滤:

datFilt <- reactive({
    ArtSearch <- paste0(input$ArtFlag,collapse = "|")
    ArtSearch <- gsub(",","|",ArtSearch)
    mydat[grepl(ArtSearch,art) & Anwendung %in% input$AnwendungFlag]
  }) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用R Leaflet和Shiny(多个条件)过滤地图上的数据点 - Filter data points on a map using R Leaflet and Shiny (multiple criteria) 使用Shiny和Leaflet访问R中的Javascript映射 - Access to a Javascript map in R, using Shiny and leaflet 使用传单/光泽的R中的交互式地图 - Interactive Map in R using leaflet/Shiny Select 在 leaflet 中使用 map_click 的多个项目,链接到 shiny 应用程序 (R) 中的 selectizeInput() - Select multiple items using map_click in leaflet, linked to selectizeInput() in shiny app (R) 使用传单中的 map_click 选择多个项目,根据过滤后的数据链接到 shiny app (R) 中的 selectizeInput() - Select multiple items using map_click in leaflet, linked to selectizeInput() in shiny app (R) based on filtered data 在带有叶子的R Shiny中选择多个过滤器选项的问题 - Issue with selecting multiple filter options in R Shiny with Leaflet R Shiny:用 Leaflet Z46F3EA056CAA31268CZEA Click 更新多个相关下拉菜单 - R Shiny: Updating Multiple Dependent Dropdown Menus with Leaflet Map Click 如何使Shiny Leaflet Map Reac更改输入值R - How to make Shiny leaflet map reac to change in Input value R 带有多个观察者的闪亮传单地图 - Shiny Leaflet Map with Multiple Observers 在R中,使用闪亮和传单,过滤器和操作(运行算法) - In R, using shiny and leaflet, filter and action (run an algorithm)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM