首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下载DT::datatable as pdf在闪亮的应用程序中

下载DT::datatable as pdf在闪亮的应用程序中
EN

Stack Overflow用户
提问于 2022-03-16 00:59:11
回答 1查看 432关注 0票数 0

是否可以通过DT::datatable应用程序下载shiny作为pdf?

代码语言:javascript
复制
#app.r
library(shiny)
library(ggplot2)
library(magrittr)
library(DT)
ui <- shinyUI(
  fluidPage(
    column(3,
      
      downloadButton(
        outputId = "downloader",
        label = "Download PDF"
      )
    ),
    column(
      width = 3,
      dataTableOutput("table")
    )
    
  )
)

server <- shinyServer(function(input, output, session){
  
  #****************************************
  #* Reactive Values
  
  table <- reactive({
    mtcars
  })
  

  
  #****************************************
  #* Output Components
  
  output$table <- 
    renderDataTable({
      table()
    })
  
  
  
  #****************************************
  #* Download Handlers
  
  output$downloader <- 
    downloadHandler(
      "results_from_shiny.pdf",
      content = 
        function(file)
        {
          rmarkdown::render(
            input = "report_file.Rmd",
            output_file = "built_report.pdf",
            params = list(table = table()
                          )
          ) 
          readBin(con = "built_report.pdf", 
                  what = "raw",
                  n = file.info("built_report.pdf")[, "size"]) %>%
            writeBin(con = file)
        }
    )
})

shinyApp(ui, server)

代码语言:javascript
复制
#report_file.rmd
---
title: "Parameterized Report for Shiny"
output: pdf_document
params:
  table: 'NULL'
---


```{r}

副词[“桌子”]

代码语言:javascript
复制
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-16 11:08:51

您可以尝试Buttons数据扩展的Buttons按钮。这样您就不需要downloadHandler了。否则,下面是一个使用好的旧xtable包的解决方案。对于更复杂的表,请使用kableExtra

代码语言:javascript
复制
library(shiny)
library(DT)
library(xtable)
library(withr)
library(shinybusy)

ui <- fluidPage(
  add_busy_spinner(spin = "cube-grid", onstart = FALSE),
  column(
    width = 3,
    downloadButton(
      outputId = "downloader",
      label = "Download PDF"
    )
  ),
  column(
    width = 3,
    DTOutput("table")
  )
  
)


server <- function(input, output, session){
  
  table <- reactive({
    mtcars
  })
  
  #****************************************
  #* Output Components
  
  output[["table"]] <- renderDT({
    datatable(table())
  })
  
  #****************************************
  #* Download Handlers
  
  output[["downloader"]] <- downloadHandler(
    filename = "results_from_shiny.pdf",
    content = function(file){
      texfile <- paste0(tools::file_path_sans_ext(file), ".tex")
      latex <- print.xtable(
        xtable(table()), print.results = FALSE, 
        floating = FALSE, scalebox = "0.7"
      )
      writeLines(
        c(
          "\\documentclass[12pt]{standalone}",
          "\\usepackage{graphics}",
          "\\usepackage{caption}",
          "\\begin{document}",
          "\\minipage{\\textwidth}",
          latex,
          "\\captionof{table}{My caption}",
          "\\endminipage",
          "\\end{document}"
        ),
        texfile
      )
      with_dir(
        dirname(texfile), 
        tools::texi2pdf(texfile, clean = TRUE)
      )
    }
  )
}

shinyApp(ui, server)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71490684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档