Skip to contents

shinyfilters_server() runs an observer that makes your filters interdependent. serverFilterInput() is an alias of shinyfilters_server().

Usage

serverFilterInput(
  x,
  session = getDefaultReactiveDomain(),
  filter_combine_method = "and",
  args_apply_filters = NULL,
  ...,
  input = deprecated()
)

shinyfilters_server(
  x,
  session = getDefaultReactiveDomain(),
  filter_combine_method = "and",
  args_apply_filters = NULL,
  ...,
  input = deprecated()
)

Arguments

x

An object being filtered; typically the result of shinyfilters().

session

The shiny session whose inputs are read and updated. Defaults to the current session. The inputs of a configuration with a namespace (see with_ns()) are found from any session of the app, so shinyfilters_server() can be called at the top level of the server or inside the module. The namespace is the full one, as in the UI: inside a module's server that is session$ns(NULL), which differs from the module's id when modules are nested.

filter_combine_method

A string or function indicating how to combine multiple filters. If a string, it can be "and" (or "&") for logical AND, or "or" (or "|") for logical OR. If a function, it should take two logical vectors and return a combined logical vector.

args_apply_filters

A named list of additional arguments passed to apply_filters().

...

Additional arguments passed to updateFilterInput().

input

Deprecated: omit it, or use session. A shiny input object, or a reactive that resolves to a list of named values.

Value

A reactiveValues list with two elements: filtered, the filtered data, and input_values, the current filter input values as a named list.

Details

Only the inputs that have no value are updated, and they are left without one. The inputs of a configuration made by shinyfilters() are updated as updateFilterInput() updates them, from the filtered data.

An input set by a function other than the shiny inputs filterInput() creates needs the function that updates it. A shinyWidgets input, such as shinyWidgets::pickerInput, comes with its own. A column's update sends its choices, or its minimum and maximum, and that function is given the ones it has an argument for. One with an argument for none of them, such as shinyWidgets::updateNumericRangeInput, leaves its input as it is, so the input doesn't follow the other filters. Any other input, such as shiny::checkboxGroupInput, needs its update function named with .update_fn = fn among the input's arguments, as described in with_filters(). Without it, shinyfilters_server() errors.

Examples

if (FALSE) { # interactive() && requireNamespace("bslib") && requireNamespace("DT")
library(bslib)
library(DT)
library(S7)
library(shiny)

must_use_radio <- new_S3_class(
   class = "must_use_radio",
   constructor = function(.data) .data
)
method(filterInput, must_use_radio) <- function(x, ...) {
   call_filter_input(x, radioButtons, ...)
}
method(updateFilterInput, must_use_radio) <- function(x, ...) {
   call_update_filter_input(x, updateRadioButtons, ...)
}

use_radio <- function(x) {
   structure(x, class = unique(c("must_use_radio", class(x))))
}

df_shared <- data.frame(
  stringsAsFactors = FALSE,
   x = letters,
   y = use_radio(sample(c("red", "green", "blue"), 26, replace = TRUE)),
   z = round(runif(26, 0, 3.5), 2),
   q = sample(Sys.Date() - 0:7, 26, replace = TRUE)
)

filters_ui <- function(id) {
   ns <- NS(id)
   filterInput(
     x = df_shared,
     range = TRUE,
     selectize = TRUE,
     slider = TRUE,
     multiple = TRUE,
     ns = ns
   )
}

filters_server <- function(id) {
   moduleServer(id, function(input, output, session) {
     # serverFilterInput() returns a reactiveValues list
     serverFilterInput(df_shared, range = TRUE)
   })
}

ui <- page_sidebar(
   sidebar = sidebar(filters_ui("demo")),
   DTOutput("df_full"),
   verbatimTextOutput("input_values"),
   DTOutput("df_filt")
)

server <- function(input, output, session) {
   res <- filters_server("demo")
   output$df_full <- renderDT(datatable(df_shared))
   output$input_values <- renderPrint(res$input_values)
   output$df_filt <- renderDT(datatable(apply_filters(
     df_shared,
     res$input_values
   )))
}

shinyApp(ui, server)
}