2

I am using selectizeGroupUI to create dynamic filters in Shiny. The problem is I cannot set a default selection of the output and right now my app is selecting everything from both existing filters. That does not work for me because the real table with all the data is very big. So, I wonder how could I set as default a specific combination of filters which is illustrative for the user to start using the app but not as huge as selecting every single combination as it is working right now.

I know know how to set a default selection by using

selectInput(...,selected = 'table1',...) 

But now I do not how to apply that logic using selectizeGroupUI.

Here you can see my code right now, which is showing all filter combinations at the same time:

library(shinyWidgets)

tf<-test_2_filtros

shinyApp(
  ui = pageWithSidebar(
    headerPanel("Conditional Filters"),
    sidebarPanel(
      selectizeGroupUI(
        id = "my-filters",
        inline = FALSE,
        params = list(
          var_one = list(inputId = "var_1", title = "Select variable 1", placeholder = 'select'),
          var_two = list(inputId = "var_2", title = "Select variable 2", placeholder = 'select')
        )
      )
    ),
    
    mainPanel(
      DT::dataTableOutput("table")
    )
  ),
  
  server = function(input, output, session) {
    
    res_mod <- callModule(
      module = selectizeGroupServer,
      id = "my-filters",
      data = tf,
      vars = c("var_1", "var_2")
    )
    
    
    
    output$table <- DT::renderDataTable({
      req(res_mod())
      mytable<-res_mod() %>%
        dplyr::mutate(n = "Yes") %>%
        mutate(row_num = 1:n()) %>%
        tidyr::pivot_wider(names_from = var_3, values_from = n, values_fill = list(n = "No"))%>%
        select(-row_num)
      mytable<-DT::datatable(mytable, filter= 'top',options = list(order=list(0,'asc'), dom='t', pageLength= 100, autoWidth = TRUE),rownames = FALSE)
      formatStyle(mytable, columns = NULL, fontWeight = styleEqual(c('No', 'Yes'), c('normal', 'bold')))
      
    })
    
  },
  
  options = list(height = 500)
)

Here you can find my input and output:

input(tf in the code above)

var_1 var_2 var_3
red table1 column1
red table1 column1
red table1 column1
blue table2 column2
blue table2 column2
blue table2 column2
green table3 column3
green table3 column3
green table3 column3

output

var_1 var_2 column1 column2 column3
red table1 Yes No No
red table1 Yes No No
red table1 Yes No No
blue table2 No Yes No
blue table2 No Yes No
blue table2 No Yes No
green table3 No No Yes
green table3 No No Yes
green table3 No No Yes

Thanks for any help or feedback.

AFS
  • 93
  • 1
  • 5

1 Answers1

1

First, get the names of your input variables. Then define a default for given variables. I have shown how to filter with some default values for var_1 below. You can do the same for additional input variables.

output$t1 <- renderText({print(names(input)) })  ###  get names of input variables

output$table <- DT::renderDataTable({
  if (is.null(input[["my-filters-var_1"]])) {filt1 <- c("red","blue")  ## default values for var_1
  }else filt1 <- input[["my-filters-var_1"]]
  req(res_mod())
  mytable<-res_mod() %>%
    dplyr::filter(var_1 %in% filt1) %>%   
    dplyr::mutate(n = "Yes") %>%
    mutate(row_num = 1:n()) %>%
    tidyr::pivot_wider(names_from = var_3, values_from = n, values_fill = list(n = "No"))%>%
    select(-row_num)
  mytable<-DT::datatable(mytable, filter= 'top',options = list(order=list(0,'asc'), dom='t', pageLength= 100, autoWidth = TRUE),rownames = FALSE)
  formatStyle(mytable, columns = NULL, fontWeight = styleEqual(c('No', 'Yes'), c('normal', 'bold')))
  
})
YBS
  • 19,324
  • 2
  • 9
  • 27
  • 1
    Hello. This solution shows the results for var_1 but "red" and "blue" do not appear selected in the "Select variable 1" menu. And if I select table3 after the default selection, I get this error: "Problem with `mutate()` column `row_num`. [34mℹ[39m `row_num = 1:n()`. [34mℹ[39m `row_num` must be size 0 or 1, not 2." What might be wrong? – AFS Sep 20 '21 at 17:36