Filter table and render in shiny based on multiple values from select input

I am trying to filter and render a table in r shiny based of mutiple values from select input. The idea is the user can select multiple values from options 1.Select config and one value from 2.Select var' . Based of this values it should filter that data from the table testdataand render it in mainpanel. What The values in the dropdowns are nothing but a unique of strings from columnsconfigandvar`. Below is the code with the data.

library(shiny)


testdata <- tibble::tribble(
  ~config,       ~construct, ~var,
  "alpha,beta", "This is line 1",   12,
  "beta,gamma,alpha", "This is line 2",   15,
  "delta,alpha,tetra", "This is line 3",   21,
  "quad,core ,delta", "This is line 4",   12,
  "alpha,gamma", "This is line 5",   12,
  "beta,core", "This is line 5",   11,
  "delta,quad,tetra", "This is line 5",   21,
  "quad,tetra", "This is line 5",   12
)
config <- unique(unlist(strsplit(as.character(testdata$config), ",")))
var <- unique(unlist(strsplit(as.character(testdata$var), ",")))

ui <- fluidPage(
  title = 'Selectize examples',
  sidebarLayout(
    sidebarPanel(
      
      selectizeInput(
        'e2', '1.Select Config', choices = config, multiple = TRUE
      ),
      selectizeInput(
        'e3', '2. Select Var', choices = var, multiple = TRUE
      ),
      br(), 
      
      actionButton('select', 'Select')
    ),
    mainPanel(
      width = 10,
      DT::dataTableOutput(outputId = "mtable")
      
    )
  )
)

server <- function(input, output) {
  
  filtered_year <- reactive({
    filter(testdata, (config %in% input$e2) &
             (var %in% input$e3) &
) 
  })
  
  fully_filtered <- eventReactive(input$select, {
    filtered_year()
  })
  
  output$mtable <- DT::renderDataTable({
    DT::datatable(data = fully_filtered(), options = list(pageLength = 10),
                  rownames = FALSE, class = 'display', escape = FALSE)
    
  })
  output$ex_out <- renderPrint({
    a <- str(sapply('e2', function(id) {
      input[[id]]
    }, simplify = FALSE))
    a
    print(a)
  })
  
}

shinyApp(ui = ui, server = server)

something like

library(shiny)
library(tidyverse)

testdata <- tibble::tribble(
  ~config,       ~construct, ~var,
  "alpha,beta", "This is line 1",   12,
  "beta,gamma,alpha", "This is line 2",   15,
  "delta,alpha,tetra", "This is line 3",   21,
  "quad,core ,delta", "This is line 4",   12,
  "alpha,gamma", "This is line 5",   12,
  "beta,core", "This is line 5",   11,
  "delta,quad,tetra", "This is line 5",   21,
  "quad,tetra", "This is line 5",   12
)
testdata$config_split <- strsplit(testdata$config,split = ",")
testdata <- rowwise(testdata)

config <- unique(unlist(strsplit(as.character(testdata$config), ",")))
var <- unique(unlist(strsplit(as.character(testdata$var), ",")))

ui <- fluidPage(
  title = 'Selectize examples',
  sidebarLayout(
    sidebarPanel(
      
      selectizeInput(
        'e2', '1.Select Config', choices = config, multiple = TRUE
      ),
      selectizeInput(
        'e3', '2. Select Var', choices = var, multiple = FALSE
      )
    ),
    mainPanel(
      helpText('Output of the examples in the left:'),
      verbatimTextOutput('ex_out'),
      tableOutput("mytable")
      # use Github instead
      
    )
  )
)

server <- function(input, output) {
  output$ex_out <- renderPrint({
    a <- str(sapply('e2', function(id) {
      input[[id]]
    }, simplify = FALSE))
    a
    print(a)
  })
  output$mytable <- renderTable({
    # browser()
    if(isTruthy(input$e2))
   {result <- map_dfr( input$e2,
                      ~filter(testdata,
                             any(config_split==.)))
   print(result)
  r2<- result %>% ungroup %>% select(config,construct,var)
   }else {
     r2<- testdata %>% ungroup %>% select(config,construct,var)
   }
    if(isTruthy(input$e3)){
      return(filter(r2,var==input$e3))
    } else
      return(r2)
  })
}

shinyApp(ui = ui, server = server)

@nirgrahamuk So when I chose quad,core and delta it should show only one row but it shows row which have all these elements.How can that be filtered.Its doing OR if I understand it as of now but I want to do AND so that it minimizes the selection as expected.

then we dont need to map_dfr for result, and we use all() instead of any()

old

   if(isTruthy(input$e2))
   {result <- map_dfr( input$e2,
                      ~filter(testdata,
                             any(config_split==.)))

new

 if(isTruthy(input$e2))
    {result <- filter(testdata,
        all(input$e2 %in% config_split)
      )

Thank you so much . This was really helpful towards my shiny app which I may submit for the competition.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.