tl;dr 你不能继续格式化;你必须编写一个自定义 JavaScript 函数。
PDF
and print
按钮有非常不同的行为。
The print
按钮行为
当您单击print
按钮,您使用用户代理(在本用例中为浏览器)来呈现HTML
文档为分页文档 (PDF)。有一个 W3C 标准名为CSS 分页媒体它定义了 CSS 规则如何应用于分页媒体。
这些 CSS 规则包含在 CSS 中@media print
at规则。
这里有一个关于 CSS 分页媒体的综合指南:print-css.rocks https://print-css.rocks/.
处理 CSS 分页媒体并不简单:
- 浏览器糟糕地实现了 CSS 分页媒体标准;无头用户代理(
wkhtmltopdf
, weasyprint
, XML Prince
...) 用于使用 CSS 分页媒体生成 PDF。使用这些用户代理之一非常容易,因为pandoc 2.0
: 他们可以替换LaTeX
引擎。
- 当你打开一个
HTML
文件,浏览器不适用@media print
默认情况下(它们适用@media screen
at 规则)。所以,很难弄清楚@media print
规则。我知道跟踪这些规则的唯一方法是使用 Chrome 开发者工具(打开菜单,选择More tools
and Rendering
。在里面Rendering
面板,您可以模拟分页媒体选择print
).
由于您想使用浏览器生成样式PDF
,我认为CSS分页媒体规则是一种不切实际的方式。此外,使用带有动态 HTML 文档的无头用户代理作为 Shiny 应用程序非常复杂。所以,我的建议是忘记print
button.
The PDF
按钮行为
DataTables
库依赖于pdfmake
用于生成 PDF 文件的 JavaScript 库。您可以应用自定义样式,将 JavaScript 函数传递给customize的选项pdfHtml5 button https://datatables.net/reference/button/pdfHtml5。该函数自定义发送到的文档对象pdfmake API https://github.com/bpampuch/pdfmake.
为了理解该结构JSON
传递的文档对象DataTables
to pdfmake
,您可以将其输出到浏览器控制台:
library(shiny)
library(DT)
library(dplyr)
data("starwars")
ui <- fluidPage(title = "Ratios",
sidebarLayout(
sidebarPanel(width = 2,
actionButton("button", "Go"), # Emulates data loading
sliderInput("seuil_j", "Threshold J",
min = 0, max = 80, value = 35, step = 0.5)),
mainPanel(
fluidRow(column(width = 12,
DT::dataTableOutput("ratios"))))
)
)
server <- function(input, output, session) {
donnees_ratios <- reactive({
req(input$button)
set.seed(14)
starwars %>%
select(1:10) %>% # DataTables is not happy with list columns
mutate(signe = sample(c(1, -1), replace = TRUE, size = nrow(.)),
ratio_j = signe * mass / height) %>%
select(name, mass, height, signe, ratio_j, everything())
})
output$ratios <- DT::renderDataTable({
donnees_ratios() %>%
creer_DT() %>%
formatter_DT(input)
})
}
creer_DT <- function(donnees) {
datatable(donnees,
rownames = FALSE,
class = 'cell-border stripe compact hover',
extensions = c("Buttons"),
options = list(
dom = 'Blfrtip',
buttons = list(
list(extend = "pdf",
exportOptions = list(stripHtml = FALSE,
columns = ':visible'),
orientation = 'landscape',
customize = JS("function(doc){console.dir(doc);}")),
list(extend = "print",
exportOptions = list(stripHtml = FALSE,
columns = ':visible')),
"excel", "csv", "colvis"),
language = list(
decimal = ",",
thousands = " " # small unbreakable space
)
)
)
}
formatter_DT <- function(table, input) {
table %>%
formatPercentage(columns = c("ratio_j"),
digits = 1L, dec.mark = ",", mark = " ") %>%
formatRound(columns = c("height", "mass"),
digits = 1L, dec.mark = ",", mark = " ") %>%
format_seuil("ratio_j", input$seuil_j)
}
format_seuil <- function(table, column, seuil) {
# Threshold for the aboslute value, and different coloring if higher or lower
formatStyle(table, column,
fontWeight = styleInterval(
c(-seuil / 100, seuil / 100), c("bold", "normal", "bold")),
color = styleInterval(
c(-seuil / 100, seuil / 100), c("red", "black", "orange")
))
}
shinyApp(ui, server)
您可以修改默认样式。这是一个更改字体颜色的示例tableHeader
style:
customize = JS("function(doc){doc.styles.tableHeader.color='yellow';}"))
为了进一步定制,您必须编写自己的 JavaScript 函数。以下是使用百分比格式化第五列的示例:
customize = JS("function(doc){doc.content[1].table.body.forEach(function(el,idx){if(idx>0){el[4].text=String((parseFloat(el[4].text)*100).toFixed(1))+'%'}})}"))