Show that Shiny is busy (or loading) when changing tab panels

后端 未结 3 1901
灰色年华
灰色年华 2020-12-07 16:14

(Code follows after problem description)

I am working on making a web app with Shiny, and some of the R commands that I am executing take minutes to complete. I foun

3条回答
  •  遥遥无期
    2020-12-07 16:38

    Via the Shiny Google group, Joe Cheng pointed me to the shinyIncubator package, where there is a progress bar function that is being implemented (see ?withProgress after installing the shinyIncubator package).

    Maybe this function will be added to the Shiny package in the future, but this works for now.

    Example:

    UI.R

    library(shiny)
    library(shinyIncubator)
    
    shinyUI(pageWithSidebar(
      headerPanel("Testing"),
      sidebarPanel(
        # Action button
        actionButton("aButton", "Let's go!")
      ),
    
      mainPanel(
        progressInit(),
        tabsetPanel(
          tabPanel(title="Tab1", plotOutput("plot1")),
          tabPanel(title="Tab2", plotOutput("plot2")))
      )
    ))
    

    SERVER.R

    library(shiny)
    library(shinyIncubator)
    
    shinyServer(function(input, output, session) {
      output$plot1 <- renderPlot({
        if(input$aButton==0) return(NULL)
    
        withProgress(session, min=1, max=15, expr={
          for(i in 1:15) {
            setProgress(message = 'Calculation in progress',
                        detail = 'This may take a while...',
                        value=i)
            print(i)
            Sys.sleep(0.1)
          }
        })
        temp <- cars + matrix(rnorm(prod(dim(cars))), nrow=nrow(cars), ncol=ncol(cars))
        plot(temp)
      })
    
      output$plot2 <- renderPlot({
        if(input$aButton==0) return(NULL)
    
        withProgress(session, min=1, max=15, expr={
          for(i in 1:15) {
            setProgress(message = 'Calculation in progress',
                        detail = 'This may take a while...',
                        value=i)
            print(i)
            Sys.sleep(0.1)
          }
        })
        temp <- cars + matrix(rnorm(prod(dim(cars))), nrow=nrow(cars), ncol=ncol(cars))
        plot(temp)
      })
    })
    

提交回复
热议问题