【问题标题】:Updatepickerinput with change in pickerinput in Shiny使用 Shiny 中的 pickerinput 更改更新pickerinput
【发布时间】:2019-02-12 11:41:09
【问题描述】:

我想用另一个 PickerInput 中的更改来更新 Pickerinput。我如何在 Shiny 的服务器端执行此操作?

【问题讨论】:

  • 欢迎来到 SO。请向我们提供更多信息,并在可能的情况下提供一个可重复的最小示例,以便我们为您提供帮助。
  • 我有两个与 :choices = colnames(intangible_features[,c(30:34)]) 相同的选择器输入。如果用户从选择器输入中选择一个选项,另一个选择器输入也会在同一个仪表板上更新
  • 如前所述,最好使用包含最小可重现示例的代码 sn-p。请看stackoverflow.com/help/mcve

标签: r shiny


【解决方案1】:

您可以在服务器端使用observeEvent 函数来监控pickerInput #1 的状态,然后使用updatePickerInput 函数更新pickerInput #2。

请看下面的代码,它取pickerInput#1中的第一个字母,并相应地选择pickerInput#2的内容:

library("shiny")
library("shinyWidgets")

ui <- fluidPage(
  tags$h2("Update pickerInput"),

  fluidRow(
    column(
      width = 5, offset = 1,
      pickerInput(
        inputId = "p1",
        label = "Starting Letters",
        choices = LETTERS
      )
    ),
    column(
      width = 5,
      pickerInput(
        inputId = "p2",
        label = "Names of Cars",
        choices = ""
      )
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$p1, {
    updatePickerInput(session = session, inputId = "p2",
                      choices = grep(paste0("^",input$p1), rownames(mtcars), value = TRUE))

  }, ignoreInit = TRUE)
}

shinyApp(ui = ui, server = server)
}

输出:

【讨论】:

    猜你喜欢
    • 2020-11-25
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2021-10-21
    • 2020-05-02
    • 2018-11-09
    • 2023-02-13
    • 2022-12-04
    相关资源
    最近更新 更多