Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 1 | #' Specify the look of the selected column |
| 2 | #' |
| 3 | #' @description This function allows users to select a column and then specify |
| 4 | #' its look. Right now it supports the following three properties: column width, |
| 5 | #' bold text and italic text. |
| 6 | #' |
| 7 | #' @param kable_input Output of `knitr::kable()` with `format` specified |
| 8 | #' @param column A numeric value indicating which column to be selected |
| 9 | #' @param width A character string telling HTML & LaTeX how wide the column |
| 10 | #' needs to be, e.g. "10cm", "3in" or "30em". |
| 11 | #' @param bold A T/F value to control whether the text of the selected column |
| 12 | #' need to be bolded. |
| 13 | #' @param italic A T/F value to control whether the text of the selected column |
| 14 | #' need to be emphasized. |
| 15 | #' @export |
Hao Zhu | f13a35e | 2017-05-24 00:55:43 -0400 | [diff] [blame] | 16 | column_spec <- function(kable_input, column, |
| 17 | width = NULL, bold = F, italic = F) { |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 18 | if (!is.numeric(column)) { |
| 19 | stop("column must be a numeric value") |
| 20 | } |
| 21 | kable_format <- attr(kable_input, "format") |
| 22 | if (!kable_format %in% c("html", "latex")) { |
| 23 | message("Currently generic markdown table using pandoc is not supported.") |
| 24 | return(kable_input) |
| 25 | } |
| 26 | if (kable_format == "html") { |
| 27 | return(column_spec_html(kable_input, column, width, bold, italic)) |
| 28 | } |
| 29 | if (kable_format == "latex") { |
| 30 | return(column_spec_latex(kable_input, column, width, bold, italic)) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | column_spec_html <- function(kable_input, column, width, bold, italic) { |
| 35 | kable_attrs <- attributes(kable_input) |
| 36 | kable_xml <- read_xml(as.character(kable_input), options = "COMPACT") |
| 37 | kable_tbody <- xml_tpart(kable_xml, "tbody") |
| 38 | |
| 39 | group_header_rows <- attr(kable_input, "group_header_rows") |
Hao Zhu | f13a35e | 2017-05-24 00:55:43 -0400 | [diff] [blame] | 40 | all_contents_rows <- seq(1, length(xml_children(kable_tbody))) |
Hao Zhu | bff0191 | 2017-05-23 18:05:00 -0400 | [diff] [blame] | 41 | if (!is.null(group_header_rows)) { |
| 42 | all_contents_rows <- all_contents_rows[!all_contents_rows %in% |
| 43 | group_header_rows] |
| 44 | } |
| 45 | |
| 46 | for (i in all_contents_rows) { |
| 47 | target_cell <- xml_child(xml_child(kable_tbody, i), column) |
| 48 | if (!is.null(width)) { |
| 49 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 50 | "width: ", width, "; ") |
| 51 | } |
| 52 | if (bold) { |
| 53 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 54 | "font-weight: bold;") |
| 55 | } |
| 56 | if (italic) { |
| 57 | xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"), |
| 58 | "font-style: italic;") |
| 59 | } |
| 60 | } |
| 61 | out <- structure(as.character(kable_xml), format = "html", |
| 62 | class = "knitr_kable") |
| 63 | attributes(out) <- kable_attrs |
| 64 | return(out) |
| 65 | } |
| 66 | |
| 67 | column_spec_latex <- function(kable_input, column, width, bold, italic) { |
| 68 | table_info <- magic_mirror(kable_input) |
| 69 | align_collapse <- ifelse(table_info$booktabs, "", "\\|") |
| 70 | kable_align_old <- paste(table_info$align_vector, collapse = align_collapse) |
| 71 | |
| 72 | if (!is.null(width)) { |
| 73 | table_info$align_vector[column] <- paste0("p\\{", width, "\\}") |
| 74 | } |
| 75 | |
| 76 | if (bold | italic) { |
| 77 | usepackage_latex("array") |
| 78 | latex_array_options <- c("\\\\bfseries", "\\\\em")[c(bold, italic)] |
| 79 | latex_array_options <- paste0( |
| 80 | "\\>\\{", paste(latex_array_options, collapse = ""), "\\}" |
| 81 | ) |
| 82 | table_info$align_vector[column] <- paste0(latex_array_options, |
| 83 | table_info$align_vector[column]) |
| 84 | } |
| 85 | |
| 86 | kable_align_new <- paste(table_info$align_vector, collapse = align_collapse) |
| 87 | |
| 88 | out <- sub(kable_align_old, kable_align_new, as.character(kable_input), |
| 89 | perl = T) |
| 90 | out <- structure(out, format = "latex", class = "knitr_kable") |
| 91 | attr(out, "original_kable_meta") <- table_info |
| 92 | return(out) |
| 93 | } |