我创建了一个Shiny应用程序,用户可以使用downloadHandler导出html报告。
这有效。但是,我希望允许用户从UI(特别是从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))
```