是否可以通过DT::datatable应用程序下载shiny作为pdf?
#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)和
#report_file.rmd
---
title: "Parameterized Report for Shiny"
output: pdf_document
params:
table: 'NULL'
---
```{r}副词[“桌子”]
发布于 2022-03-16 11:08:51
您可以尝试Buttons数据扩展的Buttons按钮。这样您就不需要downloadHandler了。否则,下面是一个使用好的旧xtable包的解决方案。对于更复杂的表,请使用kableExtra。
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)https://stackoverflow.com/questions/71490684
复制相似问题