【发布时间】:2018-12-04 15:24:05
【问题描述】:
我有一个闪亮的应用程序,一旦单击按钮就会打印报告。通过 downloadHandler() 函数创建报告。
我希望在导出报告之前有一个强制输入字段;合适的 Shiny 函数是 validate() (https://shiny.rstudio.com/articles/validation.html)。
但是,根据文档,validate() 函数只能用于 reactive() 或 render() 函数:
To use this validation test in your app, place it at the start of any reactive or render* expression that calls input$data.
我没有找到可以将此函数放入我的 downloadHandler 函数的地方。有人知道这怎么可能吗?
这里是相关的代码部分;我希望创建报告时必须填写“公司名称”字段。
ui <- fluidPage(
sidebarLayout(
position = "left",
sidebarPanel(
textInput(
inputId = "company_name",
label = "Company name",
value = ""
),
)
)
)
server <- function(input, output) {
output$report <- downloadHandler(
filename = "report.pdf",
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)
dir.create(file.path(tempdir(),"www"))
file.copy("www", file.path(tempdir()), recursive=TRUE)
# Set up parameters to pass to Rmd document
params <- list(company_name = input$company_name)
# 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())
)
}
)
}
【问题讨论】:
标签: shiny