I'm building a shiny app in which a query from textInput
is made when the user clicks on the "search" action button. I'd like that button to be disabled if the textInput
box is empty. I'm using shinyjs::toggleState()
here, but I can't figure out what logic it needs to apply to see that the text box is empty. In my reproducible file below, the logic I put in place is is.null(input$query)
. I've also tried with is.na(input$query)
, length(input$query) == 0
, and input$query == ''
, all without success What should I put there instead?
Here's the app.r file:
library(shiny)
library(shinyjs)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
useShinyjs(),
textInput(inputId = "query", label = "Enter query:", value = ""),
actionButton(inputId = "search", label = "Search", icon = icon("search"))
),
mainPanel()
)
)
server <- function(input, output) {
observe({
toggleState("search", !is.null(input$query))
})
}
shinyApp(ui = ui, server = server)
Something like this do?
observe({
if(is.null(input$query) || input$query == ""){
disable("search")
}
else{
enable("search")
}
})
As per @Sagar you can also do:
observe({
toggleState("search", input$query != "" | is.null(input$query))
})
or
observeEvent(input$query,{
toggleState("search", input$query != "" | is.null(input$query))
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With