如何将自定义 pdf 报告添加到 zip 文件以在闪亮的应用程序中下载

问题描述 投票:0回答:1

我想创建一个可定制的 pdf 报告,用户可以从闪亮的应用程序下载该报告。但是,我希望将 pdf 添加到 zip 文件中,并下载其他项目。我正在遵循这些说明,但我不太明白。

这是一个 reprex 应用程序,展示了到目前为止我的代码:


library(shiny)
library(datasets)

# Define UI for application
ui <- fluidPage(

    # Application title
    titlePanel("Data Download Test"),

    # Sidebar with dataset dropdown and download button
    sidebarLayout(
        sidebarPanel(
            selectInput("dataset", "Select Dataset",
                        c("Orange", "OrchardSprays", "PlantGrowth")),

            downloadButton("download", "Download")
        ),

        # Show a table
        mainPanel(
           tableOutput("datatable")
        )
    )
)

# Define server logic
server <- function(input, output) {

  #table output
    output$datatable <- renderTable({
      dataset <- get(input$dataset)
    })

    #download server logic
    output$download <- downloadHandler(

      #name of file
      filename = function() {
        paste0("Dataset_", Sys.Date(), ".zip")
      },

      #content of file
      content = function(file) {
        #set wd to temp directory
        tmpdir <- tempdir()
        setwd(tempdir())

        #MAKING CSV
        # Start a sink file with a CSV extension
        sink("dataset.csv")
        cat('\n')
        cat(paste0("Here is the dataset ", input$dataset, "!"))
        cat('\n')
        cat('\n')

        # Write metrics dataframe to the same sink
        write.csv(dataset <- get(input$dataset), row.names = F)

        # Close the sink
        sink()

        #MAKING PDF REPORT
        #copy file
        tempReport <- file.path(tmpdir, "download_reports/report.Rmd")
        file.copy("report.Rmd", tempReport, overwrite = TRUE)

        # Set up parameters to pass to Rmd document
        params <- list(selected_dataset = input$dataset)

        # 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 = "report.pdf",
                          params = params,
                          envir = new.env(parent = globalenv()))

        # Zip them up
        zip(file, c("dataset.csv", "report.pdf"))
      }
      )

}

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

我的 .rmd 模板如下所示:

---
title: "Dataset"
output: pdf_document
params:
  selected_dataset: NA
---

```{r}
params$selected_dataset

...etc.

该模板位于

download_reports/report.Rmd
的闪亮 app r 项目文件中。当我仅将 csv 添加到 zip 文件时,该代码有效,但当我尝试将 pdf 报告添加到 zip 文件时,它会出现以下错误

Warning in normalizePath(x, winslash = winslash, mustWork = must_work) :
  path[1]="/var/folders/_g/0xs4r4jd6kncwg40b2bp21zm0000gn/T//Rtmpmaqfgy/download_reports/report.Rmd": No such file or directory
Warning: Error in abs_path: The file '/var/folders/_g/0xs4r4jd6kncwg40b2bp21zm0000gn/T//Rtmpmaqfgy/download_reports/report.Rmd' does not exist.

我认为这与文件路径和/或临时工作目录有关......

r shiny r-markdown
1个回答
0
投票

如果您确定所有文件都存在,您可以尝试以下模式:

output$download <- downloadHandler(
  filename = 'my.zip',
  content = function(filename) {

      zip_file <- "internal.zip"
      
      list_flies <- c( "a.a", 'b.b', 'etc' )
      
      
      # ## Linux
      # # the following zip method works for me in linux but substitute with whatever method working in your OS 
      # zip_command <<- paste("zip -j", 
      #                       zip_file, 
      #                       paste(list_flies, collapse = " "))
      # 
      # system(zip_command)

      
      ## Windows: 
      zip(zip_file, list_flies )
      
            
      # copy the zip file to the file argument
      file.copy(zip_file, filename) ## here's what you are mainly missing


      # remove all the files created
      try(file.remove(zip_file))
  })
© www.soinside.com 2019 - 2024. All rights reserved.