Shiny - 通过 DownloadHandler 将工作簿下载为 pdf

问题描述

我正在努力创建一个 Shiny 应用程序,该应用程序将允许用户选择首选文件格式并下载数据。代码将在 FinalEP & FinalLR 中读取,然后运行我创建的函数,该函数将数据过滤到用户输入的帐户,然后将数据添加到 Excel 工作簿报告模板。 excel 工作簿的前几行有公司徽标和文本信息,因此我无法简单地使用 read_excel。我一直在使用“xlsx::loadWorkbook”,这样我就可以在不破坏模板格式的情况下加载整个工作簿。 有谁知道我如何加载工作簿并将其下载为 pdf?另外,我究竟如何实现 selectInput 以便下载正确的文件格式?

预先感谢您提供的任何帮助或指导。

library(shiny)
library(tidyverse)

ui <- fluidPage(title = "Interesting Title",titlePanel("Interesting Header"),sidebarLayout(
                    sidebarPanel(
                        textInput("Date","Valuation Date",placeholder = "Enter date format yyyy-mm-dd"),textInput("AffNumber","Account",placeholder = "Enter Number"),selectInput("format","Download format",c("Pdf" = "pdf","Excel" = "excel")),actionButton("run","Run"),verbatimtextoutput("default"),verbatimtextoutput("default2"),downloadButton("download","Download")
                    ),mainPanel("Text Here")
                )   
)

server <- function(input,output) {
    # Updates the report template with data for user specified account
    combined <- eventReactive(input$run,{
        FinalEP <- Function_To_Obtain_Data_From_PC(input$Date)
        FinalLR <- Function_To_Obtain_Data_From_PC(input$Date)
        Function_To_Create_Formatted_Excel_Workbook(df = FinalLR,df2 = FinalEP,choice = "Individual",Acct = input$AffNumber,RunDate = input$Date)
        Name <- paste(unique(FinalEP$Name[FinalEP$`Account` %in% input$AffNumber]),"as of",input$Date)
        wb <- xlsx::loadWorkbook("file.xlsx")
        done <- list("done" = "Complete","Name" = Name,"wb" = wb)
        return(done)
    })
    

    output$default <- renderText({print((combined()$done))}) # temporary code,used for debugging
    output$default2 <- renderText({print((combined()$Name))}) # temporary code,used for debugging
    
# Successfully downloads excel document
    output$download <- downloadHandler(
        filename = function(){
            paste0(combined()$Name,".xlsx")
        },content = function(file) {
            xlsx::saveWorkbook(combined()$wb,file = file)
        }
    )


# Issue: How can I design this to download the excel workbook as a pdf. 


# Using RDCOMClient I am able to save the workbook as a pdf document,but am unsure how I can integrate this with the downloadHandler formatting.

## RDCOMClient Code
#file_location <- "path to workbook file"
#ex <- COMCreate("Excel.Application")
#book <- ex$workbooks()$Open(file_location)
#sheet <- book$Worksheets()$Item(1)
#sheet$Select()
#ex[["ActiveSheet"]]$ExportAsFixedFormat(Type=0,Filename="example.pdf",IgnorePrintAreas=FALSE)
#ex$Quit()


# Trying to download as pdf document
    # output$download <- downloadHandler(
    #     filename = function(){
    #         paste0(combined()$Name,".pdf")
    #     },#     content = function(file) {
    #         xlsx::saveWorkbook(combined()$wb,file = file)
    #     }
    # )
}

# Run the application
shinyApp(ui = ui,server = server) 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)