0

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)) )
        
       })

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

i cant get it to work this way. button will remain disabled even with numeric input
It works fine for me. I will put image. Also, you can change the max to above 15, to whatever valid range you require. Perhaps you need to reboot your laptop or restart RStudio.
thanks. i didnt have a numeric input field so that was affecting the button state
0

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))

  })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.