[英]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.
如果一个属性具有多个值(即“水;沙”),则无法基于过滤器映射此点。
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.