Home > database >  How to disable a button based on input condition?
How to disable a button based on input condition?

Time:11-09

Im trying to create a condition on a push button so it only works when my input is not empty AND is numeric . My code partially works as the button starts in a disabled gets enables when an input is typed but even if the input is not numeric (ie. typing letters will enable the button)

UI SIDE

library(shinyjs)

    ui <-    tabItem(tabName = "principal1",
                        br(),
                        
                        fluidRow( 
                          column(2,
                                 textInput(inputId = "documento1", "Ingrese el numero de documento", "")
                                 ),
                          
                          column(2,
                                 br(),
                                 fluidRow(
                                   actionButton("consulta_gobutton1", 
                                                label =  "Procesar",
                                                icon = icon("clipboard-check") ) )))

SERVER SIDE

 observeEvent(input$consulta_gobutton1, {
    documento1 <- input$documento1

    ###HERE IS MY CODE###

    })

 observe({
        toggleState("consulta_gobutton1", input$documento1 != ""  & is.numeric(as.numeric(input$documento1)) )
        
       })

CodePudding user response:

since the input is a string, it first needs dealt with in a different way

  observe({
    toggleState("consulta_gobutton1", input$documento1 != ""  & !is.na(as.numeric(input$documento1)) )

    #if(is.numeric(as.numeric(nn)) & !is.na(as.numeric(nn))

  })

CodePudding user response:

You can use numericInput and do this

library(shinyjs)

ui <-    fluidPage(tabItem(tabName = "principal1",
                 br(),
                 useShinyjs(),
                 fluidRow( 
                   column(2,
                          numericInput(inputId="doc1",label="Ingrese el numero de documento ",value="",min=0,max=15,step=0.1, width = "80px"),
                          #textInput(inputId = "documento1", "Ingrese el numero de documento", "")
                   ),
                   
                   column(2,
                          br(),
                          fluidRow(
                            actionButton("consulta_gobutton1", 
                                         label =  "Procesar",
                                         icon = icon("clipboard-check") ) ))
                   )
))

server <- function(input, output,session) {
  observeEvent(input$consulta_gobutton1, {
    documento1 <- input$documento1
    
    ###HERE IS MY CODE###
    
  })
  
  observe({
    
    if (is.numeric(input$doc1)) shinyjs::enable("consulta_gobutton1")
    else shinyjs::disable("consulta_gobutton1")
    
    #toggleState("consulta_gobutton1", input$documento1 != ""  & is.numeric(as.numeric(input$documento1)) )
    
  })
}

shinyApp(ui = ui, server = server)

output

  • Related