如何验证在Rshiny中动态创建的textBoxes中输入的数据

时间:2017-10-25 07:02:46

标签: r shiny

我目前正在开发一款Shiny App。我有一个问题。如何验证在动态创建的textBox中输入的数据。 (即)只允许数字输入。不允许输入大于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)

上面的代码会产生错误。任何人都可以帮我这个代码吗?

1 个答案:

答案 0 :(得分:0)

如果验证失败但您在代码中使用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)

输出截图:

enter image description here

更新了将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)