https://github.com/epi-interactive/Dynamic_PDF_Generator
A sample PDF generator directly from Shiny reactive outputs.
https://github.com/epi-interactive/Dynamic_PDF_Generator
Last synced: 13 days ago
JSON representation
A sample PDF generator directly from Shiny reactive outputs.
- Host: GitHub
- URL: https://github.com/epi-interactive/Dynamic_PDF_Generator
- Owner: epi-interactive
- License: mit
- Created: 2020-02-12T23:37:36.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-03T02:33:29.000Z (about 1 year ago)
- Last Synced: 2024-11-02T16:08:17.614Z (5 months ago)
- Language: R
- Size: 111 KB
- Stars: 5
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - epi-interactive/Dynamic_PDF_Generator - A sample PDF generator directly from Shiny reactive outputs. (R)
README
# Dynamic PDF Generator
This app demonstrates how to generate a PDF report directly from Shiny reactive outputs. The parameters of the plotly chart in the PDF file will be the same as the user inputs.
You can try out the app [here](https://rshiny2.epi-interactive.com/apps/dynamic_pdf_generator/)

## How it works
1. Wrap your reactive chart(table...) in a function with user inputs as parameters:``` r
plot_function <- function(n, mean) {
plot_ly(x = ~rnorm(n), type = "box") %>%
add_trace(x = ~rnorm(n, mean)) %>%
layout(xaxis = list(visible=T, showgrid=F, showline=T, mirror=T,
zeroline=F, zerolinecolor="#fff", title = paste0("norm(", n, ")")),
yaxis = list(visible=T, showgrid=F, showline=T, mirror=T,
zeroline=F, zerolinecolor="#fff"),
showlegend = FALSE)
}
)
```2. In report.Rnw, reference the chart in the following way:
``` r
<>=
plot_function(input$plot_example_1_n, input$plot_example_1_mean)
@
```3. Allow content from the Shiny application to be made available to the user as file downloads. Both filename and contents can be calculated dynamically at the time the user initiates the download. Assign the return value to a slot on output in your server function, and in the UI use downloadButton or downloadLink to make the download available:
``` r
# In ui.R:
downloadButton('downloadReport')# In server.R:
output$downloadReport <- downloadHandler(
filename = "report.pdf",
content = function(file) {
src <- normalizePath('report.Rnw')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rnw', overwrite = TRUE)
out = knit2pdf('report.Rnw', clean = TRUE)
file.rename(out, file)
}
)
```
## Sources
- [R Studio Shiny Examples](https://github.com/rstudio/shiny-examples/tree/master/016-knitr-pdf)---
Code created by [Epi-interactive](https://www.epi-interactive.com)
As always, our expert team is here to help if you want custom training, would like to take your dashboards to the next level or just need an urgent fix to keep things running. Just get in touch for a chat.
[https://www.epi-interactive.com/contact](https://www.epi-interactive.com/contact)