| Title: | Create 'shiny' Inputs from Vectors, 'data.frames', or any R Object |
|---|---|
| Description: | Provides an interface to 'shiny' inputs used for filtering vectors, data.frames, and other objects. 'S7'-based implementation allows for seamless extensibility. |
| Authors: | Josh Livingston [cre, aut], Novica Nakov [ctb] (ORCID: <https://orcid.org/0009-0005-7773-7718>) |
| Maintainer: | Josh Livingston <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.1.9000 |
| Built: | 2026-10-18 18:26:06 UTC |
| Source: | https://github.com/joshwlivingston/shinyfilters |
Applies a list of filters to an object, returning the filtered object.
apply_filters( x, filter_list, filter_combine_method = "and", expanded = FALSE, cols = NULL, ... )apply_filters( x, filter_list, filter_combine_method = "and", expanded = FALSE, cols = NULL, ... )
x |
An object to filter; typically a data.frame. |
filter_list |
A named list of filter values, used to filter the values
in |
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. |
expanded |
Logical; if |
cols |
Optional character vector of column names to retain in the
output when |
... |
Additional arguments passed to |
A filtered object, or a named list of filtered objects if
expanded = TRUE. In a filtered data frame, a column whose class has no
[ method keeps its attributes, such as a class that chooses its input.
library(S7) df <- data.frame( stringsAsFactors = FALSE, category = rep(letters[1:3], each = 4), value = 1:12, date = as.Date('2024-01-01') + 0:11 ) filters <- list( category = c("a", "b"), value = c(3, 11) ) # Apply filters with logical AND apply_filters(df, filters, filter_combine_method = "and") # Apply filters with logical OR apply_filters(df, filters, filter_combine_method = "or") # Get expanded filters apply_filters(df, filters, expanded = TRUE)library(S7) df <- data.frame( stringsAsFactors = FALSE, category = rep(letters[1:3], each = 4), value = 1:12, date = as.Date('2024-01-01') + 0:11 ) filters <- list( category = c("a", "b"), value = c(3, 11) ) # Apply filters with logical AND apply_filters(df, filters, filter_combine_method = "and") # Apply filters with logical OR apply_filters(df, filters, filter_combine_method = "or") # Get expanded filters apply_filters(df, filters, expanded = TRUE)
Provides the appropriate function arguments for the input function selected
by filterInput() or updateFilterInput().
args_filter_input(x, ...) args_update_filter_input(x, ...)args_filter_input(x, ...) args_update_filter_input(x, ...)
x |
The object being passed to |
... |
Additional arguments passed to the method. See details. |
The following arguments are supported in ...:
(Date, POSIXt). Logical. If TRUE, args_filter_input() will provide
the arguments for range date inputs. Only applies when x is of class
Date or POSIXt.
(character). Logical. If FALSE (the default), args_filter_input()
will provide the arguments for select inputs.
(character, factor, logical). Logical. If TRUE, the choices
provided to select inputs will not be modified. If FALSE (the default),
duplicate values will be removed and the choices will be sorted. Only
applies when x is of class character, factor, or logical.
If TRUE, indicates that the choices will be provided server-side. In
this case, arguments are not computed for args_filter_input(). Ignored
in args_update_filter_input().
An optional named list of arguments passed to unique(), called when x
is a character, factor, or logical, textbox = FALSE, and
choices_asis = FALSE.
An optional named list of arguments passed to sort(), which is called
after unique().
A named list of arguments for a shiny input function
args_filter_input(iris$Petal.Length)args_filter_input(iris$Petal.Length)
Internal function used to prepare input arguments using
args_filter_input(), and gracefully pass them to provided input function.
call_filter_input(x, .f, ...) call_update_filter_input(x, .f, ...)call_filter_input(x, .f, ...) call_update_filter_input(x, .f, ...)
x |
The object being passed to |
.f |
The input function to be called. |
... |
Arguments passed to either |
call_filter_input() and call_update_filter_input() are used when
customizing shinyfilters. For more, see
vignette("customizing-shinyfilters").
The result of calling the provided input function.
library(S7) library(shiny) # call_filter_input() is used inside filterInput() methods method(filterInput, class_numeric) <- function(x, ...) { call_filter_input(x, sliderInput, ...) } # call_update_filter_input() is used inside updateFilterInput() methods method(updateFilterInput, class_numeric) <- function(x, ...) { call_update_filter_input(x, updateSliderInput, ...) }library(S7) library(shiny) # call_filter_input() is used inside filterInput() methods method(filterInput, class_numeric) <- function(x, ...) { call_filter_input(x, sliderInput, ...) } # call_update_filter_input() is used inside updateFilterInput() methods method(updateFilterInput, class_numeric) <- function(x, ...) { call_update_filter_input(x, updateSliderInput, ...) }
Selects and creates a shiny input based the type of object x and
other arguments.
filterInput(x, ...)filterInput(x, ...)
x |
The object used to create the input. |
... |
Arguments used for input selection or passed to the selected input. See details. |
The following arguments passed to ... are supported:
(character). Logical. Controls whether to use textAreaInput
(TRUE) or textInput (FALSE, default). Only applies when
textbox is TRUE.
(character, factor, list, logical). Logical. Controls whether to use
radioButtons (TRUE) or a dropdown input (FALSE, default).
For character vectors, radio only applies if textbox is FALSE,
the default.
(Date, POSIXt). Logical. Controls whether to use dateRangeInput
(TRUE) or dateInput (FALSE, default).
(character, factor, list, logical). Logical. Controls whether to use
selectizeInput (TRUE) or selectInput
(FALSE, default). For character vectors, selectize only applies if
textbox is FALSE, the default.
(numeric). Logical. Controls whether to use sliderInput
(TRUE) or numericInput (FALSE, default).
(character). Logical. Controls whether to use a text input
(TRUE) or a dropdown input (FALSE, default).
An optional namespace created by NS(). Useful when using
filterInput() on a data.frame inside a shiny module.
Remaining arguments passed to ... are passed to the args_filter_input()
or the selected input function.
One of the following shiny inputs is returned, based on the
type of object passed to x, and other specified arguments. See
vignette("filter-input-catalog") for the full list of examples.
| Value | x |
Arguments |
| dateInput | Date, POSIXt | default |
| dateRangeInput | Date, POSIXt | range = TRUE |
| numericInput | numeric | default |
| radioButtons | character, factor, list, logical | radio = TRUE |
| selectInput | character, factor, list, logical | default |
| selectizeInput | character, factor, list, logical | selectize = TRUE |
| sliderInput | numeric | slider = TRUE |
| textAreaInput | character | textbox = TRUE, area = TRUE |
| textInput | character | textbox = TRUE
|
library(shiny) ui <- fluidPage( sidebarLayout( sidebarPanel( # Create a filterInput() inside a shiny app: filterInput( x = letters, inputId = "letter", label = "Pick a letter:" ) ), mainPanel( textOutput("selected_letter") ) ) ) server <- function(input, output, session) { output$selected_letter <- renderText({ paste("You selected:", input$letter) }) } shinyApp(ui, server)library(shiny) ui <- fluidPage( sidebarLayout( sidebarPanel( # Create a filterInput() inside a shiny app: filterInput( x = letters, inputId = "letter", label = "Pick a letter:" ) ), mainPanel( textOutput("selected_letter") ) ) ) server <- function(input, output, session) { output$selected_letter <- renderText({ paste("You selected:", input$letter) }) } shinyApp(ui, server)
Computes a logical vector indicating which elements of x match the filter
criteria specified by val.
get_filter_logical(x, val, ...)get_filter_logical(x, val, ...)
x |
An object to filter; typically a data.frame. |
val |
The filter criteria. |
... |
Arguments passed to methods. See details. |
The following arguments are supported in ...:
When x is a data.frame, column is the name of the
column intended to be filtered.
When x is a numeric or Date and val is a
length-one numeric or Date, comparison is the function used to
compare x with val. The default is <=.
When x is a numeric or Date and val is a length-two
numeric or Date, gte controls whether to use >= (TRUE, default)
or > (FALSE) on val[[1]].
When x is a numeric or Date and val is a length-two
numeric or Date, lte controls whether to use <= (TRUE, default)
or < (FALSE) on val[[2]].
When val is a character vector, such as the value of a select or radio
input, and x isn't a character, factor, or logical vector, the elements of
x are compared as text. A datetime x is compared as its date, whether
val is text, a Date, or a datetime.
A logical vector indicating which elements of x match the filter
criteria specified by val.
df <- data.frame( stringsAsFactors = FALSE, category = rep(letters[1:3], each = 4), value = 1:12, date = Sys.Date() + 0:11 ) # Filter character column get_filter_logical(df, c("a", "b"), column = "category") # Filter numeric column with single value get_filter_logical(df, 5, column = "value", comparison = `<=`) # Filter numeric column with range get_filter_logical(df, c(3, 8), column = "value", gte = TRUE, lte = FALSE)df <- data.frame( stringsAsFactors = FALSE, category = rep(letters[1:3], each = 4), value = 1:12, date = Sys.Date() + 0:11 ) # Filter character column get_filter_logical(df, c("a", "b"), column = "category") # Filter numeric column with single value get_filter_logical(df, 5, column = "value", comparison = `<=`) # Filter numeric column with range get_filter_logical(df, c(3, 8), column = "value", gte = TRUE, lte = FALSE)
Returns the (unnamespaced) ids of the inputs for the provided object.
get_input_ids(x, ...)get_input_ids(x, ...)
x |
An object for which to retrieve input ids; typically a data.frame. |
... |
Passed onto methods. |
A character vector of input ids.
df <- data.frame( stringsAsFactors = FALSE, name = c("Alice", "Bob"), age = c(25, 30), completed = c(TRUE, FALSE) ) get_input_ids(df)df <- data.frame( stringsAsFactors = FALSE, name = c("Alice", "Bob"), age = c(25, 30), completed = c(TRUE, FALSE) ) get_input_ids(df)
Returns the labels of the shiny inputs for the provided object.
get_input_labels(x, ...)get_input_labels(x, ...)
x |
An object for which to retrieve input labels; typically a data.frame. |
... |
Passed onto methods. |
A character vector of input labels
df <- data.frame( stringsAsFactors = FALSE, name = c("Alice", "Bob"), age = c(25, 30), completed = c(TRUE, FALSE) ) get_input_labels(df)df <- data.frame( stringsAsFactors = FALSE, name = c("Alice", "Bob"), age = c(25, 30), completed = c(TRUE, FALSE) ) get_input_labels(df)
Retrieves multiple input values from a shiny input object based on
the names provided in x.
get_input_values(input, x, ...)get_input_values(input, x, ...)
input |
A shiny |
x |
A character vector of input names, or a data.frame whose column
names are converted to input names via |
... |
Passed onto methods. |
A named list of input values corresponding to the names in x.
library(shiny) df <- data.frame( stringsAsFactors = FALSE, name = c("Alice", "Bob"), age = c(25, 30), completed = c(TRUE, FALSE) ) ui <- fluidPage( sidebarLayout( sidebarPanel( filterInput(df) ), mainPanel( verbatimTextOutput("output_all"), verbatimTextOutput("output_subset") ) ) ) server <- function(input, output, session) { output$output_all <- renderPrint({ get_input_values(input, df) }) output$output_subset <- renderPrint({ get_input_values(input, c("name", "completed")) }) } shinyApp(ui, server)library(shiny) df <- data.frame( stringsAsFactors = FALSE, name = c("Alice", "Bob"), age = c(25, 30), completed = c(TRUE, FALSE) ) ui <- fluidPage( sidebarLayout( sidebarPanel( filterInput(df) ), mainPanel( verbatimTextOutput("output_all"), verbatimTextOutput("output_subset") ) ) ) server <- function(input, output, session) { output$output_all <- renderPrint({ get_input_values(input, df) }) output$output_subset <- renderPrint({ get_input_values(input, c("name", "completed")) }) } shinyApp(ui, server)
A sample of 200 flights that departed New York City airports in 2013,
with a mix of column types for trying out filterInput().
nyc_flightsnyc_flights
A data frame with 200 rows and 7 columns:
Date of departure.
Two-letter airline code.
Origin airport: "EWR", "JFK", or "LGA".
Destination airport code.
Departure delay in minutes. Negative values are early
departures; NA for cancelled flights.
Distance between airports in miles.
Whether the flight arrived more than 15 minutes late; NA
when the arrival time is unknown.
A random sample of flights from the nycflights13 package
(https://github.com/tidyverse/nycflights13), released under CC0.
shinyfilters_server() runs an observer that makes your filters
interdependent. serverFilterInput() is an alias of
shinyfilters_server().
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() )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() )
x |
An object being filtered; typically the result of |
session |
The shiny session whose inputs are read and updated.
Defaults to the current session. The inputs of a configuration with a
namespace (see |
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
|
... |
Additional arguments passed to |
input |
Deprecated: omit it, or use |
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.
A reactiveValues list with two elements: filtered, the filtered
data, and input_values, the current filter input values as a named
list.
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)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)
shinyfilters() creates a set of interdependent filters for use in a
shiny application.
shinyfilters( .data, ..., area = FALSE, radio = FALSE, range = TRUE, selectize = TRUE, multiple = TRUE, slider = TRUE, textbox = FALSE, ns = NULL )shinyfilters( .data, ..., area = FALSE, radio = FALSE, range = TRUE, selectize = TRUE, multiple = TRUE, slider = TRUE, textbox = FALSE, ns = NULL )
.data |
A |
... |
Named arguments passed to the selected input. |
area |
(character). Logical. Controls whether to use textAreaInput
( |
radio |
(character, factor, list, logical). Logical. Controls whether
to use radioButtons ( |
range |
(Date, POSIXt). Logical. Controls whether to use
dateRangeInput ( |
selectize |
(character, factor, list, logical). Logical. Controls
whether to use selectizeInput ( |
multiple |
Passed to selectInput or selectizeInput. |
slider |
(numeric). Logical. Controls whether to use sliderInput
( |
textbox |
(character). Logical. Controls whether to use a text input
( |
ns |
An optional namespace created by |
A shinyfilters object.
shinyfilters(nyc_flights) # Use sliders for every numeric column shinyfilters(nyc_flights, slider = TRUE)shinyfilters(nyc_flights) # Use sliders for every numeric column shinyfilters(nyc_flights, slider = TRUE)
A shinyfilters object holds a data.frame and all of the data's filters. To
create one, call shinyfilters(). To modify one, use with_filters(),
with_args(), with_defaults(), or with_ns().
The object behaves much like the data.frame that defines it.
filters <- shinyfilters(nyc_flights)
names(filters) provides the names of the data.frame.
dim(filters), length(filters), nrow(filters), and ncol(filters)
describe the dimensions.
as.data.frame(filters) returns the data.
filters$col creates the input for one filter.
filters[[cols]] creates the inputs for all filters in cols.
filters[cols] subsets a shinyfilters object, returning only filters
matching cols.
shinyfilters supports <tidy-select> in both
[ and [[
A shinyfilters object can be placed directly inside a ui:
library(bslib) ui <- page_sidebar(sidebar = filters)
To enable interdependent filters, call shinyfilters_server in the server:
server <- function(...) {
shinyfilters_server(filters)
}
Sometimes, you will have to create the filters as you place them in the app. The following functions create filters:
[[
$
ui <- function(request) {
page_sidebar(sidebar = filterInput(filters))
}
Currently, it is known that you must create the filters when:
Bookmarking is enabled
When placing a filter inside bslib::accordion.
In these instances, shinyfilters will intercept and throw an informative error.
accordion(shinyfilters(nyc_flights)) #> Error: #> ! <shinyfilters> objects cannot be used in some shiny functions. #> i Use `[[`, `$`, or `filterInput()` to render the filters directly.
Printing shows the input each column receives:
shinyfilters(nyc_flights) #> <shinyfilters> * 7 filters #> #> Filters #> date <date> dateRangeInput #> carrier <chr> selectizeInput #> origin <fct> selectizeInput #> dest <chr> selectizeInput #> dep_delay <dbl> sliderInput #> distance <dbl> sliderInput #> delayed <lgl> selectizeInput
Any modifications will be displayed:
shinyfilters(nyc_flights, range = FALSE) |>
with_filters(date = "date") |>
with_ns("sidebar-mod")
#> <shinyfilters> * 7 filters * namespace "sidebar-mod"
#>
#> Filters
#> * date <date> dateInput
#> carrier <chr> selectizeInput
#> origin <fct> selectizeInput
#> dest <chr> selectizeInput
#> dep_delay <dbl> sliderInput
#> distance <dbl> sliderInput
#> delayed <lgl> selectizeInput
#>
#> Default Overrides
#> range = FALSE
#>
#> * Filter set by `with_filters()`
Updates a shiny input based the type of object x and other arguments.
updateFilterInput(x, ...)updateFilterInput(x, ...)
x |
The object used to create the input, or a configuration made by
|
... |
Arguments used for input selection or passed to the selected input update function. See details. |
The following arguments passed to ... are supported:
(character). Logical. Controls whether to use updateTextAreaInput
(TRUE) or updateTextInput (FALSE, default). Only applies when
textbox is TRUE.
(character, factor, list, logical). Logical. Controls whether to use
updateRadioButtons (TRUE) or a dropdown input update function
(FALSE, default). For character vectors, radio only applies if
textbox is FALSE, the default.
(Date, POSIXt). Logical. Controls whether to use updateDateRangeInput
(TRUE) or updateDateInput (FALSE, default).
(character, factor, list, logical). Logical. Controls whether to use
updateSelectizeInput (TRUE) or updateSelectInput
(FALSE, default). For character vectors, selectize only applies if
textbox is FALSE, the default.
(numeric). Logical. Controls whether to use updateSliderInput
(TRUE) or updateNumericInput (FALSE, default).
(character). Logical. Controls whether to update a text input
(TRUE) or a dropdown input (FALSE, default).
Remaining arguments passed to ... are passed to
args_update_filter_input() or the selected input update function.
When x is a configuration made by shinyfilters(), each column's input
is updated with the function and arguments that match the input
filterInput() creates for it: the configuration's defaults, and the input
and arguments with_filters() or with_args() set for it, computed from
the configuration's data. The arguments that set an input's value (value,
selected, start, and end) are left out, and the configuration's
namespace is applied to the ids.
The result of the following shiny input updates is returned,
based on the type of object passed to x, and other specified arguments.
| Value | x |
Arguments |
| updateDateInput | Date, POSIXt | default |
| updateDateRangeInput | Date, POSIXt | range = TRUE |
| updateNumericInput | numeric | default |
| updateRadioButtons | character, factor, list, logical | radio = TRUE |
| updateSelectInput | character, factor, list, logical | default |
| updateSelectizeInput | character, factor, list, logical | selectize = TRUE |
| updateSliderInput | numeric | slider = TRUE |
| updateTextAreaInput | character | textbox = TRUE, area = TRUE |
| updateTextInput | character | textbox = TRUE
|
library(shiny) fruits <- list( "a" = c("apples", "avocados"), "b" = c("bananas", "blueberries"), "c" = c("cherries", "cantaloupe") ) ui <- fluidPage( sidebarLayout( sidebarPanel( filterInput( x = letters[1:3], inputId = "letter", label = "Pick a letter:", multiple = TRUE ), filterInput( x = fruits, inputId = "fruits", label = "Pick a fruit:" ) ), mainPanel() ) ) server <- function(input, output, session) { observe({ fruits_filtered <- fruits if (!is.null(input$letter) && length(input$letter) != 0L) { fruits_filtered <- fruits[input$letter] } # Call updateFilterInput() inside the shiny server: updateFilterInput(x = fruits_filtered, inputId = "fruits") }) } shinyApp(ui, server)library(shiny) fruits <- list( "a" = c("apples", "avocados"), "b" = c("bananas", "blueberries"), "c" = c("cherries", "cantaloupe") ) ui <- fluidPage( sidebarLayout( sidebarPanel( filterInput( x = letters[1:3], inputId = "letter", label = "Pick a letter:", multiple = TRUE ), filterInput( x = fruits, inputId = "fruits", label = "Pick a fruit:" ) ), mainPanel() ) ) server <- function(input, output, session) { observe({ fruits_filtered <- fruits if (!is.null(input$letter) && length(input$letter) != 0L) { fruits_filtered <- fruits[input$letter] } # Call updateFilterInput() inside the shiny server: updateFilterInput(x = fruits_filtered, inputId = "fruits") }) } shinyApp(ui, server)
with_args() sets arguments for the inputs filterInput() creates for one
or more columns of a configuration made by shinyfilters(), such as a
slider's value, step, or width. Each column keeps its input.
with_args(.filters, ...)with_args(.filters, ...)
.filters |
A configuration created by |
... |
Any number of formulas.
Arguments replace the ones shinyfilters passes for the column, such as
A value can use |
Arguments stay with their column. Setting one again replaces it, and an
input chosen later with with_filters() keeps the ones it has an argument
for.
The updated configuration.
with_filters() to choose inputs, with_defaults() to set
arguments for every column.
filters <- shinyfilters(nyc_flights) # A range slider: `.x` is the column the input is created for filters <- with_args(filters, dep_delay ~ list(value = range(.x))) filters$dep_delay # Several arguments with_args(filters, dep_delay ~ list(value = range(.x), step = 5)) # The same arguments for several columns with_args(filters, c(dep_delay, distance) ~ list(value = range(.x))) # Different arguments for different columns with_args( filters, dep_delay ~ list(step = 5), origin ~ list(label = "Airport") )filters <- shinyfilters(nyc_flights) # A range slider: `.x` is the column the input is created for filters <- with_args(filters, dep_delay ~ list(value = range(.x))) filters$dep_delay # Several arguments with_args(filters, dep_delay ~ list(value = range(.x), step = 5)) # The same arguments for several columns with_args(filters, c(dep_delay, distance) ~ list(value = range(.x))) # Different arguments for different columns with_args( filters, dep_delay ~ list(step = 5), origin ~ list(label = "Airport") )
with_defaults() adds, replaces, or removes the arguments that a
configuration made by shinyfilters() passes to filterInput() for every
column.
with_defaults(.filters, ...)with_defaults(.filters, ...)
.filters |
A configuration created by |
... |
Named arguments passed to |
The updated configuration.
shinyfilters(), with_filters(), with_ns()
filters <- shinyfilters(nyc_flights, width = "200px") # Turn a default off filters <- with_defaults(filters, slider = FALSE) filters # Replace one with_defaults(filters, width = "100%") # Remove one with_defaults(filters, width = NULL)filters <- shinyfilters(nyc_flights, width = "200px") # Turn a default off filters <- with_defaults(filters, slider = FALSE) filters # Replace one with_defaults(filters, width = "100%") # Remove one with_defaults(filters, width = NULL)
with_filters() sets the input that filterInput() creates for one or more
columns of a configuration made by shinyfilters(), and adds or replaces
columns computed from the others. When a column's input is chosen more than
once, the last one wins; the arguments set for the column stay with it.
with_filters(.filters, ...)with_filters(.filters, ...)
.filters |
A configuration created by |
... |
Either two unnamed arguments, or any number of named arguments, or formulas:
Each input is either a keyword or the shiny input function it
stands for, such as Other functions are called like An input takes its arguments with it, wherever an input goes. Write
Arguments stay with the column: setting it again adds to them, replacing
those of the same name, and an input chosen later keeps the ones it has an
argument for. They are matched by name only, so set an argument again if
its value doesn't suit the new input. |
The updated configuration.
filters <- shinyfilters(nyc_flights) filters <- with_filters(filters, origin = "radio", carrier = "selectize") filters # Choose one input for several columns filters <- with_filters(filters, dep_delay:origin ~ "slider") # Give an input its arguments. `.x` is the column the input is for. with_filters(filters, dep_delay ~ "slider" ~ list(value = range(.x))) # Add a column computed from the others with_filters(filters, delay_sq = dep_delay^2) # Give one column the input it has by default filters <- shinyfilters(nyc_flights, slider = TRUE) with_filters(filters, distance = "numeric")filters <- shinyfilters(nyc_flights) filters <- with_filters(filters, origin = "radio", carrier = "selectize") filters # Choose one input for several columns filters <- with_filters(filters, dep_delay:origin ~ "slider") # Give an input its arguments. `.x` is the column the input is for. with_filters(filters, dep_delay ~ "slider" ~ list(value = range(.x))) # Add a column computed from the others with_filters(filters, delay_sq = dep_delay^2) # Give one column the input it has by default filters <- shinyfilters(nyc_flights, slider = TRUE) with_filters(filters, distance = "numeric")
with_ns() adds, replaces, or removes the namespace that a configuration
made by shinyfilters() applies to its input ids. Use it to build a
configuration once and reuse it in several modules.
with_ns(.filters, ns)with_ns(.filters, ns)
.filters |
A configuration created by |
ns |
The namespace: a string, used as the id passed to |
The updated configuration.
filters <- shinyfilters(nyc_flights) # Add a namespace filters <- with_ns(filters, "flights") filters # Replace it, here with a namespace created by NS() with_ns(filters, shiny::NS("departures")) # Remove it with_ns(filters, NULL)filters <- shinyfilters(nyc_flights) # Add a namespace filters <- with_ns(filters, "flights") filters # Replace it, here with a namespace created by NS() with_ns(filters, shiny::NS("departures")) # Remove it with_ns(filters, NULL)