Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling buttons in Shiny

Tags:

r

shiny

I am writing some Shiny code where the user will enter some inputs to the app and then click a an action button. The action button triggers a bunch of simulations to run that take a long time so I want once the action button is clicked for it to be disabled so that the user can't keep clicking it until the simulations are run. I came across the shinyjs::enable and shinyjs::disable functions but have been having a hard time utilizing them. Here is my server code:

output$button1= renderUI({
        if(input$Button1 > 0)  {
          shinyjs::disable("Button1")
                tableOutput("table")
          shinyjs::enable("Button1")}
  })

However, when I use this code, and click the action button nothing happens. I.e., teh action button doesn't grey out nor does the table get generated. However, when I take away the shinyjs::enable() command, i.e.,

    output$button1= renderUI({
            if(input$Button1 > 0)  {
              shinyjs::disable("Button1")
                    tableOutput("table")
}
      })

The table gets generated first, and then the button goes grey, however I would have expected the button to go grey and then the table to generate itself.

What am I doing wrong here?


Here is my updated code based on Geovany's suggestion yet it still doesn't work for me

Button1Ready <- reactiveValues(ok = FALSE)

        observeEvent(input$Button1,  {
          shinyjs::disable("Button1")
          RunButton1Ready$ok <- FALSE
          RunButton1Ready$ok <- TRUE
  })

output$SumUI1= renderUI({
        if(Button1Ready$ok){
          tableOutput("table")
          shinyjs::enable("Button1")
        }
})

where for clarification I have also:

output$table <- renderTable({

#My code....

)}
like image 755
RustyStatistician Avatar asked Nov 15 '16 23:11

RustyStatistician


1 Answers

I think that you are using shinyjs::disable and shinyjs::enable in the same reactive function. You will only see the last effect. I will recommend you to split in different reactive functions the disable/enable and use an extra reactive variable to control the reactivation of the button.

I don't know how exactly your code is, but in the code below the main idea is illustrated.

library(shiny)
library(shinyjs)


ui <- fluidPage(
  shinyjs::useShinyjs(),
  sidebarLayout(
    sidebarPanel(
      actionButton("Button1", "Run"),
      shinyjs::hidden(p(id = "text1", "Processing..."))
    ),
    mainPanel(
       plotOutput("plot")
    )
  )
)

server <- function(input, output) {

  plotReady <- reactiveValues(ok = FALSE)

  observeEvent(input$Button1, {
    shinyjs::disable("Button1")
    shinyjs::show("text1")
    plotReady$ok <- FALSE
    # do some cool and complex stuff
    Sys.sleep(2)
    plotReady$ok <- TRUE
  })  

  output$plot <-renderPlot({
    if (plotReady$ok) {
      shinyjs::enable("Button1")
      shinyjs::hide("text1")
      hist(rnorm(100, 4, 1),breaks = 50)
    }
  })
}

shinyApp(ui, server)
like image 132
Geovany Avatar answered Oct 11 '22 08:10

Geovany