1

I am making a Shiny App in which, at the click of the actionButton, a model is trained using the caret package. As this training takes time - approximately 4-5 minutes - I wanted to display a progress bar which progresses as the model is trained.

Thanks

Abdul Basit Khan
  • 646
  • 1
  • 6
  • 19

1 Answers1

2

To display progress bar in shiny app, you need to use withProgress function in server as below:

withProgress(message = "Model is Training", value = 1.0, {             
   ## Your code
})

So, you put your code inside this function and it will display the message "Model is training" while your code is running. value in the function is a progress indicator in the app (1.0 is 100%). This you can set depending on computation. For example, you can set value = min + (max - min) * 0.1. It needs not to be exactly like this. Anything that works for you which depend on the code. Setting value = 1.0 wouldn't hurt because it display the progress bar with its meaningful and relevant message in your case "Model is Training".

To get more info, visit this link: https://shiny.rstudio.com/reference/shiny/latest/withProgress.html

Santosh M.
  • 2,356
  • 1
  • 17
  • 29
  • Dear @Santosh, this doesn't seem to work, I am trying to run this code:- observeEvent(input$trainModelButton, { print("Training Begins") withProgress("Model is being Trained", value = 1.0, { train(demand~temp, dataProcessed_Internal, 'rf', ntree = 50) }) }) – Abdul Basit Khan Oct 12 '17 at 12:27
  • Why do you write `print("Training Begins")` in the code. Shiny app does not print with `print` function. You need to use `renderText`. Is there any error, you are getting? Because , showing a progress bar is simple. I have implemented it thousand times. You just need to put the code inside `withProgress` function as I suggested in my answer. – Santosh M. Oct 12 '17 at 12:49
  • `observeEvent(input$trainModelButton, { withProgress("Model is being Trained", value = 1.0, { train(demand~temp, dataProcessed_Internal, 'rf', ntree = 50) }) })` This should work. If there is a problem, it has to be with the `observeEvent` and your train model. – Santosh M. Oct 12 '17 at 12:58
  • Dear @Santosh, I realized what the problem was, instead of writing message = "Model is Training", I was just writing "Model is Training" which was being passed as "expr" argument to the withSpinner() function. – Abdul Basit Khan Oct 13 '17 at 21:54
  • 1
    Brother, with all due respect, my question wasn't just about displaying the Progress Bar, which you did indeed rightly shared. It was to make the Progress Bar progress (move forward) as the model is trained. With your solution, the Progress Bar is completed at once (whereas the model is still being trained for another 5 minutes for example) ... could you kindly comment on that ! – Abdul Basit Khan Oct 13 '17 at 21:59
  • 1
    In my answer, I talked about `value = min + (max - min) * 0.1` which makes the progress bar move forward. However, `value` really depend on your code. There is no global equation for `value`. You can set some value to min and max or you can have a loop inside your training model for `value` that makes progress bar move forward as your training model computes. As I said, this equation is total depends on your code. – Santosh M. Oct 19 '17 at 13:43