2

I want to show a progress bar if something in eventReactive() is evaluated. However, withProgress() doesn't seem to work as expected if nothing is added directly to the output. (See the first part below that gets triggered by input$go1.)

In a dashboard with multiple panels it shows up if one navigates to another subpage. However, I want to have it showing up immediately after hitting the button.

Can anybody help to fix this problem in the example below?

library(shiny)

ui <- fluidPage(
            actionButton("go1", "Go! Number 1"),
            actionButton("go2", "Go! Number 2"),
            plotOutput("plot")
            )



server <- function(input, output) {

    eventReactive(input$go1,{
        withProgress({
            for (i in 1:15) {
              incProgress(1/15)
              Sys.sleep(0.25)
              }
            }, message = "Doesn't show up!")
    })

output$plot <- renderPlot({
        input$go2
        withProgress({
            for (i in 1:15) {
                incProgress(1/15)
                Sys.sleep(0.1)
            }
        }, message = "Shows up!")
        plot(cars)
    })

}


shinyApp(ui = ui, server = server)
Alex
  • 4,925
  • 2
  • 32
  • 48
  • TBH, not entirely clear what you're asking. Perhaps, you missed to add `input$go1` starting from a new line after `input$go2` in `output$plot`?: ```output$plot <- renderPlot({ input$go2 input$go1 withProgress({ for (i in 1:15) { incProgress(1/15) Sys.sleep(0.1) } }, message = "Shows up!") plot(cars) }) ``` – Vitali Avagyan Sep 22 '19 at 16:23
  • @VitaliAvagyan I want the progress bar in `eventReactive()` to work. The second one is just there to show that it work when an output is produced. – Alex Sep 22 '19 at 18:35

1 Answers1

2

eventReactive isn't executed when the event expression is triggered, it is only flagged as invalidated. You'll have to request their reactive value to have them executed - please check the following:

library(shiny)

ui <- fluidPage(
  actionButton("go1", "Go! Number 1"),
  actionButton("go2", "Go! Number 2"),
  plotOutput("plot")
)

server <- function(input, output) {

  myData <- eventReactive(input$go1, {
    withProgress({
      for (i in 1:15) {
        incProgress(1/15)
        Sys.sleep(0.25)
      }
    }, message = "Doesn't show up!")
    cars
  })

  output$plot <- renderPlot({
    input$go2
    req(myData())
    withProgress({
      for (i in 1:15) {
        incProgress(1/15)
        Sys.sleep(0.1)
      }
    }, message = "Shows up!")
    plot(myData())
  })

}

shinyApp(ui = ui, server = server)

For your example scenario observeEvent is the better choice. Please see this.

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • Sorry for letting you wait. Actually my code should look as yours. I did a mistake in my reproducible example by forgetting the assignment. Since this seems to work perfectly my actual problem is probably related to `shinydashboard`. But thanks nontheless. – Alex Sep 26 '19 at 15:16
  • Feel free to adapt your example. Maybe we'll find a solution for your real problem. – ismirsehregal Sep 26 '19 at 16:10