【问题标题】:Validating the data entered in numeric input in RShiny验证在 RShiny 中的数字输入中输入的数据
【发布时间】:2017-10-12 17:01:04
【问题描述】:

我正在开发 Shiny 应用程序,如果用户输入非数字字符,则该值应更改为 updateNumericInput() 中提到的值。

这是我的Rcode

library(shiny)
ui <- fluidPage (
  numericInput("current", "Current Week",value = 40, min = 40, max = 80)
)

server <- function(input, output, session) {
  observeEvent(input$current, { 
    updateNumericInput(session,"current", value = ({ if(!(is.numeric(input$current))){40}
                           if(!(is.null(input$current) || is.na(input$current))){
                           if(input$current < 40){
                             40 
                           }else if(input$current > 80){
                             80
                           } else{
                             return (isolate(input$current))
                           } 
                         } 
      })
    )
  })

  }

shinyApp(ui=ui, server = server)

谁能帮我处理这段代码?

【问题讨论】:

    标签: r validation shiny


    【解决方案1】:

    这样的?

    library(shiny)
    ui <- fluidPage (
      numericInput("current", "Current Week",value = 40, min = 40, max = 80)
    )
    
    server <- function(input, output, session) {
      observeEvent(input$current, { 
        updateNumericInput(session,"current", value = ({ if(!(is.numeric(input$current))){40}
          else if(!(is.null(input$current) || is.na(input$current))){
            if(input$current < 40){
              40 
            }else if(input$current > 80){
              80
            } else{
              return (isolate(input$current))
            } 
          } 
          else{40}
        })
        )
      })
    
    }
    
    shinyApp(ui=ui, server = server)
    

    【讨论】:

    • 与此类似。如果我在现有代码中的 if else 中输入 !(is.numeric(input$current)) ,它将无法正常工作。有任何可用的解决方案吗? @猪排
    • 必须先评估
    • 使用我的解决方案
    • 使用您的解决方案,我无法检查 numericInput 中是否没有输入值。能不能详细点!!
    • 我已根据您的解决方案更新了我的代码。但我仍然没有得到预期的输出。更新的代码在上面。你能帮我吗? @猪排
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多