Can't get disable button to work with observeEvent with if statement in ShinyR

Viewed 54

What I am trying to do is to use an if statement to prevent 'Analyse' button to perform an action twice, I want to disable the button when the button is clicked on one selection once already.

I have simplified the code to isolate the if statement area. Anyone know what's going on here? Thanks

library(shiny)
library(shinyjs)

ui <- fluidPage(
  navbarPage(title = "Test",             
             tabPanel("Home",
                      sidebarPanel(
                        actionButton("append", "Analyse")
                      ),
                      mainPanel()
             )
  )
)

server <- function(input, output, session) {
  observeEvent(input$append,{
    disbut<-1
    
    if(disbut==1)  
    {
      disable("append")
    }
    else {
      enable("append")
    }
  })
}

shinyApp(ui, server)
2 Answers

You have to initialise shinyjs with useShinyjs()

ui <- fluidPage(
  useShinyjs(),
  navbarPage(title = "Test",             
             tabPanel("Home",
                      sidebarPanel(
                        actionButton("append", "Analyse")
                      ),
                      mainPanel()
             )
  )
)

You have to call useShinyjs() in the ui as shown below.

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  navbarPage(title = "Test",             
             tabPanel("Home",
                      sidebarPanel(
                        actionButton("append", "Analyse")
                      ),
                      mainPanel()
             )
  )
)

server <- function(input, output, session) {
  observeEvent(input$append,{
    disbut<-1
    
    if(disbut==1)  
    {
      shinyjs::disable("append")
    }
    else {
      shinyjs::enable("append")
    }
  })
}

shinyApp(ui, server)
Related