add vector support to row_spec (HTML & PDF)
diff --git a/R/column_spec.R b/R/column_spec.R
index 27c76b6..615a22e 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -5,7 +5,7 @@
#' bold text and italic text.
#'
#' @param kable_input Output of `knitr::kable()` with `format` specified
-#' @param columns A numeric value or vector indicating which column(s) to be selected.
+#' @param column A numeric value or vector indicating which column(s) to be selected.
#' @param width A character string telling HTML & LaTeX how wide the column
#' needs to be, e.g. "10cm", "3in" or "30em".
#' @param bold A T/F value to control whether the text of the selected column
@@ -24,22 +24,18 @@
#' @param border_right A logical variable indicating whether there should be a
#' border line on the right of the selected column. In HTML, you can also pass
#' in a character string for the CSS of the border line
-#' @param column Deprecating. Same with columns
#'
#' @examples x <- knitr::kable(head(mtcars), "html")
#' column_spec(x, 1, width = "20em", bold = TRUE, italic = TRUE)
#'
#' @export
-column_spec <- function(kable_input, columns = NULL,
+column_spec <- function(kable_input, column,
width = NULL, bold = FALSE, italic = FALSE,
monospace = FALSE, color = NULL, background = NULL,
border_left = FALSE, border_right = FALSE,
- column, ...) {
- if (is.null(columns)) {
- columns <- column
- }
- if (!is.numeric(columns)) {
- stop("columns/column must be numeric. Note that column is deprecating.")
+ ...) {
+ if (!is.numeric(column)) {
+ stop("column must be numeric. ")
}
kable_format <- attr(kable_input, "format")
if (!kable_format %in% c("html", "latex")) {
@@ -47,20 +43,20 @@
return(kable_input)
}
if (kable_format == "html") {
- return(column_spec_html(kable_input, columns, width,
+ return(column_spec_html(kable_input, column, width,
bold, italic, monospace,
color, background,
border_left, border_right))
}
if (kable_format == "latex") {
- return(column_spec_latex(kable_input, columns, width,
+ return(column_spec_latex(kable_input, column, width,
bold, italic, monospace,
color, background,
border_left, border_right, ...))
}
}
-column_spec_html <- function(kable_input, columns, width,
+column_spec_html <- function(kable_input, column, width,
bold, italic, monospace,
color, background,
border_left, border_right) {
@@ -89,7 +85,7 @@
}
for (i in all_contents_rows) {
- for (j in columns) {
+ for (j in column) {
target_cell <- xml_child(xml_child(kable_tbody, i), j)
if (!is.null(width)) {
xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
@@ -131,7 +127,7 @@
return(out)
}
-column_spec_latex <- function(kable_input, columns, width,
+column_spec_latex <- function(kable_input, column, width,
bold, italic, monospace,
color, background,
border_left, border_right,
@@ -144,8 +140,8 @@
align_collapse <- ifelse(table_info$booktabs, "", "\\|")
kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
- table_info$align_vector[columns] <- unlist(lapply(
- table_info$align_vector_origin[columns],
+ table_info$align_vector[column] <- unlist(lapply(
+ table_info$align_vector_origin[column],
function(x) {
latex_column_align_builder(
x, width, bold, italic, monospace,
@@ -163,7 +159,7 @@
if (is.null(table_info$column_width)) {
table_info$column_width <- list()
}
- for (i in columns) {
+ for (i in column) {
table_info$column_width[[paste0("column_", i)]] <- width
}
}
diff --git a/R/row_spec.R b/R/row_spec.R
index 7a17e8c..5a77ee4 100644
--- a/R/row_spec.R
+++ b/R/row_spec.R
@@ -5,7 +5,7 @@
#' italic text.
#'
#' @param kable_input Output of `knitr::kable()` with `format` specified
-#' @param row A numeric value indicating which row to be selected. You don't
+#' @param row A numeric value or vector indicating which row(s) to be selected. You don't
#' need to count in header rows or group labeling rows.
#' @param bold A T/F value to control whether the text of the selected row
#' need to be bolded.
@@ -26,7 +26,7 @@
bold = FALSE, italic = FALSE, monospace = FALSE,
color = NULL, background = NULL) {
if (!is.numeric(row)) {
- stop("row must be a numeric value")
+ stop("row must be numeric. ")
}
kable_format <- attr(kable_input, "format")
if (!kable_format %in% c("html", "latex")) {
@@ -51,36 +51,38 @@
group_header_rows <- attr(kable_input, "group_header_rows")
if (!is.null(group_header_rows)) {
- row <- positions_corrector(row, group_header_rows,
- length(xml_children(kable_tbody)))
+ row <- positions_corrector(row, group_header_rows,
+ length(xml_children(kable_tbody)))
}
- target_row <- xml_child(kable_tbody, row)
-
- for (i in 1:length(xml_children(target_row))) {
- target_cell <- xml_child(target_row, i)
- if (bold) {
- xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
- "font-weight: bold;")
- }
- if (italic) {
- xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
- "font-style: italic;")
- }
- if (monospace) {
- xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
- "font-family: monospace;")
- }
- if (!is.null(color)) {
- xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
- "color: ", color, ";")
- }
- if (!is.null(background)) {
- xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
- "background-color: ",
- background, ";")
+ for (j in row) {
+ target_row <- xml_child(kable_tbody, j)
+ for (i in 1:length(xml_children(target_row))) {
+ target_cell <- xml_child(target_row, i)
+ if (bold) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ "font-weight: bold;")
+ }
+ if (italic) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ "font-style: italic;")
+ }
+ if (monospace) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ "font-family: monospace;")
+ }
+ if (!is.null(color)) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ "color: ", color, ";")
+ }
+ if (!is.null(background)) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ "background-color: ",
+ background, ";")
+ }
}
}
+
out <- as_kable_xml(kable_xml)
attributes(out) <- kable_attrs
return(out)
@@ -89,7 +91,22 @@
row_spec_latex <- function(kable_input, row, bold, italic, monospace,
color, background) {
table_info <- magic_mirror(kable_input)
- target_row <- table_info$contents[row + 1]
+ out <- enc2utf8(as.character(kable_input))
+ row <- row + 1
+ for (i in row) {
+ target_row <- table_info$contents[i]
+ new_row <- latex_new_row_builder(target_row, bold, italic, monospace,
+ color, background)
+ out <- sub(target_row, new_row, out, perl = T)
+ }
+
+ out <- structure(out, format = "latex", class = "knitr_kable")
+ attr(out, "kable_meta") <- table_info
+ return(out)
+}
+
+latex_new_row_builder <- function(target_row, bold, italic, monospace,
+ color, background) {
new_row <- latex_row_cells(target_row)
if (bold) {
new_row <- lapply(new_row, function(x) {
@@ -118,9 +135,5 @@
new_row <- paste0("\\\\rowcolor{", background, "} ", new_row)
}
- out <- sub(target_row, new_row, enc2utf8(as.character(kable_input)),
- perl = T)
- out <- structure(out, format = "latex", class = "knitr_kable")
- attr(out, "kable_meta") <- table_info
- return(out)
+ return(new_row)
}
diff --git a/man/column_spec.Rd b/man/column_spec.Rd
index efe653d..dc10432 100644
--- a/man/column_spec.Rd
+++ b/man/column_spec.Rd
@@ -6,14 +6,12 @@
\usage{
column_spec(kable_input, column, width = NULL, bold = FALSE,
italic = FALSE, monospace = FALSE, color = NULL, background = NULL,
- border_left = FALSE, border_right = FALSE)
+ border_left = FALSE, border_right = FALSE, ...)
}
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
-\item{column}{A numeric value indicating which column to be selected. When
-you do the counting, ignore the extra header columns you added through
-add_header_left.}
+\item{column}{A numeric value or vector indicating which column(s) to be selected.}
\item{width}{A character string telling HTML & LaTeX how wide the column
needs to be, e.g. "10cm", "3in" or "30em".}
diff --git a/man/row_spec.Rd b/man/row_spec.Rd
index e31ba67..eaf7b57 100644
--- a/man/row_spec.Rd
+++ b/man/row_spec.Rd
@@ -10,7 +10,7 @@
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
-\item{row}{A numeric value indicating which row to be selected. You don't
+\item{row}{A numeric value or vector indicating which row(s) to be selected. You don't
need to count in header rows or group labeling rows.}
\item{bold}{A T/F value to control whether the text of the selected row