Created
February 10, 2025 16:18
-
-
Save mnazarov/c26894367739bb2b7ef822fc0e7c8798 to your computer and use it in GitHub Desktop.
shiny progress bar linked to rmarkdown/knitr progress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) | |
}) | |
} | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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