【问题标题】:Generating downloadable reports from Shiny app从 Shiny 应用程序生成可下载的报告
【发布时间】:2021-05-20 01:19:01
【问题描述】:

我制作了一个 R 脚本,允许使用某种类型的数据集获取 R Markdown 报告。现在我希望其他人能够使用此脚本来获取包含他们的数据的自动报告,但无需使用此脚本(尤其是对于不掌握 R 的人)。

我尝试通过 Shiny 希望创建一个界面来加载数据集并自动生成我的脚本,但我无法在 Shiny 和我的 Rmd 之间建立链接。

如何告诉我的 Rmd 要处理的数据集不是我的 Rmd 脚本要在目录中查找的数据集,而是在 Shiny 界面上加载的数据集?

谢谢

这是带有我的 Rmd 的 Shiny 脚本,名为“traitemant_bis.Rmd”:

library(shiny)
library(rmarkdown)

ui <- fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput(
        inputId = "file1", label = "Choose CSV File",
        multiple = FALSE,
        accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv")
      ),
      radioButtons("format", "Document format", c("PDF", "HTML", "Word"), inline = TRUE)
    ),
    mainPanel(
      tableOutput("contents"),
      downloadButton("downloadReport")
    )
  )
)


server <- function(input, output) {
  dataset <- reactive({
    req(input$file1)
    read.csv(file = input$file1$datapath,
             na.strings = ".", 
             sep = ";",
             header = TRUE,
             nrows=10)               
  })
  
  output$contents <- renderTable({
    req(dataset())
    head(dataset())
  })
  
  output$downloadReport <- downloadHandler(
    filename = function() {
      paste("my-report", sep = ".", switch(
        input$format, PDF = "pdf", HTML = "html", Word = "docx"
      ))
    },
    
    content = function(file) {
      src <- normalizePath("traitemant_bis.Rmd")
      
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, "traitemant_bis.Rmd", overwrite = TRUE)
      
      out <- render("traitemant_bis.Rmd", switch(
        input$format,
        PDF = pdf_document(), HTML = html_document(), Word = word_document()
      ))
      file.rename(out, file)
    }
  )
}

shinyApp(ui, server) ```

【问题讨论】:

    标签: r shiny r-markdown report


    【解决方案1】:

    我举了一个简单的例子来说明如何实现这一点。基本上,您可以将 shiny 中的任何数据作为 params 传递给 Rmd

    如果您有多个数据框或任何数据将它们转换为单个列表并作为参数传递,您可以稍后在 RMarkdown 中提取单个数据

    app.R

    library(shiny)
    ui <- fluidPage(
    
        # Application title
        titlePanel("RMD example"),
        
    
        downloadButton("btn", "Generate Report")
    
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
    
        data <- reactive({
            mtcars
        })
        
        
        output$btn <- downloadHandler(
            
            filename = function(){"myreport.docx"},
            content = function(file) {
                
                    tempReport <- file.path(tempdir(),"markdown.Rmd")
                    file.copy("markdown.Rmd", tempReport, overwrite = TRUE)
                    rmarkdown::render("markdown.Rmd", output_format = "word_document", output_file = file,
                                      params = list(table = data()), # here I'm passing data in params
                                      envir = new.env(parent = globalenv()),clean=F,encoding="utf-8"
                    )
                    
                
            }
        )
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    
    

    Rmd 文件

    ---
    title: "Untitled"
    author: "Mohan"
    date: "2/17/2021"
    params:
        table: [some object]
    output: word_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ## R Markdown
    
    This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
    
    When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
    
    ```{r cars}
    params$table -> data
    
    data
    
    summary(data)
    ```

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-12
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      相关资源
      最近更新 更多