2016-09-23 72 views
2

我有一个文件,我生成闪亮 用户单击一个按钮,文件应该下载。然而,没有任何反应闪亮下载文件基于文件路径

函数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) 
    } 
+0

贵'export_report'函数返回一个文件名或文件路径?在'downloadHandler'里面,'filename'应该只是一个名字(它是用户获得的文件的名字),'content'包含了实际的文件。 –

+0

嗨@warmoverflow,如果是这种情况,你如何指定实际文件的下载位置?例如,我的文件包含在名为root \ output \ output_company的文件夹中 - 29-05-82 –

+0

实际的文件路径将在'file.copy'命令(第一个参数)中使用,而文件名将是用作'downloadHandler'中的'filename'变量。 –

回答

3

谢谢大家谁评论和澄清我的想法了一下如何下载处理程序的作品。 最后,我创建了分裂导出功能以上 我使用了新的功能被称为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) 

    } 
) 

这则找到新创建的文件并导出它为用户

1

我只是检查你的问题与此示例代码和它的工作:

output$downloadData <- downloadHandler(
    filename = function() { 
     data <- mtcars 
     myfile <- "data.csv" 
     write.csv(data, myfile) 
     myfile 
    }, 
    content = function(file) { 
     print(file) //prints: [...]\\Local\\Temp\\RtmpEBYDXT\\fileab8c003878.csv 
     file.copy(file, file) 
    } 
) 

MYFILE是下载的文件的文件名。你不能在file.copy作为输入使用它,这个变量超出了范围。看来R创建一个临时文件名(请参阅print())。

+0

Hi @Lars K.谢谢你帮助我。你能告诉我下载处理程序的'filename'部分究竟是什么?它似乎没有被使用到任何地方,路径如何传递给file.copy。我的问题是因为如果两个人同时运行网页,它如何知道要下载哪一个。再次感谢您的帮助 –

+0

嗨,约翰,我没有那么多闪亮。 filename = * myfile *是输出文件名,建议用户下载。但是你也可以用它来存储你的xlsx文件。在我的小例子中,Shiny将data.csv保存在每个会话的通用目录中。你的情况,也许你把你的xlsx存到tempfile =“Table(SessionId).xlsx”并返回myfile =“Table.xslx”。当然,tempfile应该在downloadHandler之外确定,以便在content = function(file){file.copy(tempfile,file)}中找到。希望,我可以帮忙! –

1

尝试使用filename函数来定义您的路径或自定义文件名,以及内容部分中的write.csv。示例代码:

output$downloadData <- downloadHandler(
    filename = function() { 
     paste(<user_name or date for eg>, ".csv", sep="") 
     }, 
    content = function(file) { 
     write.csv(data, file) 
    }  
) 

我注意到在上面的注释中,您询问了应用程序如何在由多个用户使用时生成正确的文件。对于这部分,您需要使用session

所以,如果你的业务逻辑功能均来自名为foo.R的R档,服务器代码应该是这个样子:

shinyServer(func = function(input, output, session) { 
    source("./foo.R", local=TRUE) 
    ...... 

,这将分离出的会话为每个用户,从而产生下载时特定于每个文件的文件。希望这给你一些指点。