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 显示一瞬间,然后在计算仍在进行时消失......我找到了一个肮脏的解决方案来解决这个问题,至少在我的应用程序中。随意尝试一下,也许有人可以提供有关如何以及为什么解决问题的见解。
在您的 server.R 中,您需要添加两个 reactiveValues:
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)
})