如何解决有什么方法可以将我的PDFoutput表转换为乳胶?
我希望导出的数据显示效果很好。为此,我必须使用一些方法来自定义我的报告。
预期结果是将我的PDF输出中的数据自定义到LaTeX中(或者以一种很好的方式)。有可能吗?
谢谢!
library(shiny)
library(DT)
library(base64enc)
ui <- fluidPage(
tags$head(
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"),tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js")
),br(),DTOutput("dtable")
)
server <- function(input,output,session){
output[["dtable"]] <- renderDT({
datatable(
iris[1:5,],extensions = "Buttons",options = list(
dom = "Bfrtip",buttons = list(
list(
extend = "pdfHtml5",customize = JS(
"function(doc) {"," doc.content.splice( 1,{"," margin: [ 0,12 ],"," alignment: 'center',sprintf(
" image: '%s',dataURI(
file = "https://www.r-project.org/logo/Rlogo.png",mime = "image/png"
)
)," width: 50"," })","}"
)
)
)
)
)
})
}
shinyApp(ui,server)
解决方法
要在普通浏览器(Chrome,Firefox等)中使用:
library(shiny)
library(DT)
library(base64enc)
library(rmarkdown)
js <- '
Shiny.addCustomMessageHandler("download",function(b64) {
const a = document.createElement("a");
document.body.append(a);
a.download = "report.pdf";
a.href = b64;
a.click();
a.remove();
})
'
ui <- basicPage(
tags$head(
tags$script(HTML(js))
),br(),DTOutput("dtable")
)
server <- function(input,output,session){
dat <- iris[1:13,]
output[["dtable"]] <- renderDT({
datatable(
dat,extensions = "Buttons",options = list(
dom = "Bfrtip",buttons = list(
list(
extend = "collection",text = "Report",action = JS(
"function ( e,dt,node,config ) {"," var array = dt.data().toArray();"," var tarray = Object.keys(array[0]).map(function(c) {"," return array.map(function(r) { return r[c]; });"," });"," var df = {};"," for(var i = 1; i <= tarray.length; ++i) {"," df['V' + i] = tarray[i-1];"," }"," Shiny.setInputValue('report',df);","}")
)
)
)
)
},server = FALSE)
observeEvent(input[["report"]],{
df <-
setNames(as.data.frame(lapply(input[["report"]][-1],unlist)),names(dat))
showNotification("Creating report...",type = "message")
tmpReport <- tempfile(fileext = ".Rmd")
file.copy("report.Rmd",tmpReport)
outfile <- file.path(tempdir(),"report.pdf")
render(
tmpReport,output_file = outfile,params = list(
data = df
)
)
b64 <- dataURI(
file = outfile,mime = "application/pdf"
)
session$sendCustomMessage("download",b64)
})
}
shinyApp(ui,server)
文件 report.Rmd :
---
title: "My awesome report"
author: "John Doe"
date: "21/10/2020"
output: pdf_document
params:
data: "x"
---
```{r setup,include=FALSE}
library(knitr)
opts_chunk$set(echo = FALSE)
```
## Data
```{r}
kable(params[["data"]],row.names = FALSE)
```
带有徽标的按钮:
tags$head(
tags$script(HTML(js)),tags$link(rel = "stylesheet",href = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css",integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==",crossorigin="anonymous")
),
和:
buttons = list(
list(
extend = "collection",text = "<span style='font-size:20px;'><i class='fa fa-file-pdf' style='color:red;'></i> Report</span>",
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。