option to add_indent spacing to any column
diff --git a/R/add_indent.R b/R/add_indent.R
index 3f57862..098989c 100644
--- a/R/add_indent.R
+++ b/R/add_indent.R
@@ -5,6 +5,7 @@
#' be indented.
#' @param level_of_indent a numeric value for the indent level. Default is 1.
#' @param all_cols T/F whether to apply indentation to all columns
+#' @param target_cols A vector of numeric column positions. Default is 1.
#'
#' @examples
#' \dontrun{
@@ -15,7 +16,8 @@
#'
#' @export
add_indent <- function(kable_input, positions,
- level_of_indent = 1, all_cols = FALSE) {
+ level_of_indent = 1, all_cols = FALSE,
+ target_cols = 1) {
if (!is.numeric(positions)) {
stop("Positions can only take numeric row numbers (excluding header rows).")
@@ -28,16 +30,17 @@
return(kable_input)
}
if (kable_format == "html") {
- return(add_indent_html(kable_input, positions, level_of_indent, all_cols))
+ return(add_indent_html(kable_input, positions, level_of_indent, all_cols, target_cols))
}
if (kable_format == "latex") {
- return(add_indent_latex(kable_input, positions, level_of_indent, all_cols))
+ return(add_indent_latex(kable_input, positions, level_of_indent, all_cols, target_cols))
}
}
# Add indentation for LaTeX
add_indent_latex <- function(kable_input, positions,
- level_of_indent = 1, all_cols = FALSE) {
+ level_of_indent = 1, all_cols = FALSE,
+ target_cols = 1) {
table_info <- magic_mirror(kable_input)
out <- solve_enc(kable_input)
level_of_indent<-as.numeric(level_of_indent)
@@ -57,18 +60,22 @@
for (i in positions + table_info$position_offset) {
rowtext <- table_info$contents[i]
+ new_rowtext <- unlist(latex_row_cells(rowtext))
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)
+ if (all(target_cols %in% seq_along(new_rowtext))) {
+ new_rowtext[target_cols] <- latex_indent_unit(new_rowtext[target_cols], level_of_indent)
+ } else {
+ stop("There aren't that many columns in the row. Check target_cols in ",
+ "add_indent_latex.")
+ }
}
- out <- sub(paste0(rowtext, "(\\\\\\\\\\*?(\\[.*\\])?\n)"),
- paste0(new_rowtext, "\\1"),
- out, perl = TRUE)
+ new_rowtext <- paste(unlist(new_rowtext), collapse = " & ")
+ out <- sub(rowtext, new_rowtext, out, perl = TRUE)
table_info$contents[i] <- new_rowtext
}
out <- structure(out, format = "latex", class = "knitr_kable")
@@ -86,7 +93,8 @@
# Add indentation for HTML
add_indent_html <- function(kable_input, positions,
- level_of_indent = 1, all_cols = FALSE) {
+ level_of_indent = 1, all_cols = FALSE,
+ target_cols = 1) {
kable_attrs <- attributes(kable_input)
kable_xml <- kable_as_xml(kable_input)
@@ -102,8 +110,6 @@
row_to_edit = xml_children(kable_tbody)[[i]]
if (all_cols) {
target_cols = 1:xml2::xml_length(row_to_edit)
- } else {
- target_cols = 1
}
for (j in target_cols) {