Home > Software design >  How to pass user input to filename in R shiny downloadhandler
How to pass user input to filename in R shiny downloadhandler

Time:01-29

This is a follow up question to this How to pass a reactive plot generated in Shiny to Rmarkdown to generate dynamic reports

I am trying to pass the user input in textInput to the filename argument of downloadhandler.

This concept usually works, but in this case this code does not work:

In essence I want to change

filename = "report.html",

TO

filename = paste0("my_new_name", input$text,"-" ,Sys.Date(), ".html"),

Here is the code:

library(shiny)
library(radarchart)

shinyApp(
  ui = fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50),
    textInput("text", "text"),
    downloadButton("report", "Generate report")
  ),
  server <- function(input, output) {
    output$plot1 <- renderChartJSRadar({
      chartJSRadar(skills[, c("Label", input$selectedPeople)], 
                   maxScale = 10, showToolTipLabel=TRUE) 
    })
    
    output$report <- downloadHandler(
      #filename = "report.html",
      filename = paste0("my_new_name", input$text,"-" ,Sys.Date(), ".html"),
      content = function(file) {
        tempReport <- file.path(tempdir(), "report.Rmd")
        file.copy("report.Rmd", tempReport, overwrite = TRUE)
        
        params <- list(scores = skills[, c("Label", input$selectedPeople)])
        
        rmarkdown::render(tempReport, output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

Report.Rmd

---
title: "Dynamic report"
output: html_document
params:
  scores: NA
---

```{r}
chartJSRadar(params$scores, maxScale = 10, showToolTipLabel=TRUE)
```

CodePudding user response:

When the desired file name relies on a reactive value, you have to set it with a function without argument:

filename = function() {
  _what_you_want_
}
  • Related