简体   繁体   中英

How to pass user input to filename in R shiny downloadhandler

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)
```

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_
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM