Hao Zhu | cc21dc7 | 2017-05-20 01:15:25 -0400 | [diff] [blame^] | 1 | #' Print the table on an isolated landscape page in PDF |
| 2 | #' |
| 3 | #' @description For very wide tables in PDF documents, sometimes it might be a |
| 4 | #' good idea to have a single landscape page to house it. This function |
| 5 | #' will rotate the PDF page for the applied table in LaTeX environment. It won't |
| 6 | #' have any effects on HTML. |
| 7 | #' |
| 8 | #' @param kable_input Output of `knitr::kable()` with `format` specified |
| 9 | #' @param margin Customizable page margin for special needs. Values can be |
| 10 | #' "1cm", "1in" or similar. |
| 11 | #' |
| 12 | #' @export |
| 13 | landscape <- function(kable_input, margin = NULL) { |
| 14 | kable_format <- attr(kable_input, "format") |
| 15 | if (!kable_format %in% c("html", "latex")) { |
| 16 | message("Currently generic markdown table using pandoc is not supported.") |
| 17 | return(kable_input) |
| 18 | } |
| 19 | if (kable_format == "html") { |
| 20 | return(kable_input) |
| 21 | } |
| 22 | if (kable_format == "latex") { |
| 23 | return(landscape_latex(kable_input, margin)) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | landscape_latex <- function(kable_input, margin) { |
| 28 | kable_attrs <- attributes(kable_input) |
| 29 | usepackage_latex("pdflscape") |
| 30 | out <- paste0( |
| 31 | "\n\\begin{landscape}", kable_input, "\n\\end{landscape}" |
| 32 | ) |
| 33 | |
| 34 | if (!is.null(margin)) { |
| 35 | out <- paste0( |
| 36 | "\n\\newgeometry{margin=", margin, "}", out, "\n\\restoregeometry" |
| 37 | ) |
| 38 | } |
| 39 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 40 | attributes(out) <- kable_attrs |
| 41 | attr(out, "landscape") <- TRUE |
| 42 | return(out) |
| 43 | } |