需要为pickerInput选择输入

时间:2018-10-23 16:14:43

标签: r input shiny

如何要求用户从pickerInput中选择输入?

这是一个基本示例:

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

ui <- fluidPage(
  column(
    width = 4,
      pickerInput(inputId = "fruit", 
                  label = "Fruits", 
                  choices = c("Apple", "Mango", "Pear", "Orange"),
                  options = list(`actions-box` = T, 
                                 `none-selected-text` = "Please make a selection!"),
                  multiple = T)
        ))
server <- function(input, output) {
   output$res <- renderPrint({
   input$fruit
   })
}

shinyApp(ui = ui, server = server)

创建pickerInput菜单时是否可以添加一个设置菜单的选项,以便菜单始终需要输入?

1 个答案:

答案 0 :(得分:0)

您可以简单地对其进行更新,我们还可以添加一个弹出窗口,指示必须至少选择1个元素

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

mychoices <- c("Apple", "Mango", "Pear", "Orange")

ui <- fluidPage(
  column(
    width = 4,
    pickerInput(inputId = "fruit", 
                label = "Fruits", 
                choices = mychoices,
                options = list(`actions-box` = T, `none-selected-text` = "Please make a selection!",selected = mychoices[1]),
                multiple = T)
  ),
  column(4,textOutput("res"))

)
server <- function(input, output,session) {

  data <- eventReactive(input$fruit,{
    if(is.null(input$fruit)){
      updatePickerInput(session,"fruit",choices = mychoices,selected = mychoices[1])
      showNotification("At least 1 should be selected", duration = 2,type = c("error"))
    }
    input$fruit
  },ignoreNULL = F)

  output$res <- renderPrint({
    req(data())
    data()
  })
}

shinyApp(ui = ui, server = server)