0

I want to create a shiny app, that filters data table with two select inputs, that one of them is multiple and dependent on another one. First select input is category and the second one is brand. Brand is dependent on category input. I already made two dependent select inputs, but when I go to the filtering process of data table I get stuck. Because when I run app and filter data table with dependent multiple select, I get error " Warning in df$C_BRAND == input$brand : longer object length is not a multiple of shorter object length". I understand that whe I choose only one value on the second select its good, but when there is more, filtering fales.

runApp(list(
  ui = basicPage(
    sidebarPanel(
      selectInput("kat", "Kategorija", choices = unique(df1$C_CTG), selected = unique(df1$C_CTG)[1]),
      tags$hr(),
      selectInput("brand", "Brandas", choices = df1$C_BRAND[df1$C_CTG == unique(df1$C_CTG)[1]], 
                  multiple = TRUE)
    ),
    mainPanel(
      DT::dataTableOutput("table")
    )
  ),
  server = function(input, output, session) {

    observe({
      kat <- input$kat
      updateSelectInput(session, "brand", choices = df1$C_BRAND[df1$C_CTG == kat])
    })
    df <- main2019

    filterData1 <- reactive({
      df[which(df$C_CTG == input$kat & df$C_BRAND == input$brand),]
    })

    output$table <- DT::renderDataTable({
      DT::datatable(filterData1(),selection="multiple",rownames = F)
    })
  }
))

I know that I have to change this code line

df[which(df$C_CTG == input$kat & df$C_BRAND == input$brand),]

But I dont know what to put there, that when I choose more options in select input, filtering would work.

Here I made simple small sample of my datatable:

C_CTG <- sample(c(1:5),8,replace=TRUE)
brand <- sample(c(6:10),8,replace=TRUE)
store <- sample(c("shoes", "phones", "food", "drinks", "pets"),8, replace = TRUE)
input <- data.frame(C_CTG,brand,store)

Here is example of my generated data table

I expect for example, that in my first select I would choose value "2" in input$C_CTG and then in second select input$brand I would get to choose from "7" or "10" values and depending on what I will choose (7 or 10 or both) data table would show one row where input$C_CTG=2 and input$brand=10 or two rows where input$C_CTG=2 and input$brand=7 or it could show 3 rows when I chose input$C_CTG=2 and input$brand=c(7,10) I hope that this example will let you understand what I want to make.

  • 1
    Possible duplicate of [Why do I get "warning longer object length is not a multiple of shorter object length"?](https://stackoverflow.com/questions/10865095/why-do-i-get-warning-longer-object-length-is-not-a-multiple-of-shorter-object-l) – divibisan Apr 02 '19 at 14:46
  • 1
    The problem is that you have `multiple = TRUE` on `input$brand`. That means `input$brand` could be longer than length 1. If you're not making a comparison against something of length 1 or 2 vectors of equal length, you probably want to use `%in%` instead of `==` – divibisan Apr 02 '19 at 14:47
  • Yeah, I know about this but I need ```multiple = TRUE``` on ```input$brand``` that so I can filter ```intput$brand``` with multiple choices, because in my variable ```input$brand``` there are stored a lot of different stores that I want to filter in data table and show comparison between them – beginner R student Apr 02 '19 at 19:28
  • Then, as the duplicate mentions, you probably want to use `%in%` which checks for the first vector inside the second. – divibisan Apr 02 '19 at 19:51
  • I would have already used it, but the problem is that, vectors ```input$brand``` and ```input&C_CTG``` don't have same values. – beginner R student Apr 02 '19 at 20:25
  • Huh? But you're not comparing those. In your example you compare `df$C_BRAND` with `input$brand`. – divibisan Apr 02 '19 at 20:27
  • In my example ```df$C_BRAND``` is copy of ```input$brand``` and ```df$C_CTG``` is copy of ```input$kat```. In my question I made an example of my data table, so I hope that it will let you understand what I want to do. – beginner R student Apr 02 '19 at 21:04
  • Then you need to use `%in%` instead of `==` since your `input$` values can be a vector and `|` (the or operator) instead of `&` since you want rows where *either* (to use on example) `input$C_CTG=2` or `input$brand=c(7,10)` is true. – divibisan Apr 02 '19 at 21:10
  • Thank you! I tried it and it really worked. It's pity that I thought only inserting ```%in%``` instead of ```&``` and now I know where is the problem that I tried it to solve for two days. Thank you very much! – beginner R student Apr 03 '19 at 06:12
  • Can I ask you a question? You saw that in example my vectors ```input$brand``` and ```input$C_CTG``` that I am filtering are both numbers one from 1 to 5, another one from 6 to 10 and they are encoded like this. My question is: Can I put their real names in both selects (names are ```character```) but filter data table with their encodings? – beginner R student Apr 03 '19 at 06:24
  • If I understand correctly, the answer would be yes. From `?selectInput`: `choices: List of values to select from. If elements of the list are named, then that name rather than the value is displayed to the user`. So if you make your `choices` argument a named vector, you can have the select box show the names of that vector, but return the values (which here are numbers) – divibisan Apr 03 '19 at 15:13
  • Thanks, I named my values and it worked out. I have more questions with my project, is there any way that I could write some private messages to you for consultation? Bacause now I am on the next step working with this data and I need to make transformations to my table according to input data but I am falling at it. – beginner R student Apr 04 '19 at 12:49
  • Sorry, that's not how things work here. If you have more questions, just ask another question! You'll get better answers than just asking me. But make sure to follow the rules and include a [mcve] of your data and new problem. This question will give you a lot of advice on asking a good question: https://stackoverflow.com/q/5963269/8366499 – divibisan Apr 04 '19 at 14:26

0 Answers0