只有激活actionButton当闪亮的应用程序不旺 [英] Only activate actionButton when Shiny app is not busy

查看:155
本文介绍了只有激活actionButton当闪亮的应用程序不旺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 actionButton()在我的网页,使一些计算,20或30秒后它完成。

I have an actionButton() in my webpage that makes some computation, and it finishes 20 or 30 seconds later.

有什么办法应用程序运行时禁用按钮?或任何其他的方法可以是有趣了。我问这个,因为我的应用程序的一些用户双击该 actionButton ,我可以在计算被运行两次服务器看到的。

Is there any way to disable the button while the app is running? Or any other approach could be interesting too. I am asking this because some users of my app double-clicks the actionButton and I can see in the server that the calculations are run twice.

感谢您!

推荐答案

被点击后,您可以禁用按钮,退出再次启用它。它可以手工完成,但 shinyjs 已经提供了所需的帮手。

You can disable button after it is clicked and enable it again on exit. It can be done manually but shinyjs already provides required helpers.

如果函数调用上的点击可能会失败,你可以使用 tryCatch 最后,以确保您的应用程序获得了 ŧ留在禁用状态:

If function called on click may fail you can use tryCatch with finally to make sure that your app won't stay in the disabled state:

library(shiny)
library(shinyjs)

foo <- function() {
    Sys.sleep(4)
    x <- runif(1)
    if(x < 0.5) stop("Fatal error")
    print(x)
}

shinyApp(
    ui=shinyUI(bootstrapPage(
        useShinyjs(),
        actionButton("go", "GO")
    )),
    server=shinyServer(function(input, output, session){
        observe({
            if(input$go == 0) return()
            shinyjs::disable("go")

            tryCatch(
                foo(),          
                error = function(e) return(),
                finally = shinyjs::enable("go")
            )
        })
    })
)

这篇关于只有激活actionButton当闪亮的应用程序不旺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆