我有一个文件,我在 Shiny 的
用户单击一个按钮,文件应下载。然而什么也没有发生
函数export_report
生成 excel 文件并将其保存到某个位置。然后该函数将文件位置传回下载处理程序,以便下载文件。问题似乎是它没有正确返回。我已经在 Shiny 的外部测试了函数( export_report
),它完美地返回了所有内容,所以从 Shiny 的角度来看,我显然做错了。
文件本身是在它应该在服务器上的位置创建的,因为我可以在 RStudio 中下载它并在文件资源管理器中查看它。谁能帮忙
# UI Section
downloadButton("downloadRpt", "Download Report")
# Server Section
output$downloadRpt <- downloadHandler(
filename = function() {
mydf <- report()
dateRange <- input$dates_report
selection <- input$selection
myfile <- export_report (mydf, selection, dateRange)
},
content = function(file) {
file.copy(myfile, file)
}
)
我看过其他例子 R Shiny: Download existing file这是我的代码所基于的
编辑 1:添加带有一些虚假数据的 export_report 函数来运行它
export_report <- function(mydf,selection,dateRange) {
# Template for where the template excel file is stored
myoutputTemplate <- '/home/shiny_tutorials/Save to Database/templates/output_template.xlsx'
start_date <- dateRange[1]
end_date <- dateRange[2]
date_range <- paste(start_date ,end_date, sep = " - " )
# Load workbook the template workbook
wb <- loadWorkbook(myoutputTemplate)
# write to the workbook the data frame
writeWorksheet(wb, mydf, sheet="Details",
startRow=8, startCol=2,
header=FALSE)
# add the the customer the user selected
writeWorksheet(wb, selection, sheet="Details",
startRow=3, startCol=3,
header=FALSE)
# date
writeWorksheet(wb, date_range, sheet="Details",
startRow=5, startCol=3,
header=FALSE)
# Create The file Name
filename <- paste(selection, Sys.Date(), sep = " - ") %>%
paste(.,"xlsx", sep = ".")
# removes the % sign and extra qoutes
filename <- gsub (pattern = '\'|%','', x = filename)
# output directory
myoutput <- paste('/home/shiny_tutorials/Save to Database/output/',
filename, sep = '')
# Save workbook
saveWorkbook(wb, myoutput)
# Return File Path
myoutput
}
要调用该函数,您可以使用以下数据
dateRange <- c("2011-09-23","2016-09-23")
selection = "COMPANY_A"
mydf <- iris
myfile <- export_report(mydf,selection,dateRange)
编辑 2 我现在已经设法解决了一个错误。当我
cat(myfile)
在 filename = function() {
返回正确的文件路径后我收到错误的代码部分Warning in rep(yes, length.out = length(ans)) : 'x' is NULL so the result will be NULL Warning: Error in ifelse: replacement has length zero Stack trace (innermost first): 1: runApp Error : replacement has length zero
这个错误基本上是因为我的文件路径没有传递到段
myfile
所以如果有人可以告诉我如何将我的函数生成的文件路径获取到下面代码的服务器部分,那应该可以解决我的问题
content = function(file) {
file.copy(myfile, file)
}
最佳答案
感谢所有评论并澄清我对下载处理程序如何工作的想法的人。
最后,我创建了一个新函数,它拆分了上面的导出函数
我使用的新函数叫做 generate_file()
它只是返回文件名
generate_file_name <- function(selection) {
# Create The file Name
filename <- paste(selection, Sys.Date(), sep = " - ") %>%
paste(.,"xlsx", sep = ".")
# removes the % sign and extra qoutes
filename <- gsub (pattern = '\'|%','', x = filename)
# output directory
myoutput <- paste('/home/shiny_tutorials/Save to Database/output/',
filename, sep = '')
# Return File Path
myoutput
}
然后在服务器端
output$downloadRpt <- downloadHandler(
filename = function() {
selection <- input$company
generate_file_name(selection)
},
content = function(file) {
mydf <- report()
dateRange <- input$dates_report
selection <- input$company
export_report(mydf,selection,dateRange)
myfile <- generate_file_name(selection)
file.copy(myfile, file)
}
)
然后找到新创建的文件并将其导出给用户
关于r - 基于文件路径的 Shiny 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39663028/