How to disable an actionButton until a computation is finished ?

3,871 views
Skip to first unread message

Olivier Savriama

unread,
Aug 27, 2014, 1:16:11 AM8/27/14
to shiny-...@googlegroups.com

Hey guys, is there a way to disable a "goButton" in charge of starting computationally expensive operations until the operations are done and then re-enable it ? 1/ I've tried the shinyBS package with the bsActionButton() and updateButton() but no way it doesn't work. 2/ Implementing javascript is a bit too hard for me at the moment. Thanks.

David Wahman

unread,
Aug 27, 2014, 12:16:45 PM8/27/14
to shiny-...@googlegroups.com
Here is one option that may work.  This following code will make the action button "disappear" until the compution is complete.  Because of the conditional panel, it only displays the button when shiny is "not busy".
 
conditionalPanel(condition = "!$('html').hasClass('shiny-busy')",   
         actionButton("ButtonId", label = "my button ID text"))

Olivier Savriama

unread,
Aug 28, 2014, 1:01:53 AM8/28/14
to shiny-...@googlegroups.com
Hi David, thanks but it doesn't work.
By the way is it possible to create a reactive variable in "server.R" which indicates that a computation is finished and then use it in the condition field of your conditionalPanel ?
Thanks.

Olivier Savriama

unread,
Sep 16, 2014, 2:11:06 AM9/16/14
to shiny-...@googlegroups.com
Hi,


Here is my code:

# ui.R

library(shiny)

shinyUI(fluidPage(
  singleton(tags$head(HTML(
    '
    <script type="text/javascript">
    $(document).ready(function() {
    
    // disable start_proc button after a click
    Shiny.addCustomMessageHandler("disableButton", function(message) {
    $("#start_proc").attr("disabled", "true");
    });
    
    // Enable start_proc button when computation is finished
    Shiny.addCustomMessageHandler("enableButton", function(message) {
    $("#start_proc").removeAttr("disabled");
    });
})
    </script>
    '
))),
  tabsetPanel(
    tabPanel('Enable/Disable Start Button',
      actionButton("start_proc", h5("Start Button")),
      hr(),

      helpText("Start Button will be available once the computation (5 seconds) is completed.")
    )
  )
))


# server.R

library(shiny)

fakeDataProcessing <- function(duration) {
  # does nothing but sleep for "duration" seconds while
  # pretending some background task is going on...
  Sys.sleep(duration)
}

shinyServer(function(input, output, session) {
  
  observe({
    if (input$start_proc > 0) {
      # Notify the browser that the computation is running
      session$sendCustomMessage("disableButton", "start_proc")
      print("Computation is running.")
      fakeDataProcessing(5)
      # Notify the browser that the computation is finished
      session$sendCustomMessage("enableButton", "start_proc")
      print("Computation is done.")
    }
  })
})

Can anybody tell me how to print my messages "Computation is running." and "Computation is done." in a textOutput in real time ?

Joe Cheng

unread,
Sep 16, 2014, 12:26:59 PM9/16/14
to Olivier Savriama, shiny-...@googlegroups.com
Shiny 10.2 (which is coming out in the next couple of weeks) has a progress API that can be used for "Computation is running". You can install it from GitHub by using

if (!require("devtools"))
  install.packages("devtools")
devtools::install_github("rstudio/shiny")

then do ?shiny::withProgress to see the help topic.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/2d2c1e0c-4e8a-4d9d-b8bd-1fe7d4ba1fbf%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Olivier Savriama

unread,
Sep 17, 2014, 12:59:13 AM9/17/14
to shiny-...@googlegroups.com, olivier....@gmail.com
Hi Joe,

Thanks, it's very useful.

Winston Chang

unread,
Sep 17, 2014, 1:51:26 PM9/17/14
to Olivier Savriama, shiny-discuss
Also see this for more information on progress indicators:
http://rpubs.com/wch/28353

On Tue, Sep 16, 2014 at 11:59 PM, Olivier Savriama
> https://groups.google.com/d/msgid/shiny-discuss/3a8190da-dc37-4fd2-a634-2974714ebff7%40googlegroups.com.

Olivier Savriama

unread,
Sep 18, 2014, 2:04:36 AM9/18/14
to Winston Chang, shiny-discuss
Hi Chang,

That's great !

Thank you,
Olivier.

k.kos...@gmail.com

unread,
Jan 26, 2019, 6:53:00 AM1/26/19
to Shiny - Web Framework for R
Hi, can you show an example of how this would be used?
Reply all
Reply to author
Forward
0 new messages