Nevedha Ayyanar
Nevedha Ayyanar

Reputation: 865

Validating the data entered in numeric input in RShiny

I am developing the Shiny app in which if the user enters non-numeric characters, the value should be changed to the value mentioned in updateNumericInput().

Here is my 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)

Can anyone help me with this code?

Upvotes: 1

Views: 1491

Answers (1)

Pork Chop
Pork Chop

Reputation: 29407

Something like this?

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)

Upvotes: 1

Related Questions