【问题标题】:how to put shinyApp html out in PDF or other format如何将 ShinyApp html 以 PDF 或其他格式输出
【发布时间】:2020-06-15 09:34:38
【问题描述】:

这是我希望最终以 PDF 格式存储的代码示例。

library(shiny)
library(arsenal)
library(knitr)
library(survival)

# 创建带有列和标识符的随机数据集 样品

random <- data.frame(replicate(10,sample(5:100, 10)))

samples <- data.frame(replicate(10,sample(0:1, 1)))

randomdf <- merge(samples,random, by= "row.names")
rm(random, samples)

我删除了带有行名的列以获得更好的表格示例

randomdf$Row.names <- NULL

用 Shiny 创建表

shinyApp(
  ui = fluidPage(tableOutput("table")), # creating a page with fluidpage
  server = function(input, output) {
    output$table <- renderTable({
      as.data.frame(summary(tableby(randomdf$replicate.10..sample.0.1..1..~., 
                                    data = randomdf, 
                                    numeric.stats=c("medianq1q3"), 
                                    numeric.test="kwt"), 
                            text = "html",
                            digits = 2))
    },striped=TRUE, sanitize.text.function = function(x) x)

  }
)

我找到了下面应该生成 PDF 的代码,但我不确定如何使用之前的代码来实现它。

shinyApp(
        ui <- fluidPage(tableOutput("table"),
                 downloadButton("report","Generate report")), 

  server <-function(input, output) {
    rendertable to indicate that
    inputs change

    output$table <- renderTable({
     as.data.frame(summary(tableby(randomdf$replicate.10..sample.0.1..1..~., 
                                    data = randomdf, 
                                    numeric.stats=c("medianq1q3"), 
                                    numeric.test="kwt"), 
                            text = "html",
                            digits = 2))
    },striped=TRUE, sanitize.text.function = function(x) x)

# 这里是我现在添加新代码的地方

 output$report <- downloadHandler(
      filename = "report.pdf",
      content = function(file) {
        tempReport <- file.path(tempdir(), "report.Rmd")
        file.copy("report.Rmd", tempReport, overwrite = TRUE)

        params = list(n= input$table)

        rmarkdown::render(tempReport, output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv()))
      }
    )
  }
)

现在,我的 html 中确实有“生成报告”按钮,但由于以下错误,我无法生成报告:

normalizePath 中的警告(路径,winslash = winslash,mustWork = 必须工作):
路径[1]="/var/folders/4g/ld58jxf94s74vrcf_1gd07bh0000gn/T//RtmphCZeLD/report.Rmd": 没有这样的文件或目录警告:abs_path 中的错误:文件 '/var/folders/4g/ld58jxf94s74vrcf_1gd07bh0000gn/T//RtmphCZeLD/report.Rmd' 不存在。 [没有可用的堆栈跟踪]

【问题讨论】:

标签: r shiny pdf-generation


【解决方案1】:

我使用 R Markdown 代替它完美地满足了我的需要

table_one <- tableby(Sample ~., data = Random, 
                     numeric.stats=c("medianq1q3"), 
                     numeric.test="kwt", 
                     digits = 2,
                     digits.p= 2)

summary(sort(table_one, increasing = TRUE), text=TRUE, title = "test")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2019-03-16
    • 2020-08-27
    • 2016-08-18
    相关资源
    最近更新 更多