Displaying dbplyr collect progress in shiny

Viewed 141

I'm writing a dashboard with shiny. To load data I'm using an equivalent of

    mydata <- reactive({
      in.positions <- isolate(input$positions)
      (db_conn
        %>% tbl('table1')
        %>% filter(position %in% in.positions)
        %>% inner_join(db_conn %>% tbl('table2'), by = 'id')
        %>% collect()
      )
   })

Here db_conn is a dbPool object. The problem is that sometimes there'd be a lot of data and it would take some time to load it. Is there any way I could monitor progress of collect(), ideally mapped to a shiny progress bar?

1 Answers

I've drafted an example dashboard using shinycssloaders. The gist is to pipe your UI elements into a withSpinner function. Hope this helps.

library(shiny)
library(shinycssloaders)
library(ggplot2)
library(magrittr)

ui <- fluidPage(  
  sidebarPanel(
    selectInput('n_datapoints', label = 'how many?',
                choices=c(1000, 10000, 1000000))
  ),

  mainPanel(
    plotOutput('plot') %>%
      # Adds a dark red spinner while waiting for the plot
      withSpinner(color='#8B0000')
  )
)

server <- function(input, output, session) {

  # Some long-running data retrieval
  data <- reactive({
    rnorm(input$n_datapoints)
  })

  # Get the plot
  output$plot <- renderPlot(
    data() %>% 
      plot()
  )
}

shinyApp(ui, server)

enter image description here

Related