【问题标题】:Generate warning message on R Shiny Dashboard Sidebar using "SelectizeInput"使用“SelectizeInput”在 R Shiny Dashboard Sidebar 上生成警告消息
【发布时间】:2019-10-08 13:07:37
【问题描述】:

如果用户输入了无法识别的内容,我想在闪亮的仪表板侧边栏中添加一条警告消息。我发现了一些非常有用的信息:Check Shiny inputs and generate warning on sidebar layout 但这并不是我所需要的,我想听听你的想法。下面是我的代码

library(shiny)
ui <- dashboardPage(

  dashboardHeader(),
  dashboardSidebar(
    selectizeInput('email', 'Email', c("NYC@gmail.com", "LA@gmail.com","SF@gmail.com"), multiple = FALSE,
                   options = list(
                     placeholder = 'Email addresss',
                     onInitialize = I('function() { this.setValue(""); }')
                   )),
    uiOutput('email_text')
  ),
  dashboardBody()
)

server <- function(input, output) {
  output$email_text <-
    renderUI({
      if(input$email == ""){
        return(p("Please add your gmail e-mail address."))
      }

      #Update: Below checks for "gmail" - I would something to search list and return.
      if(!grepl("gmail", input$email)){
        return(p("Your email is not a gmail e-mail address!"))
      }

    })
}

shinyApp(ui = ui, server = server)

只要我从下拉列表中选择,当前的侧边栏选择就可以很好地识别电子邮件格式

但是,我还想补充的是,如果我输入了不期望的内容(不在给定列表中),系统可以捕获并警告我(例如“您的电子邮件不是预期的电子邮件地址! ”)。目前,如果我只是输入一些不在列表中的内容,系统不会执行任何操作:

我觉得和我上面提到的帖子相比,我的版本有“selectizeInput”功能的问题。它旨在从列表中获取元素,而不是用户输入的所有内容。有没有办法解决它?我尝试使用 validate() 但没有运气。

非常感谢您的帮助!

【问题讨论】:

    标签: r shiny shinydashboard selectize.js


    【解决方案1】:

    默认情况下selecticizeInput 不允许用户输入新值。您必须使用options = list(create = TRUE) 启用此功能。拥有此选项后,您可以使用%in% 检查新创建的电子邮件是否在预定义的电子邮件列表中,并在侧边栏中报告自定义错误消息。

    这是更新后的代码:

    library(shiny)
    library(shinydashboard)
    
    list_of_emails <- c("NYC@gmail.com", "LA@gmail.com", "SF@gmail.com")
    
    ui <- dashboardPage(dashboardHeader(),
                        dashboardSidebar(
                          selectizeInput(
                            'email',
                            'Email',
                            c("NYC@gmail.com", "LA@gmail.com", "SF@gmail.com"),
                            multiple = FALSE,
                            options = list(
                              create=TRUE,
                              placeholder = 'Email addresss',
                              onInitialize = I('function() { this.setValue(""); }')
                            )
                          ),
                          uiOutput('email_text')
                        ),
                        dashboardBody())
    
    server <- function(input, output) {
      output$email_text <-
        renderUI({
          # print the input email to the console to help with debugging
          message(input$email)
          if (input$email == "") {
            return(p("Please add your gmail e-mail address."))
          }
    
          #Update: Below checks for "gmail" - I would something to search list and return.
          if (!input$email %in% list_of_emails) {
            return(p("Your email is not in the list of emails!"))
          }
    
        })
    }
    
    runApp(list(ui = ui, server = server))
    
    

    【讨论】:

    • 谢谢。很有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 2017-07-01
    • 2023-03-14
    • 2022-08-20
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    相关资源
    最近更新 更多