Hao Zhu | 8f41720 | 2017-05-20 16:37:14 -0400 | [diff] [blame^] | 1 | #' Add indentations to row headers |
| 2 | #' @export |
| 3 | add_indent <- function(kable_input, positions) { |
| 4 | kable_format <- attr(kable_input, "format") |
| 5 | if (!kable_format %in% c("html", "latex")) { |
| 6 | message("Currently generic markdown table using pandoc is not supported.") |
| 7 | return(kable_input) |
| 8 | } |
| 9 | if (kable_format == "html") { |
| 10 | return(kable_input) |
| 11 | } |
| 12 | if (kable_format == "latex") { |
| 13 | return(add_indent_latex(kable_input, positions)) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | add_indent_latex <- function(kable_input, positions) { |
| 18 | table_info <- attr(kable_input, "original_kable_meta") |
| 19 | if (is.null(table_info)) { |
| 20 | table_info <- magic_mirror(kable_input) |
| 21 | } |
| 22 | |
| 23 | if (!is.numeric(positions)) { |
| 24 | stop("Positions can only take numeric row numbers (excluding header rows).") |
| 25 | } |
| 26 | if (max(positions) > table_info$nrow - 1) { |
| 27 | stop("There aren't that many rows in the table. Check positions in ", |
| 28 | "add_indent_latex.") |
| 29 | } |
| 30 | |
| 31 | out <- kable_input |
| 32 | for (i in positions) { |
| 33 | rowtext <- table_info$contents[i + 1] |
| 34 | out <- sub(rowtext, latex_indent_unit(rowtext), out) |
| 35 | } |
| 36 | return(out) |
| 37 | } |
| 38 | |
| 39 | latex_indent_unit <- function(rowtext) { |
| 40 | paste0("\\\\hspace{1em}", rowtext) |
| 41 | } |