Skip to content

Instantly share code, notes, and snippets.

@mnazarov
Created February 10, 2025 16:18
Show Gist options
  • Save mnazarov/c26894367739bb2b7ef822fc0e7c8798 to your computer and use it in GitHub Desktop.
Save mnazarov/c26894367739bb2b7ef822fc0e7c8798 to your computer and use it in GitHub Desktop.
shiny progress bar linked to rmarkdown/knitr progress
library(shiny)
# custom progress function
myProgress <- function(total, labels) {
list(update = function(i) {
shiny::setProgress(value = i/total, message = paste0("Running chunk ", i, "/", total, ": "), detail = labels[i])
cat(sep = "", i, "/", total, ": ", labels[i], "\n")
}, done = function() {
shiny::setProgress(1, message = "Done")
})
}
# register it with knitr
op <- options(knitr.progress.fun = myProgress)
# shiny app
shinyApp(
ui = fluidPage(
titlePanel("Rmarkdown + Shiny Progress"),
downloadButton("run", "Create Report")
),
server = function(input, output, session) {
output$run <- downloadHandler(filename = "report.html",
content = function(file) {
withProgress(message = "Running report...", {
rmarkdown::render("report.Rmd", output_file = file)
})
})
}
)
---
title: "Rmarkdown + Shiny Progress Example"
output: html_document
---
# Header
```{r plot}
Sys.sleep(1)
plot(rnorm(100), pch = 20)
```
Some text
```{r chunk-with-table}
Sys.sleep(1)
knitr::kable(head(mtcars))
```
some more text
```{r unknown}
Sys.sleep(1)
plot(rnorm(10), pch = 20, col = sample(1:16, 10, FALSE))
```
# Conclusion
```{r final}
Sys.sleep(1)
1+1
```
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment