Create remove_column.R
Added a function to remove a column after the kable is made
diff --git a/R/remove_column.R b/R/remove_column.R
new file mode 100644
index 0000000..041cc18
--- /dev/null
+++ b/R/remove_column.R
@@ -0,0 +1,59 @@
+#' Remove columns
+#'
+#' @param kable_input Output of [knitr::kable()] with format specified
+#' @param columns A numeric value or vector indicating in which column(s) rows need to be removed
+#'
+#' @export
+#'
+#' @examples
+#' mtcars %>%
+#' kable() %>%
+#' remove_column(2:3)
+function (kable_input, columns) {
+ kable_format <- attr(kable_input, "format")
+ if (!kable_format %in% c("html", "latex")) {
+ warning("Please specify format in kable. kableExtra can customize either ",
+ "HTML or LaTeX outputs. See https://haozhu233.github.io/kableExtra/ ",
+ "for details.")
+ return(kable_input)
+ }
+ if (kable_format == "html") {
+ return(remove_column_html(kable_input, columns))
+ } else if (kable_format == "latex") {
+ stop("Removing columns was not implemented for latex kables yet")
+ }
+}
+
+remove_column_html <- function (kable_input, columns) {
+ kable_attrs <- attributes(kable_input)
+ kable_xml <- kable_as_xml(kable_input)
+ kable_tbody <- xml_tpart(kable_xml, "tbody")
+ kable_thead <- xml_tpart(kable_xml, "thead")
+
+ cell_topleft <- xml2::xml_child(kable_thead, 1) %>%
+ xml2::xml_child(1) %>%
+ xml2::xml_text() %>%
+ stringr::str_trim()
+ has_rownames <- cell_topleft==""
+ if(has_rownames) columns <- columns+1
+
+ body_rows <- xml2::xml_length(kable_tbody)
+ for(i in 1:body_rows){
+ for(j in columns){
+ target_row <- xml2::xml_child(kable_tbody, i)
+ target_cell <- xml2::xml_child(target_row, j)
+ xml2::xml_remove(target_cell)
+ }
+ }
+ target_row_head <- xml2::xml_child(kable_thead, 1)
+ for(j in columns){
+ target_cell_head <- xml2::xml_child(target_row_head, j)
+ xml2::xml_remove(target_cell_head)
+ }
+ out <- as_kable_xml(kable_xml)
+ attributes(out) <- kable_attrs
+ if (!"kableExtra" %in% class(out))
+ class(out) <- c("kableExtra", class(out))
+
+ return(out)
+}