View source: R/choices_selected.R
choices_selected | R Documentation |
Construct a single list containing available choices, the default selected value, and additional settings such as to order the choices with the selected elements appearing first or whether to block the user from making selections.
Can be used in UI input elements such as teal.widgets::optionalSelectInput()
.
choices_selected(
choices,
selected = if (inherits(choices, "delayed_data")) NULL else choices[1],
keep_order = FALSE,
fixed = FALSE
)
is.choices_selected(x)
choices |
( See |
selected |
( If |
keep_order |
( |
fixed |
(optional |
x |
( |
Please note that the order of selected will always follow the order of choices. The keep_order
argument is set to false which will run the following code inside:
choices <- c(selected, setdiff(choices, selected))
In case you want to keep your specific order of choices, set keep_order
to TRUE
.
choices_selected
returns list of choices_selected
, encapsulating the specified
choices
, selected
, keep_order
and fixed
.
is.choices_selected
returns TRUE
if x
inherits from a choices_selected
object, FALSE
otherwise.
is.choices_selected()
: Check if an object is a choices_selected class
library(shiny)
library(teal.widgets)
# all_choices example - semantically the same objects
choices_selected(choices = letters, selected = all_choices())
choices_selected(choices = letters, selected = letters)
choices_selected(
choices = setNames(LETTERS[1:5], paste("Letter", LETTERS[1:5])),
selected = "C"
)
ADSL <- teal.transform::rADSL
choices_selected(variable_choices(ADSL), "SEX")
# How to select nothing
# use an empty character
choices_selected(
choices = c("", "A", "B", "C"),
selected = ""
)
# How to allow the user to select nothing
# use an empty character
choices_selected(
choices = c("A", "", "B", "C"),
selected = "A"
)
# How to make Nothing the Xth choice
# just use keep_order
choices_selected(
choices = c("A", "", "B", "C"),
selected = "A",
keep_order = TRUE
)
# How to give labels to selections
# by adding names - choices will be replaced by "name" in UI, not in code
choices_selected(
choices = c("name for A" = "A", "Name for nothing" = "", "name for b" = "B", "name for C" = "C"),
selected = "A"
)
# by using choices_labeled
# labels will be shown behind the choice
choices_selected(
choices = choices_labeled(
c("A", "", "B", "C"),
c("name for A", "nothing", "name for B", "name for C")
),
selected = "A"
)
# Passing a `delayed_data` object to `selected`
choices_selected(
choices = variable_choices("ADSL"),
selected = variable_choices("ADSL", subset = c("STUDYID"))
)
# functional form (subsetting for factor variables only) of choices_selected
# with delayed data loading
choices_selected(variable_choices("ADSL", subset = function(data) {
idx <- vapply(data, is.factor, logical(1))
names(data)[idx]
}))
cs <- choices_selected(
choices = c("A", "B", "C"),
selected = "A"
)
ui <- fluidPage(
optionalSelectInput(
inputId = "id",
choices = cs$choices,
selected = cs$selected
)
)
server <- function(input, output, session) {}
if (interactive()) {
shinyApp(ui, server)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.