Skip to content

Commit

Permalink
updatePickerInput option disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Jul 15, 2017
1 parent 6c1a3a5 commit 4e23089
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 12 deletions.
100 changes: 94 additions & 6 deletions R/input-selectpicker.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#'
#' @param inputId The \code{input} slot that will be used to access the value.
#' @param label Display a text in the center of the switch.
#' @param choices List of values to select from. If elements of the list are named then that name rather than the value is displayed to the user.
#' @param selected The initially selected value (or multiple values if multiple = TRUE). If not specified then defaults to the first value for single-select lists and no values for multiple select lists.
#' @param choices List of values to select from. If elements of the
#' list are named then that name rather than the value is displayed to the user.
#' @param selected The initially selected value (or multiple values if multiple = TRUE).
#' If not specified then defaults to the first value for single-select lists
#' and no values for multiple select lists.
#' @param multiple Is selection of multiple items allowed?
#' @param options Options to customize the select picker
#' @param choicesOpt Options for choices in the dropdown menu
Expand Down Expand Up @@ -86,14 +89,91 @@ pickerInput <- function(inputId, label = NULL, choices, selected = NULL, multipl
#' @param session The session object passed to function given to shinyServer.
#' @param inputId The id of the input object.
#' @param label Display a text in the center of the switch.
#' @param choices List of values to select from. If elements of the list are named then that name rather than the value is displayed to the user.
#' @param selected The initially selected value (or multiple values if multiple = TRUE). If not specified then defaults to the first value for single-select lists and no values for multiple select lists.
#' @param choices List of values to select from. If elements of the list are named
#' then that name rather than the value is displayed to the user.
#' @param selected The initially selected value (or multiple values if multiple = TRUE).
#' If not specified then defaults to the first value for single-select lists
#' and no values for multiple select lists.
#' @param choicesOpt Options for choices in the dropdown menu
#'
#' @importFrom utils capture.output
#' @export


#'
#' @examples
#' \dontrun{
#' if (interactive()) {
#'
#' library("shiny")
#' library("shinyWidgets")
#'
#' ui <- fluidPage(
#' tags$h2("Update pickerInput"),
#'
#' fluidRow(
#' column(
#' width = 5, offset = 1,
#' pickerInput(
#' inputId = "p1",
#' label = "classic update",
#' choices = rownames(mtcars)
#' )
#' ),
#' column(
#' width = 5,
#' pickerInput(
#' inputId = "p2",
#' label = "disabled update",
#' choices = rownames(mtcars)
#' )
#' )
#' ),
#'
#' fluidRow(
#' column(
#' width = 10, offset = 1,
#' sliderInput(
#' inputId = "up",
#' label = "Select between models with mpg greater than :",
#' width = "50%",
#' min = min(mtcars$mpg),
#' max = max(mtcars$mpg),
#' value = min(mtcars$mpg),
#' step = 0.1
#' )
#' )
#' )
#'
#' )
#'
#' server <- function(input, output, session) {
#'
#' observeEvent(input$up, {
#' mtcars2 <- mtcars[mtcars$mpg >= input$up, ]
#'
#' # Method 1
#' updatePickerInput(session = session, inputId = "p1",
#' choices = rownames(mtcars2))
#'
#' # Method 2
#' disabled_choices <- !rownames(mtcars) %in% rownames(mtcars2)
#' updatePickerInput(
#' session = session, inputId = "p2",
#' choices = rownames(mtcars),
#' choicesOpt = list(
#' disabled = disabled_choices,
#' style = ifelse(disabled_choices,
#' yes = "color: rgba(119, 119, 119, 0.5);",
#' no = "")
#' )
#' )
#' }, ignoreInit = TRUE)
#'
#' }
#'
#' shinyApp(ui = ui, server = server)
#'
#' }
#' }
updatePickerInput <- function (session, inputId, label = NULL, selected = NULL, choices = NULL, choicesOpt = NULL) {
choices <- if (!is.null(choices))
choicesWithNames(choices)
Expand All @@ -109,6 +189,13 @@ updatePickerInput <- function (session, inputId, label = NULL, selected = NULL,



#' Generate pickerInput options
#'
#' @param choices a named list
#' @param selected selected value if any
#' @param choicesOpt additional option ofr choices
#'
#' @noRd
pickerOptions <- function (choices, selected = NULL, choicesOpt = NULL)
{
if (is.null(choicesOpt))
Expand Down Expand Up @@ -141,6 +228,7 @@ pickerOptions <- function (choices, selected = NULL, choicesOpt = NULL)
`data-icon` = choicesOpt$icon[i],
`data-subtext` = choicesOpt$subtext[i],
`data-content` = choicesOpt$content[i],
disabled = if (!is.null(choicesOpt$disabled[i]) && choicesOpt$disabled[i]) "disabled",
selected = if (choice %in% selected) "selected" else NULL
)
# optionTag$attribs <- c(optionTag$attribs, list(if (choice %in% selected) " selected" else ""))
Expand Down
12 changes: 10 additions & 2 deletions inst/examples/shinyWidgets/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ function(input, output, session) {
})


# Update mamterialSwitch ----



# Update materialSwitch ----
output$resupMaterial <- renderPrint({
input$upMaterial
})
Expand All @@ -135,7 +138,12 @@ function(input, output, session) {
updateMaterialSwitch(session = session, inputId = "upMaterial", value = FALSE)
})

# Update mamterialSwitch ----





# Update PickerInput ----
output$resuppickerIcons <- renderPrint({
input$uppickerIcons
})
Expand Down
7 changes: 5 additions & 2 deletions man/pickerInput.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 81 additions & 2 deletions man/updatePickerInput.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4e23089

Please sign in to comment.