2016-07-11 2 views
6

私は次のようになります。フィールド間の選択AND/OR参加して複数のフィールド上の動的データ・フィルターのセットを作るのに苦労しています:ここでダイナミックAND ORフィルタ

enter image description here

私ですサンプルコード。私はフィルタの結合(AND/OR)を正しく動作させる方法がわかりません。

library(shiny) 
library(dplyr) 
library(DT) 

data("baseball") 

Year = unique(baseball$year) 
Team = unique(baseball$team) 
Stint = unique(baseball$stint) 

runApp(list(ui = fluidPage(
    titlePanel("Summary"), 

    sidebarLayout(
    sidebarPanel(

     selectInput("year", label = "Year", choices = Year, selected = NULL, multiple = TRUE), 
     selectInput("filter_join1", label = "", choices = c("OR","AND")), 
     selectInput("team", label = "Team", choices = Team, selected = NULL, multiple = TRUE), 
     selectInput("filter_join2", label = "", choices = c("OR","AND")), 
     selectInput("stint", label = "Stint", choices = Stint, selected = NULL, multiple = TRUE) 
    ), 

    mainPanel(
     DT::dataTableOutput("table") 
    ) 
) 
), 

server = function(input, output, session) { 
    WorkingDataset <- reactive({ 
    df_temp <- baseball %>% 
     filter(
     is.null(input$year) | year %in% input$year, 
     is.null(input$team) | team %in% input$team, 
     is.null(input$stint) | stint %in% input$stint 
    ) 
    }) 

    output$table <- DT::renderDataTable({ WorkingDataset() }) 

}) 

) 
+0

'filter'の条件の間にカンマを使用すると、'& 'のように機能します。代わりに '|'を使用するには、カンマを '|'で置き換える必要があります。これを行う最も簡単な方法(たぶんかなり恐ろしいことですが)は、多くの 'if'ステートメントです。 'substitute'はもっと簡潔かもしれませんが、かなり厄介なことがあります。 – alistaire

+0

こんにちはAlistaire、あなたのメッセージのおかげで。フィルタ間でAND/OR結合を選択する必要があります。たとえば、ユーザーの例では、Year OR Team AND St​​intを実行できます。または可能な他の組み合わせ –

+0

私は知っている。明らかに対話的に簡単です。プログラム的には痛みです。上記のコメントはいくつかの出発点です。 – alistaire

答えて

4

セイは、我々はirisデータセットを持っているし、我々はそれにいくつかのサブセットをしたいです。

iris$Species 

# We can also use `with` for that 
with(iris, Species) 

# We are interested in more complicated subsetting though. Want to have all rows 
# with 'setosa' 
with(iris, Species %in% 'setosa') 
iris[with(iris, Species %in% 'setosa'), ] 

# Now 'setosa' with some more condition 
iris[with(iris, Species %in% 'setosa' & Sepal.Length > 5.3), ] 


# That works perfectly. There is, however, an another way doing the exact thing in r. 
# We can input the subsetting condition as a character string, then change it to 
# the `expression` and `eval`uate it. 

cond_str <- paste0("with(iris, Species %in% 'setosa' & Sepal.Length > 5.3)") 
cond_str 
# which is the same as 
cond_str <- paste0("with(iris, ", "Species %in% ", "'", "setosa", "'", " & ", 
        "Sepal.Length > ", "5.3", ")") 
cond_str 

# This second approach will prove very powerful since we will replace "setosa" 
# with, say, `input$species` later on. 


cond <- parse(text = cond_str) 
cond 
eval(cond) 
iris[eval(cond), ] # √ 

input$speciesはベクトル することができ、結果として、我々は出力として、複数の文字列を取得することができますので、それは少し複雑になります。たとえば :

Spec <- c("setosa", "virginica") # ~ input$species 
paste0("with(iris, Species %in% ", Spec, ")") 

# We want only one character string! So, we'll have to collapse the vector Spec 
paste0("with(iris, Species %in% ", 
     paste0(Spec, collapse = " "), ")") 

# This is still not what we wanted. We have to wrap the entries into "c()" 
# and add quote marks. So, it's going to be pretty technical: 
paste0("with(iris, Species %in% ", 
     "c(", paste0("'", Spec, collapse = "',"), "'))") 

# Now, this is what we wanted :) Let's check it 
check <- eval(parse(text = paste0("with(iris, Species %in% ", 
     "c(", paste0("'", Spec, collapse = "',"), "'))"))) 
iris[check, ] # √ 

さて、光沢のあるアプリに移動してみましょう。変数と一致するデータセットbaseballがどこにあるかわからないため、を使用しないで、ggplot2diamondsデータセットを使用します。

私は変数の変更された名前を少し変更してから、前述のトリックをサブセット化に使用しました。私の例をあなたの問題に適合させるのは簡単です。

library(shiny) 
library(DT) 

# data("diamonds") don't know where I can find this dataset, hence I'll use 
       # diamond dataset 

library(ggplot2) # for diamonds dataset 


cut <- unique(as.character(diamonds$cut)) # or just levels(diamonds$cut) 
color <- unique(as.character(diamonds$color)) 
clarity <- unique(as.character(diamonds$clarity)) 

runApp(list(ui = fluidPage(
    titlePanel("Summary"), 

    sidebarLayout(
     sidebarPanel(
     # changed names of inputs 
     selectInput("cut", label = "Cut", choices = cut, selected = NULL, multiple = T), 
     selectInput("filter_join1", label = "", choices = c("OR","AND")), 
     selectInput("color", label = "Color", choices = color, selected = NULL, multiple = T), 
     selectInput("filter_join2", label = "", choices = c("OR","AND")), 
     selectInput("clarity", label = "Clarity", choices = clarity, selected = NULL, multiple = T) 
    ), 

     mainPanel(
     DT::dataTableOutput("table") 
    ) 
    ) 
), 

    server = function(input, output, session) { 

    WorkingDataset <- reactive({ 
     req(input$cut, input$color, input$clarity) 
     # show table only if all three inputs are available 

     # depending on filter_join inputs return "OR" or "AND" 
     join1 <- ifelse(test = input$filter_join1 == "OR", yes = "| ", no = "& ") 
     join2 <- ifelse(test = input$filter_join2 == "OR", yes = "| ", no = "& ") 

     # You could do this differently: just set choices = c("OR" = "|", "AND" = "&")) 
     # in the selectInput widget. 

     # Similar as in the example above with the iris dataset. 

     cond_str <- paste0(
     "with(diamonds, ", 
     paste0("cut %in% ", "c(", paste0("'", input$cut, collapse = "',"), "')", colapse = " "), 
     join1, 
     paste0("color %in% ", "c(", paste0("'", input$color, collapse = "',"), "')", colapse = " "), 
     join2, 
     paste0("clarity %in% ", "c(", paste0("'", input$clarity, collapse = "',"), "')", colapse = " "), 
     ")") 

     print(cond_str) # print the result to the console 
     cond <- parse(text = cond_str) 
     df <- as.data.frame(diamonds)[eval(cond), ] 
     df 
    }) 

    output$table <- DT::renderDataTable({ WorkingDataset() }) 

    }) 
) 
+0

あなたは輝いている神の仲間です...優れた解決策以上のもの... –

+0

ありがとう:)私はあなたがそれが好きで非常にうれしいです:) –