mirror of
https://github.com/rstudio/shiny.git
synced 2026-04-07 03:00:20 -04:00
replace choicesNames with choiceNames, and choicesValues with choiceValues
This commit is contained in:
@@ -11,13 +11,13 @@
|
||||
#' must not be provided, and vice-versa.
|
||||
#' @param selected The values that should be initially selected, if any.
|
||||
#' @param inline If \code{TRUE}, render the choices inline (i.e. horizontally)
|
||||
#' @param choicesNames,choicesValues List of names and values, respectively,
|
||||
#' @param choiceNames,choiceValues List of names and values, respectively,
|
||||
#' that are displayed to the user in the app and correspond to the each
|
||||
#' choice (for this reason, \code{choicesNames} and \code{choicesValues}
|
||||
#' choice (for this reason, \code{choiceNames} and \code{choiceValues}
|
||||
#' must have the same length). If either of these arguments is
|
||||
#' provided, then the other \emph{must} be provided and \code{choices}
|
||||
#' \emph{must not} be provided. The advantage of using both of these over
|
||||
#' a named list for \code{choices} is that \code{choicesNames} allows any
|
||||
#' a named list for \code{choices} is that \code{choiceNames} allows any
|
||||
#' type of UI object to be passed through (tag objects, icons, HTML code,
|
||||
#' ...), instead of just simple text. See Examples.
|
||||
#'
|
||||
@@ -48,10 +48,10 @@
|
||||
#'
|
||||
#' ui <- fluidPage(
|
||||
#' checkboxGroupInput("icons", "Choose icons:",
|
||||
#' choicesNames =
|
||||
#' choiceNames =
|
||||
#' list(icon("calendar"), icon("bed"),
|
||||
#' icon("cog"), icon("bug")),
|
||||
#' choicesValues =
|
||||
#' choiceValues =
|
||||
#' list("calendar", "bed", "cog", "bug")
|
||||
#' ),
|
||||
#' textOutput("txt")
|
||||
@@ -68,19 +68,19 @@
|
||||
#' }
|
||||
#' @export
|
||||
checkboxGroupInput <- function(inputId, label, choices = NULL, selected = NULL,
|
||||
inline = FALSE, width = NULL, choicesNames = NULL, choicesValues = NULL) {
|
||||
inline = FALSE, width = NULL, choiceNames = NULL, choiceValues = NULL) {
|
||||
|
||||
args <- normalizeChoicesArgs(choices, choicesNames, choicesValues)
|
||||
args <- normalizeChoicesArgs(choices, choiceNames, choiceValues)
|
||||
|
||||
selected <- restoreInput(id = inputId, default = selected)
|
||||
|
||||
# default value if it's not specified
|
||||
if (!is.null(selected))
|
||||
selected <- normalizeSelected(selected, inputId,
|
||||
args$choicesNames, args$choicesValues)
|
||||
args$choiceNames, args$choiceValues)
|
||||
|
||||
options <- generateOptions(inputId, selected, inline,
|
||||
'checkbox', args$choicesNames, args$choicesValues)
|
||||
'checkbox', args$choiceNames, args$choiceValues)
|
||||
|
||||
divClass <- "form-group shiny-input-checkboxgroup shiny-input-container"
|
||||
if (inline)
|
||||
|
||||
@@ -18,13 +18,13 @@
|
||||
#' defaults to the first value)
|
||||
#' @param inline If \code{TRUE}, render the choices inline (i.e. horizontally)
|
||||
#' @return A set of radio buttons that can be added to a UI definition.
|
||||
#' @param choicesNames,choicesValues List of names and values, respectively,
|
||||
#' @param choiceNames,choiceValues List of names and values, respectively,
|
||||
#' that are displayed to the user in the app and correspond to the each
|
||||
#' choice (for this reason, \code{choicesNames} and \code{choicesValues}
|
||||
#' choice (for this reason, \code{choiceNames} and \code{choiceValues}
|
||||
#' must have the same length). If either of these arguments is
|
||||
#' provided, then the other \emph{must} be provided and \code{choices}
|
||||
#' \emph{must not} be provided. The advantage of using both of these over
|
||||
#' a named list for \code{choices} is that \code{choicesNames} allows any
|
||||
#' a named list for \code{choices} is that \code{choiceNames} allows any
|
||||
#' type of UI object to be passed through (tag objects, icons, HTML code,
|
||||
#' ...), instead of just simple text. See Examples.
|
||||
#'
|
||||
@@ -61,12 +61,12 @@
|
||||
#'
|
||||
#' ui <- fluidPage(
|
||||
#' radioButtons("rb", "Choose one:",
|
||||
#' choicesNames = list(
|
||||
#' choiceNames = list(
|
||||
#' icon("calendar"),
|
||||
#' HTML("<p style='color:red;'>Red Text</p>"),
|
||||
#' "Normal text"
|
||||
#' ),
|
||||
#' choicesValues = list(
|
||||
#' choiceValues = list(
|
||||
#' "icon", "html", "text"
|
||||
#' )),
|
||||
#' textOutput("txt")
|
||||
@@ -82,21 +82,21 @@
|
||||
#' }
|
||||
#' @export
|
||||
radioButtons <- function(inputId, label, choices = NULL, selected = NULL,
|
||||
inline = FALSE, width = NULL, choicesNames = NULL, choicesValues = NULL) {
|
||||
inline = FALSE, width = NULL, choiceNames = NULL, choiceValues = NULL) {
|
||||
|
||||
args <- normalizeChoicesArgs(choices, choicesNames, choicesValues)
|
||||
args <- normalizeChoicesArgs(choices, choiceNames, choiceValues)
|
||||
|
||||
selected <- restoreInput(id = inputId, default = selected)
|
||||
|
||||
# default value if it's not specified
|
||||
selected <- if (is.null(selected)) args$choicesValues[[1]] else {
|
||||
normalizeSelected(selected, inputId, args$choicesNames, args$choicesValues)
|
||||
selected <- if (is.null(selected)) args$choiceValues[[1]] else {
|
||||
normalizeSelected(selected, inputId, args$choiceNames, args$choiceValues)
|
||||
}
|
||||
|
||||
if (length(selected) > 1) stop("The 'selected' argument must be of length 1")
|
||||
|
||||
options <- generateOptions(inputId, selected, inline, 'radio',
|
||||
args$choicesNames, args$choicesValues)
|
||||
args$choiceNames, args$choiceValues)
|
||||
|
||||
divClass <- "form-group shiny-input-radiogroup shiny-input-container"
|
||||
if (inline) divClass <- paste(divClass, "shiny-input-container-inline")
|
||||
|
||||
@@ -2,37 +2,37 @@ controlLabel <- function(controlName, label) {
|
||||
label %AND% tags$label(class = "control-label", `for` = controlName, label)
|
||||
}
|
||||
|
||||
normalizeChoicesArgs <- function(choices, choicesNames, choicesValues) {
|
||||
# if-else to check that either choices OR (choicesNames + choicesValues)
|
||||
normalizeChoicesArgs <- function(choices, choiceNames, choiceValues) {
|
||||
# if-else to check that either choices OR (choiceNames + choiceValues)
|
||||
# were correctly provided
|
||||
if (is.null(choices)) {
|
||||
if (length(choicesNames) == 0 || length(choicesValues) == 0) {
|
||||
if (length(choiceNames) == 0 || length(choiceValues) == 0) {
|
||||
stop("Please specify a non-empty vector for `choices` (or,
|
||||
alternatively, for both `choicesNames` and `choicesValues`).")
|
||||
alternatively, for both `choiceNames` and `choiceValues`).")
|
||||
}
|
||||
if (length(choicesNames) != length(choicesValues)) {
|
||||
stop("`choicesNames` and `choicesValues` must have the same length.")
|
||||
if (length(choiceNames) != length(choiceValues)) {
|
||||
stop("`choiceNames` and `choiceValues` must have the same length.")
|
||||
}
|
||||
if (anyNamed(choicesNames) || anyNamed(choicesValues)) {
|
||||
stop("`choicesNames` and `choicesValues` must not be named.")
|
||||
if (anyNamed(choiceNames) || anyNamed(choiceValues)) {
|
||||
stop("`choiceNames` and `choiceValues` must not be named.")
|
||||
}
|
||||
} else {
|
||||
if (!is.null(choicesNames) || !is.null(choicesValues)) {
|
||||
warning("Using `choices` argument; ignoring `choicesNames` and
|
||||
`choicesValues`.")
|
||||
if (!is.null(choiceNames) || !is.null(choiceValues)) {
|
||||
warning("Using `choices` argument; ignoring `choiceNames` and
|
||||
`choiceValues`.")
|
||||
}
|
||||
choices <- choicesWithNames(choices) # resolve names if not specified
|
||||
choicesNames <- names(choices)
|
||||
choicesValues <- unname(choices)
|
||||
choiceNames <- names(choices)
|
||||
choiceValues <- unname(choices)
|
||||
}
|
||||
|
||||
return(list(choicesNames = choicesNames, choicesValues = choicesValues))
|
||||
return(list(choiceNames = choiceNames, choiceValues = choiceValues))
|
||||
}
|
||||
|
||||
# Before shiny 0.9, `selected` refers to names/labels of `choices`; now it
|
||||
# refers to values. Below is a function for backward compatibility. It also
|
||||
# coerces the value to `character`.
|
||||
normalizeSelected <- function(selected, inputId, choicesNames, choicesValues) {
|
||||
normalizeSelected <- function(selected, inputId, choiceNames, choiceValues) {
|
||||
# this line accomplishes two tings:
|
||||
# - coerces selected to character
|
||||
# - drops name, otherwise toJSON() keeps it too
|
||||
@@ -40,17 +40,17 @@ normalizeSelected <- function(selected, inputId, choicesNames, choicesValues) {
|
||||
|
||||
# if you are using optgroups, you're using shiny > 0.10.0, and you should
|
||||
# already know that `selected` must be a value instead of a label
|
||||
if (needOptgroup(choicesValues)) return(selected)
|
||||
if (needOptgroup(choiceValues)) return(selected)
|
||||
|
||||
if (is.list(choicesNames)) choicesNames <- unlist(choicesNames)
|
||||
if (is.list(choicesValues)) choicesValues <- unlist(choicesValues)
|
||||
if (is.list(choiceNames)) choiceNames <- unlist(choiceNames)
|
||||
if (is.list(choiceValues)) choiceValues <- unlist(choiceValues)
|
||||
|
||||
# when selected labels instead of values
|
||||
i <- (selected %in% choicesNames) & !(selected %in% choicesValues)
|
||||
i <- (selected %in% choiceNames) & !(selected %in% choiceValues)
|
||||
if (any(i)) {
|
||||
warnFun <- if (all(i)) {
|
||||
# replace names with values
|
||||
selected <- choicesValues[[which(choicesNames == selected)]]
|
||||
selected <- choiceValues[[which(choiceNames == selected)]]
|
||||
warning
|
||||
} else stop # stop when it is ambiguous (some labels == values)
|
||||
warnFun("'selected' must be the values instead of names of 'choices' ",
|
||||
@@ -62,11 +62,11 @@ normalizeSelected <- function(selected, inputId, choicesNames, choicesValues) {
|
||||
# generate options for radio buttons and checkbox groups (type = 'checkbox' or
|
||||
# 'radio')
|
||||
generateOptions <- function(inputId, selected, inline, type = 'checkbox',
|
||||
choicesNames, choicesValues,
|
||||
choiceNames, choiceValues,
|
||||
session = getDefaultReactiveDomain()) {
|
||||
# generate a list of <input type=? [checked] />
|
||||
options <- mapply(
|
||||
choicesValues, choicesNames,
|
||||
choiceValues, choiceNames,
|
||||
FUN = function(value, name) {
|
||||
inputTag <- tags$input(
|
||||
type = type, name = inputId, value = value
|
||||
|
||||
@@ -453,17 +453,17 @@ updateSliderInput <- function(session, inputId, label = NULL, value = NULL,
|
||||
|
||||
updateInputOptions <- function(session, inputId, label = NULL, choices = NULL,
|
||||
selected = NULL, inline = FALSE, type = NULL,
|
||||
choicesNames = NULL, choicesValues = NULL) {
|
||||
args <- normalizeChoicesArgs(choices, choicesNames, choicesValues)
|
||||
choiceNames = NULL, choiceValues = NULL) {
|
||||
args <- normalizeChoicesArgs(choices, choiceNames, choiceValues)
|
||||
|
||||
if (!is.null(selected))
|
||||
selected <- normalizeSelected(selected, session$ns(inputId),
|
||||
args$choicesNames, args$choicesValues)
|
||||
args$choiceNames, args$choiceValues)
|
||||
|
||||
options <- if (!is.null(args$choicesValues)) {
|
||||
options <- if (!is.null(args$choiceValues)) {
|
||||
format(tagList(
|
||||
generateOptions(session$ns(inputId), selected, inline, type,
|
||||
args$choicesNames, args$choicesValues)
|
||||
args$choiceNames, args$choiceValues)
|
||||
))
|
||||
}
|
||||
|
||||
@@ -513,9 +513,9 @@ updateInputOptions <- function(session, inputId, label = NULL, choices = NULL,
|
||||
#' @export
|
||||
updateCheckboxGroupInput <- function(session, inputId, label = NULL,
|
||||
choices = NULL, selected = NULL, inline = FALSE,
|
||||
choicesNames = NULL, choicesValues = NULL) {
|
||||
choiceNames = NULL, choiceValues = NULL) {
|
||||
updateInputOptions(session, inputId, label, choices, selected,
|
||||
inline, "checkbox", choicesNames, choicesValues)
|
||||
inline, "checkbox", choiceNames, choiceValues)
|
||||
}
|
||||
|
||||
|
||||
@@ -556,11 +556,11 @@ updateCheckboxGroupInput <- function(session, inputId, label = NULL,
|
||||
#' @export
|
||||
updateRadioButtons <- function(session, inputId, label = NULL, choices = NULL,
|
||||
selected = NULL, inline = FALSE,
|
||||
choicesNames = NULL, choicesValues = NULL) {
|
||||
choiceNames = NULL, choiceValues = NULL) {
|
||||
# you must select at least one radio button
|
||||
if (is.null(selected) && !is.null(choices)) selected <- choices[[1]]
|
||||
updateInputOptions(session, inputId, label, choices, selected,
|
||||
inline, 'radio', choicesNames, choicesValues)
|
||||
inline, 'radio', choiceNames, choiceValues)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user