【问题标题】:How to validate the data entered in dynamically created textBoxes in Rshiny如何验证在 Rshiny 中动态创建的文本框中输入的数据
【发布时间】:2017-10-25 07:02:45
【问题描述】:

我目前正在开发一个闪亮的应用程序。我有一个问题。如何验证在动态创建的文本框中输入的数据。 (即)只允许输入数字。不允许输入大于 100 小于 0 的特殊字符或数字。

使用的RCode如下:

    require(shiny)

ui = fluidPage(
  fluidRow(
    column(3,numericInput("count", "No. of boxes",value = 3, min = 2, max = 10),actionButton("View","view")
    )
  ),
  fluidRow(uiOutput("inputGroup")),
  fluidRow(column(3,wellPanel(textOutput("text3"))))
)

# takes in two arguments
sumN <- function(a, x){
  if(!(is.numeric(x))){ x = 0}
  if(x > 100){
    x = 0
  }
  a <- sum(a, as.numeric(x),na.rm=T)
  return(a)
}

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

  Widgets <- eventReactive(input$View,{
    input_list <- lapply(1:(input$count), function(i) {
      inputName <- paste("id", i, sep = "")
      textInputRow<-function (inputId,value) {
        textAreaInput(inputName,"", width = "200px", height = "43px", resize = "horizontal")
      }
      column(4,textInputRow(inputName, ""))
    })
    do.call(tagList, input_list)},ignoreInit = T)

  output$inputGroup = renderUI({Widgets()})

  getvalues <- reactive({
    val <- 0
    for(lim in 1:input$count){
      val <- sumN(val,as.numeric(input[[paste0("id",lim)]]))
    }
    val
  })

  output$text3 <- renderText({getvalues()})
}

shinyApp(ui=ui, server = server)

上面的代码产生错误。谁能帮我处理这段代码?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    如果验证失败,我不确定您是否需要打印错误消息,但在您的代码中使用 req 您可以进行验证。请检查以下代码:

    require(shiny)
    
    ui = fluidPage(
      fluidRow(
        column(3,numericInput("count", "No. of boxes",value = 3, min = 2, max = 10),actionButton("View","view")
        )
      ),
      fluidRow(uiOutput("inputGroup")),
      fluidRow(column(3,wellPanel(textOutput("text3"))))
    )
    
    # takes in two arguments
    sumN <- function(a, x){
      if(!(is.numeric(x))){ x = 0}
      if(x > 100){
        x = 0
      }
      a <- sum(a, as.numeric(x),na.rm=T)
      return(a)
    }
    
    server <- function(input, output, session) {
    
      Widgets <- eventReactive(input$View,{
        input_list <- lapply(1:(input$count), function(i) {
          inputName <- paste("id", i, sep = "")
          textInputRow<-function (inputId,value) {
            #textAreaInput(inputName,"", width = "200px", height = "43px", resize = "horizontal")
             numericInput(inputName,"",1,0,100)
          }
          column(4,textInputRow(inputName, ""))
        })
        do.call(tagList, input_list)},ignoreInit = T)
    
      output$inputGroup = renderUI({Widgets()})
    
    
    
      getvalues <- reactive({
        val <- 0
        for(lim in 1:input$count){
          #validate(need(is.numeric(input[[paste0("id",lim)]]),"Please enter a number"))
          req(is.numeric(input[[paste0("id",lim)]]) & input[[paste0("id",lim)]] > 0 & input[[paste0("id",lim)]] <100)
          val <- sumN(val,as.numeric(input[[paste0("id",lim)]]))
        }
        val
      })
    
      output$text3 <- renderText({getvalues()})
    }
    
    shinyApp(ui=ui, server = server)
    

    编辑req 表达式:

      req(as.numeric(input[[paste0("id",lim)]]) > 0 & as.numeric(input[[paste0("id",lim)]]) <100)
    

    输出截图:

    更新了将 char 替换为 0 的答案:

    require(shiny)
    library(shinyjs)
    ui = fluidPage( useShinyjs(), 
      fluidRow(
        column(3,numericInput("count", "No. of boxes",value = 3, min = 2, max = 10),actionButton("View","view")
        )
      ),
      fluidRow(uiOutput("inputGroup")),
      fluidRow(column(3,wellPanel(textOutput("text3"))))
    )
    
    # takes in two arguments
    sumN <- function(a, x){
      if(!(is.numeric(x))){ x = 0}
      if(x > 100){
        x = 0
      }
      a <- sum(a, as.numeric(x),na.rm=T)
      return(a)
    }
    
    server <- function(input, output, session) {
    
    
      reactiveValues( initial_val = 0)
    
      Widgets <- eventReactive(input$View,{
        input_list <- lapply(1:(input$count), function(i) {
          inputName <- paste("id", i, sep = "")
          textInputRow<-function (inputId,value) {
            #textAreaInput(inputName,"", width = "200px", value = changed_input(), height = "43px", resize = "horizontal")
            numericInput(inputName,"",0,0,100)
          }
          column(4,textInputRow(inputName, ""))
        })
        do.call(tagList, input_list)},ignoreInit = T)
    
      output$inputGroup = renderUI({Widgets()})
    
    
    
      getvalues <- reactive({
        val <- 0
        for(lim in 1:input$count){
          #validate(need(is.numeric(input[[paste0("id",lim)]]),"Please enter a number"))
          #ifelse()
          #ifelse(grepl('^[0-9]*$',input[[paste0("id",lim)]]),cat('cool'),reset(paste0("id",lim)))
          if(!grepl('^[0-9]*$',input[[paste0("id",lim)]])){
            reset(paste0("id",lim))
          }
          req(is.numeric(input[[paste0("id",lim)]]) & input[[paste0("id",lim)]] > 0 & input[[paste0("id",lim)]] <100)
          val <- sumN(val,as.numeric(input[[paste0("id",lim)]]))
        }
        val
      })
    
      output$text3 <- renderText({getvalues()})
    }
    
    shinyApp(ui=ui, server = server)
    

    【讨论】:

    • textAreaInput 是否可以进行验证?
    • 您想保留 textAreaInput 而不是 numericInput?
    • 是的。同样使用上面的代码,我也可以在数字输入中输入字符。
    • 抱歉,有点不清楚。您还想在文本框中输入字符吗?那么,如果需要此功能,为什么我们必须仅针对数字验证它
    • 如果使用textAreaInput,可以使用编辑后的req
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多