1
votes

I have a R Shinyweb page that takes in user inputs, queries a Postgres database per user inputs and then plots the information. I am trying to add a status bar to the Shiny application.

However, my problem is that I don't know how long it takes for the query to execute. The query execution time is dependent on user inputs.

Is there any way I can loop the status bar continuously till the query is completed? If so, how can I track the completion of the query?

Below is a example code. The line in 'plot(rnorm(as.numeric(input$n_breaks)))' is the actual SQL query in my code. The time for execution of this line is unknown. I am not looking for an actual code. I just need ideas on how this concept may be implemented.

library(shiny)
mycss <- "
#plot-container {
position: relative;
}
#loading-spinner {
position: absolute;
left: 50%;
top: 50%;
z-index: -1;
margin-top: -33px;  /* half of the spinner's height */
margin-left: -33px; /* half of the spinner's width */
}
#plot.recalculating {
z-index: -2;
}
"

ui <- fluidPage(
tags$head(tags$style(HTML(mycss))),
actionButton("btn", "Plot (takes 2 seconds)"),
selectInput(inputId = "n_breaks",
          label = "Select number of points",
          choices = c(100, 1000, 10000, 100000),
          selected = 100),
div(id = "plot-container",
  tags$img(src = "spinner.gif",
           id = "loading-spinner"),
  plotOutput("plot")
 )
)

server <- function(input, output, session) {
output$plot <- renderPlot({
input$btn
i = 0
while (i==0) {
  Sys.sleep(1)
  plot(rnorm(as.numeric(input$n_breaks)))
  i = 1
  }
})
}

shinyApp(ui, server)

1

1 Answers

0
votes

I found the answer through the following link:

https://shiny.rstudio.com/articles/progress.html

Essentially, there is no way this can be done if the progress function calls out to another function, to do the long-running computation. The best we can do is to set it to a starting value of, say, 0.3, and then move it to 1 at completion. This may also be true if, for example, the function is in an external package.