| 1 |
#' Change the value of a text input on the client |
| 2 |
#' |
| 3 |
#' @template update-input |
| 4 |
#' @inheritParams textInput |
| 5 |
#' |
| 6 |
#' @seealso [textInput()] |
| 7 |
#' |
| 8 |
#' @examples |
| 9 |
#' ## Only run examples in interactive R sessions |
| 10 |
#' if (interactive()) { |
| 11 |
#' |
| 12 |
#' ui <- fluidPage( |
| 13 |
#' sliderInput("controller", "Controller", 0, 20, 10), |
| 14 |
#' textInput("inText", "Input text"), |
| 15 |
#' textInput("inText2", "Input text 2") |
| 16 |
#' ) |
| 17 |
#' |
| 18 |
#' server <- function(input, output, session) { |
| 19 |
#' observe({ |
| 20 |
#' # We'll use the input$controller variable multiple times, so save it as x |
| 21 |
#' # for convenience. |
| 22 |
#' x <- input$controller |
| 23 |
#' |
| 24 |
#' # This will change the value of input$inText, based on x |
| 25 |
#' updateTextInput(session, "inText", value = paste("New text", x)) |
| 26 |
#' |
| 27 |
#' # Can also set the label, this time for input$inText2 |
| 28 |
#' updateTextInput(session, "inText2", |
| 29 |
#' label = paste("New label", x), |
| 30 |
#' value = paste("New text", x)) |
| 31 |
#' }) |
| 32 |
#' } |
| 33 |
#' |
| 34 |
#' shinyApp(ui, server) |
| 35 |
#' } |
| 36 |
#' @export |
| 37 |
updateTextInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, value = NULL, placeholder = NULL) { |
| 38 |
validate_session_object(session) |
| 39 |
|
| 40 |
message <- dropNulls(list( |
| 41 |
label = if (!is.null(label)) processDeps(label, session), |
| 42 |
value = value, |
| 43 |
placeholder = placeholder |
| 44 |
)) |
| 45 |
session$sendInputMessage(inputId, message) |
| 46 |
} |
| 47 |
|
| 48 |
#' Change the value of a textarea input on the client |
| 49 |
#' |
| 50 |
#' @template update-input |
| 51 |
#' @inheritParams updateTextInput |
| 52 |
#' |
| 53 |
#' @seealso [textAreaInput()] |
| 54 |
#' |
| 55 |
#' @examples |
| 56 |
#' ## Only run examples in interactive R sessions |
| 57 |
#' if (interactive()) { |
| 58 |
#' |
| 59 |
#' ui <- fluidPage( |
| 60 |
#' sliderInput("controller", "Controller", 0, 20, 10), |
| 61 |
#' textAreaInput("inText", "Input textarea"), |
| 62 |
#' textAreaInput("inText2", "Input textarea 2") |
| 63 |
#' ) |
| 64 |
#' |
| 65 |
#' server <- function(input, output, session) { |
| 66 |
#' observe({ |
| 67 |
#' # We'll use the input$controller variable multiple times, so save it as x |
| 68 |
#' # for convenience. |
| 69 |
#' x <- input$controller |
| 70 |
#' |
| 71 |
#' # This will change the value of input$inText, based on x |
| 72 |
#' updateTextAreaInput(session, "inText", value = paste("New text", x)) |
| 73 |
#' |
| 74 |
#' # Can also set the label, this time for input$inText2 |
| 75 |
#' updateTextAreaInput(session, "inText2", |
| 76 |
#' label = paste("New label", x), |
| 77 |
#' value = paste("New text", x)) |
| 78 |
#' }) |
| 79 |
#' } |
| 80 |
#' |
| 81 |
#' shinyApp(ui, server) |
| 82 |
#' } |
| 83 |
#' @export |
| 84 |
updateTextAreaInput <- updateTextInput |
| 85 |
|
| 86 |
|
| 87 |
#' Change the value of a checkbox input on the client |
| 88 |
#' |
| 89 |
#' @template update-input |
| 90 |
#' @inheritParams checkboxInput |
| 91 |
#' |
| 92 |
#' @seealso [checkboxInput()] |
| 93 |
#' |
| 94 |
#' @examples |
| 95 |
#' ## Only run examples in interactive R sessions |
| 96 |
#' if (interactive()) { |
| 97 |
#' |
| 98 |
#' ui <- fluidPage( |
| 99 |
#' sliderInput("controller", "Controller", 0, 1, 0, step = 1), |
| 100 |
#' checkboxInput("inCheckbox", "Input checkbox") |
| 101 |
#' ) |
| 102 |
#' |
| 103 |
#' server <- function(input, output, session) { |
| 104 |
#' observe({ |
| 105 |
#' # TRUE if input$controller is odd, FALSE if even. |
| 106 |
#' x_even <- input$controller %% 2 == 1 |
| 107 |
#' |
| 108 |
#' updateCheckboxInput(session, "inCheckbox", value = x_even) |
| 109 |
#' }) |
| 110 |
#' } |
| 111 |
#' |
| 112 |
#' shinyApp(ui, server) |
| 113 |
#' } |
| 114 |
#' @export |
| 115 |
updateCheckboxInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, value = NULL) { |
| 116 |
validate_session_object(session) |
| 117 |
|
| 118 |
message <- dropNulls(list( |
| 119 |
label = if (!is.null(label)) processDeps(label, session), |
| 120 |
value = value |
| 121 |
)) |
| 122 |
session$sendInputMessage(inputId, message) |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
#' Change the label or icon of an action button on the client |
| 127 |
#' |
| 128 |
#' @template update-input |
| 129 |
#' @param disabled If `TRUE`, the button will not be clickable; if `FALSE`, it |
| 130 |
#' will be. |
| 131 |
#' @inheritParams actionButton |
| 132 |
#' |
| 133 |
#' @seealso [actionButton()] |
| 134 |
#' |
| 135 |
#' @examples |
| 136 |
#' ## Only run examples in interactive R sessions |
| 137 |
#' if (interactive()) { |
| 138 |
#' |
| 139 |
#' ui <- fluidPage( |
| 140 |
#' actionButton("update", "Update other buttons and link"), |
| 141 |
#' br(), |
| 142 |
#' actionButton("goButton", "Go"), |
| 143 |
#' br(), |
| 144 |
#' actionButton("goButton2", "Go 2", icon = icon("area-chart")), |
| 145 |
#' br(), |
| 146 |
#' actionButton("goButton3", "Go 3"), |
| 147 |
#' br(), |
| 148 |
#' actionLink("goLink", "Go Link") |
| 149 |
#' ) |
| 150 |
#' |
| 151 |
#' server <- function(input, output, session) { |
| 152 |
#' observe({ |
| 153 |
#' req(input$update) |
| 154 |
#' |
| 155 |
#' # Updates goButton's label and icon |
| 156 |
#' updateActionButton(session, "goButton", |
| 157 |
#' label = "New label", |
| 158 |
#' icon = icon("calendar")) |
| 159 |
#' |
| 160 |
#' # Leaves goButton2's label unchanged and |
| 161 |
#' # removes its icon |
| 162 |
#' updateActionButton(session, "goButton2", |
| 163 |
#' icon = character(0)) |
| 164 |
#' |
| 165 |
#' # Leaves goButton3's icon, if it exists, |
| 166 |
#' # unchanged and changes its label |
| 167 |
#' updateActionButton(session, "goButton3", |
| 168 |
#' label = "New label 3") |
| 169 |
#' |
| 170 |
#' # Updates goLink's label and icon |
| 171 |
#' updateActionButton(session, "goLink", |
| 172 |
#' label = "New link label", |
| 173 |
#' icon = icon("link")) |
| 174 |
#' }) |
| 175 |
#' } |
| 176 |
#' |
| 177 |
#' shinyApp(ui, server) |
| 178 |
#' } |
| 179 |
#' @rdname updateActionButton |
| 180 |
#' @export |
| 181 |
updateActionButton <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, icon = NULL, disabled = NULL) { |
| 182 |
validate_session_object(session) |
| 183 |
|
| 184 |
message <- dropNulls(list( |
| 185 |
label = if (!is.null(label)) processDeps(label, session), |
| 186 |
icon = if (!is.null(icon)) processDeps(validateIcon(icon), session), |
| 187 |
disabled = disabled |
| 188 |
)) |
| 189 |
session$sendInputMessage(inputId, message) |
| 190 |
} |
| 191 |
#' @rdname updateActionButton |
| 192 |
#' @export |
| 193 |
updateActionLink <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, icon = NULL) { |
| 194 |
updateActionButton(session, inputId=inputId, label=label, icon=icon) |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
#' Change the value of a date input on the client |
| 199 |
#' |
| 200 |
#' @template update-input |
| 201 |
#' @inheritParams dateInput |
| 202 |
#' |
| 203 |
#' @seealso [dateInput()] |
| 204 |
#' |
| 205 |
#' @examples |
| 206 |
#' ## Only run examples in interactive R sessions |
| 207 |
#' if (interactive()) { |
| 208 |
#' |
| 209 |
#' ui <- fluidPage( |
| 210 |
#' sliderInput("n", "Day of month", 1, 30, 10), |
| 211 |
#' dateInput("inDate", "Input date") |
| 212 |
#' ) |
| 213 |
#' |
| 214 |
#' server <- function(input, output, session) { |
| 215 |
#' observe({ |
| 216 |
#' date <- as.Date(paste0("2013-04-", input$n)) |
| 217 |
#' updateDateInput(session, "inDate", |
| 218 |
#' label = paste("Date label", input$n), |
| 219 |
#' value = date, |
| 220 |
#' min = date - 3, |
| 221 |
#' max = date + 3 |
| 222 |
#' ) |
| 223 |
#' }) |
| 224 |
#' } |
| 225 |
#' |
| 226 |
#' shinyApp(ui, server) |
| 227 |
#' } |
| 228 |
#' @export |
| 229 |
updateDateInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, value = NULL, |
| 230 |
min = NULL, max = NULL) |
| 231 |
{ |
| 232 |
validate_session_object(session) |
| 233 |
|
| 234 |
value <- dateYMD(value, "value") |
| 235 |
min <- dateYMD(min, "min") |
| 236 |
max <- dateYMD(max, "max") |
| 237 |
|
| 238 |
message <- dropNulls(list( |
| 239 |
label = if (!is.null(label)) processDeps(label, session), |
| 240 |
value = value, |
| 241 |
min = min, |
| 242 |
max = max |
| 243 |
)) |
| 244 |
session$sendInputMessage(inputId, message) |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
#' Change the start and end values of a date range input on the client |
| 249 |
#' |
| 250 |
#' @template update-input |
| 251 |
#' @inheritParams dateRangeInput |
| 252 |
#' |
| 253 |
#' @seealso [dateRangeInput()] |
| 254 |
#' |
| 255 |
#' @examples |
| 256 |
#' ## Only run examples in interactive R sessions |
| 257 |
#' if (interactive()) { |
| 258 |
#' |
| 259 |
#' ui <- fluidPage( |
| 260 |
#' sliderInput("n", "Day of month", 1, 30, 10), |
| 261 |
#' dateRangeInput("inDateRange", "Input date range") |
| 262 |
#' ) |
| 263 |
#' |
| 264 |
#' server <- function(input, output, session) { |
| 265 |
#' observe({ |
| 266 |
#' date <- as.Date(paste0("2013-04-", input$n)) |
| 267 |
#' |
| 268 |
#' updateDateRangeInput(session, "inDateRange", |
| 269 |
#' label = paste("Date range label", input$n), |
| 270 |
#' start = date - 1, |
| 271 |
#' end = date + 1, |
| 272 |
#' min = date - 5, |
| 273 |
#' max = date + 5 |
| 274 |
#' ) |
| 275 |
#' }) |
| 276 |
#' } |
| 277 |
#' |
| 278 |
#' shinyApp(ui, server) |
| 279 |
#' } |
| 280 |
#' @export |
| 281 |
updateDateRangeInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, |
| 282 |
start = NULL, end = NULL, min = NULL, |
| 283 |
max = NULL) |
| 284 |
{ |
| 285 |
validate_session_object(session) |
| 286 |
|
| 287 |
start <- dateYMD(start, "start") |
| 288 |
end <- dateYMD(end, "end") |
| 289 |
min <- dateYMD(min, "min") |
| 290 |
max <- dateYMD(max, "max") |
| 291 |
|
| 292 |
message <- dropNulls(list( |
| 293 |
label = if (!is.null(label)) processDeps(label, session), |
| 294 |
value = dropNulls(list(start = start, end = end)), |
| 295 |
min = min, |
| 296 |
max = max |
| 297 |
)) |
| 298 |
|
| 299 |
session$sendInputMessage(inputId, message) |
| 300 |
} |
| 301 |
|
| 302 |
#' Change the selected tab on the client |
| 303 |
#' |
| 304 |
#' @param session The `session` object passed to function given to |
| 305 |
#' `shinyServer`. Default is `getDefaultReactiveDomain()`. |
| 306 |
#' @param inputId The id of the `tabsetPanel`, `navlistPanel`, |
| 307 |
#' or `navbarPage` object. |
| 308 |
#' @inheritParams tabsetPanel |
| 309 |
#' |
| 310 |
#' @seealso [tabsetPanel()], [navlistPanel()], |
| 311 |
#' [navbarPage()] |
| 312 |
#' |
| 313 |
#' @examples |
| 314 |
#' ## Only run examples in interactive R sessions |
| 315 |
#' if (interactive()) { |
| 316 |
#' |
| 317 |
#' ui <- fluidPage(sidebarLayout( |
| 318 |
#' sidebarPanel( |
| 319 |
#' sliderInput("controller", "Controller", 1, 3, 1) |
| 320 |
#' ), |
| 321 |
#' mainPanel( |
| 322 |
#' tabsetPanel(id = "inTabset", |
| 323 |
#' tabPanel(title = "Panel 1", value = "panel1", "Panel 1 content"), |
| 324 |
#' tabPanel(title = "Panel 2", value = "panel2", "Panel 2 content"), |
| 325 |
#' tabPanel(title = "Panel 3", value = "panel3", "Panel 3 content") |
| 326 |
#' ) |
| 327 |
#' ) |
| 328 |
#' )) |
| 329 |
#' |
| 330 |
#' server <- function(input, output, session) { |
| 331 |
#' observeEvent(input$controller, { |
| 332 |
#' updateTabsetPanel(session, "inTabset", |
| 333 |
#' selected = paste0("panel", input$controller) |
| 334 |
#' ) |
| 335 |
#' }) |
| 336 |
#' } |
| 337 |
#' |
| 338 |
#' shinyApp(ui, server) |
| 339 |
#' } |
| 340 |
#' @export |
| 341 |
updateTabsetPanel <- function(session = getDefaultReactiveDomain(), inputId, selected = NULL) { |
| 342 |
validate_session_object(session) |
| 343 |
|
| 344 |
message <- dropNulls(list(value = selected)) |
| 345 |
session$sendInputMessage(inputId, message) |
| 346 |
} |
| 347 |
|
| 348 |
#' @rdname updateTabsetPanel |
| 349 |
#' @export |
| 350 |
updateNavbarPage <- updateTabsetPanel |
| 351 |
|
| 352 |
#' @rdname updateTabsetPanel |
| 353 |
#' @export |
| 354 |
updateNavlistPanel <- updateTabsetPanel |
| 355 |
|
| 356 |
#' Change the value of a number input on the client |
| 357 |
#' |
| 358 |
#' @template update-input |
| 359 |
#' @inheritParams numericInput |
| 360 |
#' |
| 361 |
#' @seealso [numericInput()] |
| 362 |
#' |
| 363 |
#' @examples |
| 364 |
#' ## Only run examples in interactive R sessions |
| 365 |
#' if (interactive()) { |
| 366 |
#' |
| 367 |
#' ui <- fluidPage( |
| 368 |
#' sliderInput("controller", "Controller", 0, 20, 10), |
| 369 |
#' numericInput("inNumber", "Input number", 0), |
| 370 |
#' numericInput("inNumber2", "Input number 2", 0) |
| 371 |
#' ) |
| 372 |
#' |
| 373 |
#' server <- function(input, output, session) { |
| 374 |
#' |
| 375 |
#' observeEvent(input$controller, { |
| 376 |
#' # We'll use the input$controller variable multiple times, so save it as x |
| 377 |
#' # for convenience. |
| 378 |
#' x <- input$controller |
| 379 |
#' |
| 380 |
#' updateNumericInput(session, "inNumber", value = x) |
| 381 |
#' |
| 382 |
#' updateNumericInput(session, "inNumber2", |
| 383 |
#' label = paste("Number label ", x), |
| 384 |
#' value = x, min = x-10, max = x+10, step = 5) |
| 385 |
#' }) |
| 386 |
#' } |
| 387 |
#' |
| 388 |
#' shinyApp(ui, server) |
| 389 |
#' } |
| 390 |
#' @export |
| 391 |
updateNumericInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, value = NULL, |
| 392 |
min = NULL, max = NULL, step = NULL) { |
| 393 |
|
| 394 |
validate_session_object(session) |
| 395 |
|
| 396 |
message <- dropNulls(list( |
| 397 |
label = if (!is.null(label)) processDeps(label, session), |
| 398 |
value = formatNoSci(value), |
| 399 |
min = formatNoSci(min), |
| 400 |
max = formatNoSci(max), |
| 401 |
step = formatNoSci(step) |
| 402 |
)) |
| 403 |
session$sendInputMessage(inputId, message) |
| 404 |
} |
| 405 |
|
| 406 |
#' Update Slider Input Widget |
| 407 |
#' |
| 408 |
#' Change the value of a slider input on the client. |
| 409 |
#' |
| 410 |
#' @template update-input |
| 411 |
#' @inheritParams sliderInput |
| 412 |
#' |
| 413 |
#' @seealso [sliderInput()] |
| 414 |
#' |
| 415 |
#' @examples |
| 416 |
#' ## Only run this example in interactive R sessions |
| 417 |
#' if (interactive()) { |
| 418 |
#' shinyApp( |
| 419 |
#' ui = fluidPage( |
| 420 |
#' sidebarLayout( |
| 421 |
#' sidebarPanel( |
| 422 |
#' p("The first slider controls the second"), |
| 423 |
#' sliderInput("control", "Controller:", min=0, max=20, value=10, |
| 424 |
#' step=1), |
| 425 |
#' sliderInput("receive", "Receiver:", min=0, max=20, value=10, |
| 426 |
#' step=1) |
| 427 |
#' ), |
| 428 |
#' mainPanel() |
| 429 |
#' ) |
| 430 |
#' ), |
| 431 |
#' server = function(input, output, session) { |
| 432 |
#' observe({ |
| 433 |
#' val <- input$control |
| 434 |
#' # Control the value, min, max, and step. |
| 435 |
#' # Step size is 2 when input value is even; 1 when value is odd. |
| 436 |
#' updateSliderInput(session, "receive", value = val, |
| 437 |
#' min = floor(val/2), max = val+4, step = (val+1)%%2 + 1) |
| 438 |
#' }) |
| 439 |
#' } |
| 440 |
#' ) |
| 441 |
#' } |
| 442 |
#' @export |
| 443 |
updateSliderInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, value = NULL, |
| 444 |
min = NULL, max = NULL, step = NULL, timeFormat = NULL, timezone = NULL) |
| 445 |
{ |
| 446 |
validate_session_object(session) |
| 447 |
|
| 448 |
if (!is.null(value)) { |
| 449 |
if (!is.null(min) && !is.null(max)) { |
| 450 |
# Validate value/min/max together if all three are provided |
| 451 |
tryCatch( |
| 452 |
validate_slider_value(min, max, value, "updateSliderInput"), |
| 453 |
error = function(err) warning(conditionMessage(err), call. = FALSE) |
| 454 |
) |
| 455 |
} else if (length(value) < 1 || length(value) > 2 || any(is.na(value))) { |
| 456 |
# Otherwise ensure basic assumptions about value are met |
| 457 |
warning( |
| 458 |
"In updateSliderInput(): value must be a single value or a length-2 ", |
| 459 |
"vector and cannot contain NA values.", |
| 460 |
call. = FALSE |
| 461 |
) |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
# If no min/max/value is provided, we won't know the |
| 466 |
# type, and this will return an empty string |
| 467 |
dataType <- getSliderType(min, max, value) |
| 468 |
|
| 469 |
if (is.null(timeFormat)) { |
| 470 |
timeFormat <- switch(dataType, date = "%F", datetime = "%F %T", number = NULL) |
| 471 |
} |
| 472 |
|
| 473 |
if (isTRUE(dataType %in% c("date", "datetime"))) { |
| 474 |
to_ms <- function(x) 1000 * as.numeric(as.POSIXct(x)) |
| 475 |
if (!is.null(min)) min <- to_ms(min) |
| 476 |
if (!is.null(max)) max <- to_ms(max) |
| 477 |
if (!is.null(value)) value <- to_ms(value) |
| 478 |
} |
| 479 |
|
| 480 |
message <- dropNulls(list( |
| 481 |
label = if (!is.null(label)) processDeps(label, session), |
| 482 |
value = formatNoSci(value), |
| 483 |
min = formatNoSci(min), |
| 484 |
max = formatNoSci(max), |
| 485 |
step = formatNoSci(step), |
| 486 |
`data-type` = dataType, |
| 487 |
`time-format` = timeFormat, |
| 488 |
timezone = timezone |
| 489 |
)) |
| 490 |
session$sendInputMessage(inputId, message) |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
updateInputOptions <- function(session, inputId, label = NULL, choices = NULL, |
| 495 |
selected = NULL, inline = FALSE, type = NULL, |
| 496 |
choiceNames = NULL, choiceValues = NULL) { |
| 497 |
validate_session_object(session) |
| 498 |
|
| 499 |
if (is.null(type)) stop("Please specify the type ('checkbox' or 'radio')") |
| 500 |
|
| 501 |
args <- normalizeChoicesArgs(choices, choiceNames, choiceValues, mustExist = FALSE) |
| 502 |
|
| 503 |
if (!is.null(selected)) selected <- as.character(selected) |
| 504 |
|
| 505 |
options <- if (!is.null(args$choiceValues)) { |
| 506 |
format(tagList( |
| 507 |
generateOptions(session$ns(inputId), selected, inline, type, |
| 508 |
args$choiceNames, args$choiceValues) |
| 509 |
)) |
| 510 |
} |
| 511 |
|
| 512 |
message <- dropNulls(list( |
| 513 |
label = if (!is.null(label)) processDeps(label, session), |
| 514 |
options = options, |
| 515 |
value = selected |
| 516 |
)) |
| 517 |
|
| 518 |
session$sendInputMessage(inputId, message) |
| 519 |
} |
| 520 |
|
| 521 |
#' Change the value of a checkbox group input on the client |
| 522 |
#' |
| 523 |
#' @template update-input |
| 524 |
#' @inheritParams checkboxGroupInput |
| 525 |
#' |
| 526 |
#' @seealso [checkboxGroupInput()] |
| 527 |
#' |
| 528 |
#' @examples |
| 529 |
#' ## Only run examples in interactive R sessions |
| 530 |
#' if (interactive()) { |
| 531 |
#' |
| 532 |
#' ui <- fluidPage( |
| 533 |
#' p("The first checkbox group controls the second"), |
| 534 |
#' checkboxGroupInput("inCheckboxGroup", "Input checkbox", |
| 535 |
#' c("Item A", "Item B", "Item C")), |
| 536 |
#' checkboxGroupInput("inCheckboxGroup2", "Input checkbox 2", |
| 537 |
#' c("Item A", "Item B", "Item C")) |
| 538 |
#' ) |
| 539 |
#' |
| 540 |
#' server <- function(input, output, session) { |
| 541 |
#' observe({ |
| 542 |
#' x <- input$inCheckboxGroup |
| 543 |
#' |
| 544 |
#' # Can use character(0) to remove all choices |
| 545 |
#' if (is.null(x)) |
| 546 |
#' x <- character(0) |
| 547 |
#' |
| 548 |
#' # Can also set the label and select items |
| 549 |
#' updateCheckboxGroupInput(session, "inCheckboxGroup2", |
| 550 |
#' label = paste("Checkboxgroup label", length(x)), |
| 551 |
#' choices = x, |
| 552 |
#' selected = x |
| 553 |
#' ) |
| 554 |
#' }) |
| 555 |
#' } |
| 556 |
#' |
| 557 |
#' shinyApp(ui, server) |
| 558 |
#' } |
| 559 |
#' @export |
| 560 |
updateCheckboxGroupInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, |
| 561 |
choices = NULL, selected = NULL, inline = FALSE, |
| 562 |
choiceNames = NULL, choiceValues = NULL) |
| 563 |
{ |
| 564 |
validate_session_object(session) |
| 565 |
|
| 566 |
updateInputOptions(session, inputId, label, choices, selected, |
| 567 |
inline, "checkbox", choiceNames, choiceValues) |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
#' Change the value of a radio input on the client |
| 572 |
#' |
| 573 |
#' @template update-input |
| 574 |
#' @inheritParams radioButtons |
| 575 |
#' |
| 576 |
#' @seealso [radioButtons()] |
| 577 |
#' |
| 578 |
#' @examples |
| 579 |
#' ## Only run examples in interactive R sessions |
| 580 |
#' if (interactive()) { |
| 581 |
#' |
| 582 |
#' ui <- fluidPage( |
| 583 |
#' p("The first radio button group controls the second"), |
| 584 |
#' radioButtons("inRadioButtons", "Input radio buttons", |
| 585 |
#' c("Item A", "Item B", "Item C")), |
| 586 |
#' radioButtons("inRadioButtons2", "Input radio buttons 2", |
| 587 |
#' c("Item A", "Item B", "Item C")) |
| 588 |
#' ) |
| 589 |
#' |
| 590 |
#' server <- function(input, output, session) { |
| 591 |
#' observe({ |
| 592 |
#' x <- input$inRadioButtons |
| 593 |
#' |
| 594 |
#' # Can also set the label and select items |
| 595 |
#' updateRadioButtons(session, "inRadioButtons2", |
| 596 |
#' label = paste("radioButtons label", x), |
| 597 |
#' choices = x, |
| 598 |
#' selected = x |
| 599 |
#' ) |
| 600 |
#' }) |
| 601 |
#' } |
| 602 |
#' |
| 603 |
#' shinyApp(ui, server) |
| 604 |
#' } |
| 605 |
#' @export |
| 606 |
updateRadioButtons <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, choices = NULL, |
| 607 |
selected = NULL, inline = FALSE, |
| 608 |
choiceNames = NULL, choiceValues = NULL) |
| 609 |
{ |
| 610 |
validate_session_object(session) |
| 611 |
|
| 612 |
# you must select at least one radio button |
| 613 |
if (is.null(selected)) { |
| 614 |
if (!is.null(choices)) selected <- choices[[1]] |
| 615 |
else if (!is.null(choiceValues)) selected <- choiceValues[[1]] |
| 616 |
} |
| 617 |
updateInputOptions(session, inputId, label, choices, selected, |
| 618 |
inline, 'radio', choiceNames, choiceValues) |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
#' Change the value of a select input on the client |
| 623 |
#' |
| 624 |
#' @template update-input |
| 625 |
#' @inheritParams selectInput |
| 626 |
#' |
| 627 |
#' @seealso [selectInput()] [varSelectInput()] |
| 628 |
#' |
| 629 |
#' @examples |
| 630 |
#' ## Only run examples in interactive R sessions |
| 631 |
#' if (interactive()) { |
| 632 |
#' |
| 633 |
#' ui <- fluidPage( |
| 634 |
#' p("The checkbox group controls the select input"), |
| 635 |
#' checkboxGroupInput("inCheckboxGroup", "Input checkbox", |
| 636 |
#' c("Item A", "Item B", "Item C")), |
| 637 |
#' selectInput("inSelect", "Select input", |
| 638 |
#' c("Item A", "Item B", "Item C")) |
| 639 |
#' ) |
| 640 |
#' |
| 641 |
#' server <- function(input, output, session) { |
| 642 |
#' observe({ |
| 643 |
#' x <- input$inCheckboxGroup |
| 644 |
#' |
| 645 |
#' # Can use character(0) to remove all choices |
| 646 |
#' if (is.null(x)) |
| 647 |
#' x <- character(0) |
| 648 |
#' |
| 649 |
#' # Can also set the label and select items |
| 650 |
#' updateSelectInput(session, "inSelect", |
| 651 |
#' label = paste("Select input label", length(x)), |
| 652 |
#' choices = x, |
| 653 |
#' selected = tail(x, 1) |
| 654 |
#' ) |
| 655 |
#' }) |
| 656 |
#' } |
| 657 |
#' |
| 658 |
#' shinyApp(ui, server) |
| 659 |
#' } |
| 660 |
#' @export |
| 661 |
updateSelectInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, choices = NULL, |
| 662 |
selected = NULL) |
| 663 |
{ |
| 664 |
validate_session_object(session) |
| 665 |
|
| 666 |
choices <- if (!is.null(choices)) choicesWithNames(choices) |
| 667 |
if (!is.null(selected)) selected <- as.character(selected) |
| 668 |
options <- if (!is.null(choices)) selectOptions(choices, selected, inputId, FALSE) |
| 669 |
message <- dropNulls(list( |
| 670 |
label = if (!is.null(label)) processDeps(label, session), |
| 671 |
options = options, |
| 672 |
value = selected |
| 673 |
)) |
| 674 |
session$sendInputMessage(inputId, message) |
| 675 |
} |
| 676 |
|
| 677 |
#' @rdname updateSelectInput |
| 678 |
#' @inheritParams selectizeInput |
| 679 |
#' @param server whether to store `choices` on the server side, and load |
| 680 |
#' the select options dynamically on searching, instead of writing all |
| 681 |
#' `choices` into the page at once (i.e., only use the client-side |
| 682 |
#' version of \pkg{selectize.js}) |
| 683 |
#' @export |
| 684 |
updateSelectizeInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, choices = NULL, |
| 685 |
selected = NULL, options = list(), |
| 686 |
server = FALSE) |
| 687 |
{ |
| 688 |
validate_session_object(session) |
| 689 |
|
| 690 |
if (length(options)) { |
| 691 |
res <- checkAsIs(options) |
| 692 |
cfg <- tags$script( |
| 693 |
type = 'application/json', |
| 694 |
`data-for` = session$ns(inputId), |
| 695 |
`data-eval` = if (length(res$eval)) HTML(toJSON(res$eval)), |
| 696 |
HTML(toJSON(res$options)) |
| 697 |
) |
| 698 |
session$sendInputMessage(inputId, list(config = as.character(cfg))) |
| 699 |
} |
| 700 |
if (!server) { |
| 701 |
return(updateSelectInput(session, inputId, label, choices, selected)) |
| 702 |
} |
| 703 |
|
| 704 |
noOptGroup <- TRUE |
| 705 |
if (is.list(choices)) { |
| 706 |
# check if list is nested |
| 707 |
for (i in seq_along(choices)) { |
| 708 |
if (is.list(choices[[i]]) || length(choices[[i]]) > 1) { |
| 709 |
noOptGroup <- FALSE |
| 710 |
break() |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
# convert choices to a data frame so it returns [{label: , value: , optgroup: },...] |
| 715 |
choices <- if (is.data.frame(choices)) { |
| 716 |
# jcheng 2018/09/25: I don't think we ever said data frames were OK to pass |
| 717 |
# to updateSelectInput, but one of the example apps does this and at least |
| 718 |
# one user noticed when we broke it. |
| 719 |
# https://github.com/rstudio/shiny/issues/2172 |
| 720 |
# https://github.com/rstudio/shiny/issues/2192 |
| 721 |
as.data.frame(choices, stringsAsFactors = FALSE) |
| 722 |
} else if (is.atomic(choices) || noOptGroup) { |
| 723 |
# fast path for vectors and flat lists |
| 724 |
if (is.list(choices)) { |
| 725 |
choices <- unlist(choices) |
| 726 |
} |
| 727 |
if (is.null(names(choices))) { |
| 728 |
lab <- as.character(choices) |
| 729 |
} else { |
| 730 |
lab <- names(choices) |
| 731 |
# replace empty names like: choices = c(a = 1, 2) |
| 732 |
# in this case: names(choices) = c("a", "") |
| 733 |
# with replacement below choices will be: lab = c("a", "2") |
| 734 |
empty_names_indices <- lab == "" |
| 735 |
lab[empty_names_indices] <- as.character(choices[empty_names_indices]) |
| 736 |
} |
| 737 |
data.frame(label = lab, value = choices, stringsAsFactors = FALSE) |
| 738 |
} else { |
| 739 |
# slow path for nested lists/optgroups |
| 740 |
list_names <- names(choices) |
| 741 |
if (is.null(list_names)) { |
| 742 |
list_names <- rep("", length(choices)) |
| 743 |
} |
| 744 |
|
| 745 |
choice_list <- mapply(choices, list_names, FUN = function (choice, name) { |
| 746 |
group <- "" |
| 747 |
lab <- name |
| 748 |
if (lab == "") lab <- as.character(choice) |
| 749 |
|
| 750 |
if (is.list(choice) || length(choice) > 1) { |
| 751 |
group <- rep(name, length(choice)) |
| 752 |
choice <- unlist(choice) |
| 753 |
|
| 754 |
if (is.null(names(choice))) { |
| 755 |
lab <- as.character(choice) |
| 756 |
} else { |
| 757 |
lab <- names(choice) |
| 758 |
# replace empty names like: choices = c(a = 1, 2) |
| 759 |
# in this case: names(choices) = c("a", "") |
| 760 |
# with replacement below choices will be: lab = c("a", "2") |
| 761 |
empty_names_indices <- lab == "" |
| 762 |
lab[empty_names_indices] <- as.character(choice[empty_names_indices]) |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
list( |
| 767 |
label = lab, |
| 768 |
value = as.character(choice), |
| 769 |
# The name "optgroup" is because this is the default field where |
| 770 |
# selectize will look for group IDs |
| 771 |
optgroup = group |
| 772 |
) |
| 773 |
}, SIMPLIFY = FALSE) |
| 774 |
|
| 775 |
|
| 776 |
extract_vector <- function(x, name) { |
| 777 |
vecs <- lapply(x, `[[`, name) |
| 778 |
do.call(c, vecs) |
| 779 |
} |
| 780 |
|
| 781 |
data.frame( |
| 782 |
label = extract_vector(choice_list, "label"), |
| 783 |
value = extract_vector(choice_list, "value"), |
| 784 |
optgroup = extract_vector(choice_list, "optgroup"), |
| 785 |
stringsAsFactors = FALSE, row.names = NULL |
| 786 |
) |
| 787 |
} |
| 788 |
|
| 789 |
value <- unname(selected) |
| 790 |
attr(choices, 'selected_value') <- value |
| 791 |
|
| 792 |
message <- dropNulls(list( |
| 793 |
label = label, |
| 794 |
value = value, |
| 795 |
url = session$registerDataObj(inputId, choices, selectizeJSON) |
| 796 |
)) |
| 797 |
session$sendInputMessage(inputId, message) |
| 798 |
} |
| 799 |
#' @rdname updateSelectInput |
| 800 |
#' @inheritParams varSelectInput |
| 801 |
#' @export |
| 802 |
updateVarSelectInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, data = NULL, selected = NULL) { |
| 803 |
validate_session_object(session) |
| 804 |
|
| 805 |
if (is.null(data)) { |
| 806 |
choices <- NULL |
| 807 |
} else { |
| 808 |
choices <- colnames(data) |
| 809 |
} |
| 810 |
|
| 811 |
updateSelectInput( |
| 812 |
session = session, |
| 813 |
inputId = inputId, |
| 814 |
label = label, |
| 815 |
choices = choices, |
| 816 |
selected = selected |
| 817 |
) |
| 818 |
} |
| 819 |
#' @rdname updateSelectInput |
| 820 |
#' @export |
| 821 |
updateVarSelectizeInput <- function(session = getDefaultReactiveDomain(), inputId, label = NULL, |
| 822 |
data = NULL, selected = NULL, options = list(), server = FALSE) |
| 823 |
{ |
| 824 |
validate_session_object(session) |
| 825 |
|
| 826 |
if (is.null(data)) { |
| 827 |
choices <- NULL |
| 828 |
} else { |
| 829 |
choices <- colnames(data) |
| 830 |
} |
| 831 |
updateSelectizeInput( |
| 832 |
session = session, |
| 833 |
inputId = inputId, |
| 834 |
label = label, |
| 835 |
choices = choices, |
| 836 |
selected = selected, |
| 837 |
options = options, |
| 838 |
server = server |
| 839 |
) |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
selectizeJSON <- function(data, req) { |
| 845 |
query <- parseQueryString(req$QUERY_STRING) |
| 846 |
|
| 847 |
# extract the query variables, conjunction (and/or), search string, maximum options |
| 848 |
var <- c(safeFromJSON(query$field)) |
| 849 |
|
| 850 |
# all keywords in lower-case, for case-insensitive matching |
| 851 |
key <- unique(strsplit(tolower(query$query), '\\s+')[[1]]) |
| 852 |
|
| 853 |
if (identical(key, '')) key <- character(0) |
| 854 |
mop <- as.numeric(query$maxop) |
| 855 |
vfd <- query$value # the value field name |
| 856 |
sel <- attr(data, 'selected_value', exact = TRUE) |
| 857 |
|
| 858 |
# start searching for keywords in all specified columns |
| 859 |
idx <- logical(nrow(data)) |
| 860 |
if (length(key)) { |
| 861 |
for (v in var) { |
| 862 |
matches <- do.call( |
| 863 |
cbind, |
| 864 |
lapply(key, function(k) { |
| 865 |
grepl(k, tolower(as.character(data[[v]])), fixed = TRUE) |
| 866 |
}) |
| 867 |
) |
| 868 |
# merge column matches using OR, and match multiple keywords in one column |
| 869 |
# using the conjunction setting (AND or OR) |
| 870 |
matches <- rowSums(matches) |
| 871 |
if (query$conju == 'and') |
| 872 |
idx <- idx | (matches == length(key)) |
| 873 |
else |
| 874 |
idx <- idx | matches |
| 875 |
} |
| 876 |
} |
| 877 |
# only return the first n rows (n = maximum options in configuration) |
| 878 |
idx <- utils::head(if (length(key)) which(idx) else seq_along(idx), mop) |
| 879 |
# make sure the selected value is in the data |
| 880 |
if (length(sel)) { |
| 881 |
i <- stats::na.omit(match(sel, data[, vfd])) |
| 882 |
if (length(i)) idx <- sort(utils::head(unique(c(i, idx)), mop)) |
| 883 |
} |
| 884 |
data <- data[idx, ] |
| 885 |
|
| 886 |
res <- toJSON(columnToRowData(data)) |
| 887 |
httpResponse(200, 'application/json', enc2utf8(res)) |
| 888 |
} |