Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable action button when textinput is empty in Shiny app [R]

Tags:

r

shiny

shinyjs

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)
like image 396
Phil Avatar asked Oct 23 '17 13:10

Phil


1 Answers

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))
})
like image 102
Pork Chop Avatar answered Nov 16 '22 03:11

Pork Chop