如何在闪亮的应用程序中显示忙碌指示器? [英] How to display a busy indicator in a shiny app?

查看:20
本文介绍了如何在闪亮的应用程序中显示忙碌指示器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我已经阅读了几乎所有关于此对象的讨论,这些讨论都是在醒目的googleggroup中进行的,在SO中也是如此。

我需要一个指示器来显示闪亮的服务器正忙。我试过闪亮的孵化器,但问题是我不能设置进度条的最大值。 我不想要这样的东西:https://shiny.rstudio.com/gallery/progress-bar-example.html 我需要的是: 1-只要服务器正在计算,就会显示忙碌指示器消息和条(即,只是一个简单的动画条,不需要显示填充条) 2-无论您正在查看哪个选项卡,它都会显示在中。(不仅在相关选项卡中,而且在选项卡集顶部)

推荐答案

更新2018:目前有一个很棒的包可以帮助您展示加载器:shinycssloaders(来源https://github.com/andrewsali/shinycssloaders)

我也一直在找这个。大多数人建议使用这样的条件面板:

conditionalPanel(
            condition="!($('html').hasClass('shiny-busy'))",
            img(src="images/busy.gif")
)

您始终可以在您的ui.R:

中给自己更多控制权并创建条件处理(可能取决于更多内容)
div(class = "busy",
    p("Calculation in progress.."),
    img(src="images/busy.gif")
)

一些JavaScript处理该div的显示和隐藏的位置:

setInterval(function(){
  if ($('html').attr('class')=='shiny-busy') {
    $('div.busy').show()
  } else {
    $('div.busy').hide()
  }
},100)

使用一些额外的CSS,您可以确保您的忙碌动画图像获得始终可见的固定位置。

在上述任何一种情况下,我都发现"闪亮-忙碌"条件有些不精确和不可靠:div显示一秒钟,然后在计算仍在进行时消失…… 我找到了一个肮脏的解决方案来解决这个问题,至少在我的应用程序中是这样。请随时试用,也许有人可以就此如何以及为什么解决问题提供一些见解。

在您的服务器中。R您需要添加两个reactiveValue:

shinyServer(function(input, output, session) {

    # Reactive Value to reset UI, see render functions for more documentation
    uiState <- reactiveValues()
    uiState$readyFlag <- 0
    uiState$readyCheck <- 0

然后,在renderPlot函数(或进行计算的其他输出函数)中,使用以下反应值重置函数:

output$plot<- renderPlot({

    if (is.null(input$file)){
        return()
    }
    if(input$get == 0){
        return()
    }

    uiState$readyFlag

    # DIRTY HACK:
    # Everytime "Get Plot" is clicked we get into this function
    # In order for the ui to be able show the 'busy' indicator we
    # somehow need to abort this function and then of course seamlessly
    # call it again.
    # We do this by using a reactive value keeping track of the ui State:
    # renderPlot is depending on 'readyFlag': if that gets changed somehow
    # the reactive programming model will call renderPlot
    # If readyFlag equals readyCheck we exit the function (= ui reset) but in the
    # meantime we change the readyFlag, so the renderHeatMap function will 
    # immediatly be called again. At the end of the function we make sure 
    # readyCheck gets the same value so we are back to the original state

    isolate({
        if (uiState$readyFlag == uiState$readyCheck) {
            uiState$readyFlag <- uiState$readyFlag+1
            return(NULL)
        }
    })

    isolate({plot <- ...})

    # Here we make sure readyCheck equals readyFlag once again
    uiState$readyCheck <- uiState$readyFlag

    return(plot)
})

这篇关于如何在闪亮的应用程序中显示忙碌指示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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