I have a rather large table, that if I fit it into the DT
table and do a PDF print on it, there will be portions of the column that will be cut off. Also, I wanted to adjust the size of the title when exporting it to the PDF as well. How should I go about this?
Here's an example of what I'm talking about when all the columns don't fit into one page. How do I adjust the size of the table and the title inside the DT
table PDF?
library("shiny")
library("DT")
data <- cbind(iris,iris,iris)
shinyApp(
ui = fluidPage(DT::dataTableOutput('tbl')),
server = function(input, output) {
output$tbl = DT::renderDataTable(
data,extensions="Buttons",options = list(dom = 'Bt',
buttons = list( list( extend = 'pdf',
pageSize = 'A4',
orientation = 'landscape',
filename = 'tt',
title= "A smaller title here")))
)
}
)
Several things I've tried:
capture
package: Will print the entire table to how I adjust and format the table in Shiny, but all in one PDF page, which is not ideal since I want the PDF to be printable over several A4 pages.width
argument in DTOutput
: Will not show up adjusted in the PDF export table.You can use the customize
callback of the pdf
extension to change the pdfMake
options, which is the workhorse of the pdf rendering. For example you can set the column width such that each column uses only a percentage of the total width.
Similarly, you can change the styling of the title with the same idea:
library(shiny)
library(DT)
js_cb <- JS("function(pdf_opts, btn, dt) {
let n = pdf_opts.content[1].table.body[0].length;
// each column occupies a percentage of the total width
let widths = Array(n).fill(100 / n + '%');
pdf_opts.content[1].table.widths = widths;
pdf_opts.styles.title.fontSize = 8; // change fontsize
}")
data <- cbind(iris,iris,iris)
shinyApp(
ui = fluidPage(DT::dataTableOutput('tbl')),
server = function(input, output) {
output$tbl = DT::renderDataTable(
data,
extensions = "Buttons",
options = list(dom = "Bt",
buttons = list(list(extend = "pdf",
pageSize = "A4",
orientation = "landscape",
filename = "tt",
customize = js_cb,
title = "A smaller title here")))
)
}
)