I have created a reprex example.
R markdown report.rmd file:
---
title: "report"
date: "5/9/2022"
output: html_document
params:
n: NA
---
```{r pressure, echo=FALSE}
plot(rnorm(params$n))```
App code:
shinyApp(
ui = fluidPage(
sliderInput("slider", "Slider", 1, 100, 50),
pickerInput("selectreport", "Select Report",
c("Report1","Report2"),multiple = F),
downloadButton("report", "Generate report")
),
server = function(input, output) {
ending <- reactive({
if (input$selectreport =="Report1") ending<-".html"
else{ending <-".pdf"}
})
parameters <- reactive({
if (input$selectreport =="Report1") {
list(n = input$file1$datapath,
obs=input$obs)}else{
list(n = input$file1$datapath,
course=input$inSelect)}
})
filename <- reactive({
filename<-paste0(input$selectreport , ending())
})
observe({
print(filename())
})
output$report <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = filename(),
content = function(file) {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(n = input$slider)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}
)