Skip to content

Variables

You can also use the flexfilter not to filter but to display and select columns from the data.frame, this has little to advantage over using a selectize input; it was added to have a consistent look and feel with the filter.

Note

The flexfilter_server() will then return a vector of variables to select.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
library(shiny)
library(flexfilter)

data <- data.frame(
  text = letters[1:10],
  factors = as.factor(LETTERS[1:10]),
  numeric = runif(10),
  integer = 1:10,
  date = seq.Date(Sys.Date()-9, Sys.Date(), by = "day"),
  logical = sample(c(T, F), 10, replace = TRUE)
)

ui <- fluidPage(
  theme = bslib::bs_theme(5L),
  # flexfilter UI
  flexfilterUI("filter"),
  DT::DTOutput("table")
)

server <- function(input, output, session) {
  # flexfilter server
  values <- flexfilter_server("filter", data, variables_only = TRUE)

  output$table <- DT::renderDT({
    if(!length(values()))
      return(data)

    data |>
      # filter values
      dplyr::select(values()) |>
      DT::datatable()
  })
}

shinyApp(ui, server)


This product was funded by The Association of State and Territorial Health Officials and was supported by OE22-2203: Strengthening U.S. Public Health Infrastructure, Workforce, and Data Systems grant Number 6 NE11OE000066, funded by CDC. Its contents are solely the responsibility of the authors and do not necessarily represent the official views of CDC.