add all_cols to add_indent
diff --git a/R/add_indent.R b/R/add_indent.R
index bddc5e4..8a90c49 100644
--- a/R/add_indent.R
+++ b/R/add_indent.R
@@ -3,20 +3,15 @@
#' @param kable_input Output of `knitr::kable()` with `format` specified
#' @param positions A vector of numeric row numbers for the rows that need to
#' be indented.
-#' @param level_of_indent a numeric value for the indent level
+#' @param level_of_indent a numeric value for the indent level. Default is 1.
#'
#' @examples x <- knitr::kable(head(mtcars), "html")
#' # Add indentations to the 2nd & 4th row
#' add_indent(x, c(2, 4), level_of_indent = 1)
#'
#' @export
-add_indent <- function(kable_input, positions, level_of_indent) {
-
- if(missing(level_of_indent)){
- level_of_indent = 1
- }else{
- level_of_indent = level_of_indent
- }
+add_indent <- function(kable_input, positions,
+ level_of_indent = 1, all_cols = FALSE) {
if (!is.numeric(positions)) {
stop("Positions can only take numeric row numbers (excluding header rows).")
@@ -29,25 +24,20 @@
return(kable_input)
}
if (kable_format == "html") {
- return(add_indent_html(kable_input, positions, level_of_indent))
+ return(add_indent_html(kable_input, positions, level_of_indent, all_cols))
}
if (kable_format == "latex") {
- return(add_indent_latex(kable_input, positions, level_of_indent))
+ return(add_indent_latex(kable_input, positions, level_of_indent, all_cols))
}
}
# Add indentation for LaTeX
-add_indent_latex <- function(kable_input, positions, level_of_indent) {
+add_indent_latex <- function(kable_input, positions,
+ level_of_indent = 1, all_cols = FALSE) {
table_info <- magic_mirror(kable_input)
out <- solve_enc(kable_input)
level_of_indent<-as.numeric(level_of_indent)
- if(missing(level_of_indent)){
- level_of_indent = 1
- }else{
- level_of_indent = level_of_indent
- }
-
if (table_info$duplicated_rows) {
dup_fx_out <- fix_duplicated_rows_latex(out, table_info)
out <- dup_fx_out[[1]]
@@ -63,10 +53,19 @@
for (i in positions + table_info$position_offset) {
rowtext <- table_info$contents[i]
+ if (all_cols) {
+ new_rowtext <- latex_row_cells(rowtext)
+ new_rowtext <- lapply(new_rowtext, function(x) {
+ paste0("\\\\hspace\\{", level_of_indent ,"em\\}", x)
+ })
+ new_rowtext <- paste(unlist(new_rowtext), collapse = " & ")
+ } else {
+ new_rowtext <- latex_indent_unit(rowtext, level_of_indent)
+ }
out <- sub(paste0(rowtext, "\\\\\\\\"),
- paste0(latex_indent_unit(rowtext), "\\\\\\\\"),
+ paste0(new_rowtext, "\\\\\\\\"),
out, perl = TRUE)
- table_info$contents[i] <- latex_indent_unit(rowtext)
+ table_info$contents[i] <- new_rowtext
}
out <- structure(out, format = "latex", class = "knitr_kable")
attr(out, "kable_meta") <- table_info
@@ -75,24 +74,18 @@
}
-latex_indent_unit <- function(rowtext) {
- paste0("\\\\hspace\\{",level_of_indent,"em\\}", rowtext)
+latex_indent_unit <- function(rowtext, level_of_indent) {
+ paste0("\\\\hspace\\{", level_of_indent ,"em\\}", rowtext)
}
# Add indentation for HTML
-add_indent_html <- function(kable_input, positions, level_of_indent) {
-
- if(missing(level_of_indent)){
- level_of_indent = 1
- }else{
- level_of_indent = level_of_indent
- }
-
+add_indent_html <- function(kable_input, positions,
+ level_of_indent = 1, all_cols = FALSE) {
kable_attrs <- attributes(kable_input)
- kable_xml <- read_kable_as_xml(kable_input)
+ kable_xml <- kable_as_xml(kable_input)
kable_tbody <- xml_tpart(kable_xml, "tbody")
group_header_rows <- attr(kable_input, "group_header_rows")
@@ -102,20 +95,30 @@
}
for (i in positions) {
- node_to_edit <- xml_child(xml_children(kable_tbody)[[i]], 1)
- if (!xml_has_attr(node_to_edit, "indentlevel")) {
- xml_attr(node_to_edit, "style") <- paste(
- xml_attr(node_to_edit, "style"), "padding-left: ",paste0(level_of_indent*2,"em;")
- )
- xml_attr(node_to_edit, "indentlevel") <- 1
+ row_to_edit = xml_children(kable_tbody)[[i]]
+ if (all_cols) {
+ target_cols = 1:xml2::xml_length(row_to_edit)
} else {
- indentLevel <- as.numeric(xml_attr(node_to_edit, "indentlevel"))
- xml_attr(node_to_edit, "style") <- sub(
- paste0("padding-left: ", indentLevel * 2, "em;"),
- paste0("padding-left: ", (indentLevel + 1) * 2, "em;"),
- xml_attr(node_to_edit, "style")
- )
- xml_attr(node_to_edit, "indentlevel") <- indentLevel + 1
+ target_cols = 1
+ }
+
+ for (j in target_cols) {
+ print(j)
+ node_to_edit <- xml_child(row_to_edit, j)
+ if (!xml_has_attr(node_to_edit, "indentlevel")) {
+ xml_attr(node_to_edit, "style") <- paste(
+ xml_attr(node_to_edit, "style"), "padding-left: ",paste0(level_of_indent*2,"em;")
+ )
+ xml_attr(node_to_edit, "indentlevel") <- 1
+ } else {
+ indentLevel <- as.numeric(xml_attr(node_to_edit, "indentlevel"))
+ xml_attr(node_to_edit, "style") <- sub(
+ paste0("padding-left: ", indentLevel * 2, "em;"),
+ paste0("padding-left: ", (indentLevel + 1) * 2, "em;"),
+ xml_attr(node_to_edit, "style")
+ )
+ xml_attr(node_to_edit, "indentlevel") <- indentLevel + 1
+ }
}
}
out <- as_kable_xml(kable_xml)