blob: 5727fe6fa4d1b570bfac67dfa43d7050b8320fbe [file] [log] [blame]
Hao Zhucc21dc72017-05-20 01:15:25 -04001#' Print the table on an isolated landscape page in PDF
2#'
Hao Zhu8f417202017-05-20 16:37:14 -04003#' @description This function will put the table on an single landscape page.
4#' It's useful for wide tables that cann't be printed on a portrait page.
Hao Zhucc21dc72017-05-20 01:15:25 -04005#'
6#' @param kable_input Output of `knitr::kable()` with `format` specified
7#' @param margin Customizable page margin for special needs. Values can be
8#' "1cm", "1in" or similar.
9#'
10#' @export
11landscape <- function(kable_input, margin = NULL) {
12 kable_format <- attr(kable_input, "format")
13 if (!kable_format %in% c("html", "latex")) {
14 message("Currently generic markdown table using pandoc is not supported.")
15 return(kable_input)
16 }
17 if (kable_format == "html") {
18 return(kable_input)
19 }
20 if (kable_format == "latex") {
21 return(landscape_latex(kable_input, margin))
22 }
23}
24
25landscape_latex <- function(kable_input, margin) {
26 kable_attrs <- attributes(kable_input)
27 usepackage_latex("pdflscape")
28 out <- paste0(
29 "\n\\begin{landscape}", kable_input, "\n\\end{landscape}"
30 )
31
32 if (!is.null(margin)) {
33 out <- paste0(
34 "\n\\newgeometry{margin=", margin, "}", out, "\n\\restoregeometry"
35 )
36 }
37 out <- structure(out, format = "latex", class = "knitr_kable")
38 attributes(out) <- kable_attrs
39 attr(out, "landscape") <- TRUE
40 return(out)
41}