我有一个文件,我生成闪亮 用户单击一个按钮,文件应该下载。然而,没有任何反应闪亮下载文件基于文件路径
函数export_report
生成excel文件并将其保存到一个位置。该函数然后将文件位置传递回下载处理程序,以便它下载文件。问题似乎是它不能正确返回。我已经测试了闪亮之外的功能(export_report
),并且它完美地返回了一切,所以我明显从闪亮的角度做了错误的事情。
该文件本身创建的位置应该在服务器上,因为我可以在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() {
部分(是的,length.out =长度(ANS)): “x”为NULL这样的结果将是NULL 警告:错误ifelse:更换具有长度为零 堆栈跟踪(最前): 1:runApp 错误:更换具有长度为零
这个错误基本上是因为我的文件路径不会传递给段myfile
所以
如果有人能告诉我如何让我的函数下面的代码的服务器部分产生的文件路径,应该解决我的问题
content = function(file) {
file.copy(myfile, file)
}
贵'export_report'函数返回一个文件名或文件路径?在'downloadHandler'里面,'filename'应该只是一个名字(它是用户获得的文件的名字),'content'包含了实际的文件。 –
嗨@warmoverflow,如果是这种情况,你如何指定实际文件的下载位置?例如,我的文件包含在名为root \ output \ output_company的文件夹中 - 29-05-82 –
实际的文件路径将在'file.copy'命令(第一个参数)中使用,而文件名将是用作'downloadHandler'中的'filename'变量。 –