Last active
June 30, 2026 11:45
-
-
Save matt-dray/557376b3ab4aef06d8b5c2f172664611 to your computer and use it in GitHub Desktop.
Demo: recording downloads in a Shiny app using {pins} for persistent storage
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
| shiny::shinyApp( | |
| ui = shiny::fluidPage( | |
| shiny::h1("Demo: count downloads"), | |
| shiny::p("Click the download button and the counter will increment."), | |
| shiny::downloadButton("report", "Generate report"), | |
| shiny::textOutput("pin_count"), | |
| ), | |
| server = function(input, output) { | |
| board <- pins::board_connect() # auto-auth if board is on same server as app | |
| pin_name <- "username/pinname" # change to your pin | |
| pin_content <- pins::pin_reactive_read(board, pin_name) | |
| output$pin_count <- shiny::renderText({ | |
| n <- max(pin_content()[["count"]]) | |
| paste("Download count:", n) | |
| }) | |
| output$report <- shiny::downloadHandler( | |
| # Generate file | |
| filename = "report.html", | |
| content = function(file) { | |
| tempReport <- file.path(tempdir(), "report.Rmd") | |
| file.copy("report.Rmd", tempReport, overwrite = TRUE) | |
| rmarkdown::render( | |
| tempReport, | |
| output_file = file, | |
| params = list(df = pin_content()), | |
| envir = new.env(parent = globalenv()) | |
| ) | |
| # Increment count in pin | |
| new_row <- data.frame( | |
| date = as.character(Sys.Date()), | |
| count = max(pin_content()[["count"]]) + 1 | |
| ) | |
| pin_updated <- rbind(pin_content(), new_row) | |
| pins::pin_write(board, pin_updated, pin_name) | |
| } | |
| ) | |
| } | |
| ) |
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: "Demo: download count" | |
| output: html_document | |
| params: | |
| df | |
| --- | |
| This is the content of the pinned data.frame prior to this download: | |
| ```{r, echo=FALSE} | |
| knitr::kable(params$df) | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment