2016-08-26 7 views
0

属性に基づいてマップ上のポイントをフィルタリングしようとしています。 1つの属性に1つの値しか含まれていない限り正常に動作します。 1つの属性に複数の値(「水;砂」)がある場合、フィルタに基づいてこのポイントをマッピングすることはできません。リーフレットとShiny in Rを使用したフィルタリング - 複数の値属性

は、ここに私の最小の例である:

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) 

私のコーディングがうまくフォーマットされていない場合、私は申し訳ありません。私はまだ初心者です。

どうもありがとう、

クリスティーナ

答えて

0

はまあ、私はそれがない場合ので、コメントを残してください、これはあなたが探しているものを解決し、100%わかりません。あなたの問題はreactiveの声明にあります。水、砂のチェックボックスがないので、チェックボックスBに一致することはありません。水や砂が選択されているかどうかにかかわらずチェックボックスBの一致を得るには、私はこれを試して、greplを使って完全一致ではなくパターン一致を得ました。これまでごdatFilt呼び出し変更してみてください:。

#Filter data 
    datFilt <- reactive({ 

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

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

(これはおそらく、これを行うための最も効率的なコードではありませんが、うまくいけば、それはどちらかでしょう仕事やあなたが探しているものにあなたが近づくと完璧な世界では、それは、あなたやない)のためのオプションがあります場合は、データの構造が異なるが、わからなかった場合は、このためにコーディングする方が簡単かもしれません

+0

こんにちはサム、これはまさに私が探していたものです。ありがとうございます! – ChMoe

+0

@ChMoeこれがあなたの質問に対する適切な答えであればそれを受け入れるべきです。 – TimSalabim

0

サムの答えが間違っている - あなたはgrepl

にこのような二つのベクトルを比較することはできません

代わりに連結してフィルタリングする必要があります。

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