Skip to content

Commit

Permalink
refactor: simplify update_dropdown_input
Browse files Browse the repository at this point in the history
  • Loading branch information
TymekDev committed Mar 20, 2024
1 parent 16e6244 commit 948bd53
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions R/dropdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,14 @@ selectInput <- function(inputId, label, choices, selected = NULL, multiple = FAL
#'
#' @export
update_dropdown_input <- function(session, input_id, choices = NULL, choices_value = choices, value = NULL) {
if (!is.null(value)) value <- paste(as.character(value), collapse = ",") else value <- NULL
msg <- list()
if (!is.null(value)) {
msg$value <- paste(as.character(value), collapse = ",") # NOTE: paste() converts character(0) to ""
}
if (!is.null(choices)) {
options <- jsonlite::toJSON(list(values = data.frame(name = choices, text = choices, value = choices_value)))
} else {
options <- NULL
msg$options <- jsonlite::toJSON(list(values = data.frame(name = choices, text = choices, value = choices_value)))
}

message <- list(choices = options, value = value)
message <- message[!vapply(message, is.null, FUN.VALUE = logical(1))]

session$sendInputMessage(input_id, message)
session$sendInputMessage(input_id, msg)
}

#' Change the value of a select input on the client
Expand Down

0 comments on commit 948bd53

Please sign in to comment.