Я создал приложение Shiny, в котором пользователь может экспортировать отчет в формате html с помощью downloadHandler.
Это работает. Тем не менее, я хотел бы разрешить пользователю указывать пользовательское имя файла из пользовательского интерфейса, в частности, из поля textInput.
Это не работает. Я получаю просто пустое имя.
Вот воспроизводимый набор кода для приложения и документа rmarkdown. Я не уверен, в чем проблема.
app.R
library(shiny)
shinyApp(
ui = fluidPage(
sliderInput("slider", "Slider", 1, 100, 50),
downloadButton("report", "Generate report"),
## Text box for user defined file name
textInput("report.title", label = h4("Report title"), placeholder = "Enter title")
),
server = function(input, output) {
output$report <- downloadHandler(
# For PDF output, change this to "report.pdf"
## Attempt to set file name to user defined name plus .html
filename = paste(input$report.title, ".html", sep=""),
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
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())
)
}
)
}
)
report.Rmd
---
title: "Dynamic report"
output: html_document
params:
n: NA
---
```{r}
# The `params` object is available in the document.
params$n
```
A plot of `params$n` random points.
```{r}
plot(rnorm(params$n), rnorm(params$n))
```