Version 1.1 release
diff --git a/R/add_header_above.R b/R/add_header_above.R
index 0d90117..1b73229 100644
--- a/R/add_header_above.R
+++ b/R/add_header_above.R
@@ -149,13 +149,13 @@
ifelse(strikeout, "text-decoration: line-through; ", "")
)
if (!is.null(color)) {
- row_style <- paste0(row_style, "color: ", html_color(color), ";")
+ row_style <- paste0(row_style, "color: ", html_color(color), " !important;")
}
if (!is.null(background)) {
row_style <- paste0(
row_style,
"padding-right: 4px; padding-left: 4px; ",
- "background-color: ", html_color(background), ";"
+ "background-color: ", html_color(background), " !important;"
)
}
if (!is.null(font_size)) {
diff --git a/R/cell_spec.R b/R/cell_spec.R
index 498cae7..faaf85b 100644
--- a/R/cell_spec.R
+++ b/R/cell_spec.R
@@ -93,14 +93,15 @@
cell_style <- paste(cell_style,
ifelse(strikeout, "text-decoration: line-through;", ""))
if (!is.null(color)) {
- cell_style <- paste0(cell_style, "color: ", html_color(color), ";")
+ cell_style <- paste0(cell_style, "color: ", html_color(color),
+ " !important;")
}
if (!is.null(background)) {
cell_style <- paste0(
cell_style,
ifelse(background_as_tile, "border-radius: 4px; ", ""),
"padding-right: 4px; padding-left: 4px; ",
- "background-color: ", html_color(background), ";"
+ "background-color: ", html_color(background), " !important;"
)
}
if (!is.null(extra_css)) {
diff --git a/R/column_spec.R b/R/column_spec.R
index 693bc80..7d0cefc 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -185,12 +185,14 @@
}
if (!is.null(color)) {
xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
- "color: ", color, ";")
+ "color: ", html_color(color),
+ " !important;")
}
if (!is.null(background)) {
xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
"background-color: ",
- background, ";")
+ html_color(background),
+ " !important;")
}
if (border_left) {
xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
diff --git a/R/footnote.R b/R/footnote.R
index 1c9dcac..01f0c57 100644
--- a/R/footnote.R
+++ b/R/footnote.R
@@ -26,7 +26,7 @@
#' long paragraph of footnotes.
#' @param fixed_small_size T/F When you want to keep the footnote small after
#' specifying large font size with the kable_styling() (e.g. ideal font for headers
-#' and table content with small font in footnotes).
+#' and table content with small font in footnotes).
#' @param general_title Section header for general footnotes. Default is
#' "Note: ".
#' @param number_title Section header for number footnotes. Default is "".
@@ -111,7 +111,7 @@
}
if (kable_format == "latex") {
return(footnote_latex(kable_input, footnote_table, footnote_as_chunk,
- threeparttable))
+ threeparttable, fixed_small_size))
}
}
@@ -245,7 +245,7 @@
# LaTeX
footnote_latex <- function(kable_input, footnote_table, footnote_as_chunk,
- threeparttable) {
+ threeparttable, fixed_small_size) {
table_info <- magic_mirror(kable_input)
out <- solve_enc(kable_input)
diff --git a/R/group_rows.R b/R/group_rows.R
index 5017159..e913497 100644
--- a/R/group_rows.R
+++ b/R/group_rows.R
@@ -36,7 +36,7 @@
#'
#' @examples x <- knitr::kable(head(mtcars), "html")
#' # Put Row 2 to Row 5 into a Group and label it as "Group A"
-#' group_rows(x, "Group A", 2, 5)
+#' pack_rows(x, "Group A", 2, 5)
#'
#' @export
group_rows <- function(kable_input, group_label = NULL,
@@ -220,3 +220,7 @@
names(index) <- x_rle$values
return(index)
}
+
+#' @rdname group_rows
+#' @export
+pack_rows <- group_rows
diff --git a/R/kable_styling.R b/R/kable_styling.R
index 2045c15..a7546f1 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -58,6 +58,8 @@
#' a plain no-caption table in a `table` environment in order to center the
#' table. You can specify this option to things like `table*` or `float*` based
#' on your need.
+#' @param fixed_thead HTML table option so table header row is fixed at top.
+#' Values can be either T/F or `list(enabled = T/F, background = "anycolor")`.
#'
#' @details For LaTeX, if you use other than English environment
#' - all tables are converted to 'UTF-8'. If you use, for example, Hungarian
@@ -91,7 +93,8 @@
stripe_index = NULL,
latex_table_env = NULL,
protect_latex = TRUE,
- table.envir = "table") {
+ table.envir = "table",
+ fixed_thead = FALSE) {
if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
@@ -125,7 +128,8 @@
full_width = full_width,
position = position,
font_size = font_size,
- protect_latex = protect_latex))
+ protect_latex = protect_latex,
+ fixed_thead = fixed_thead))
}
if (kable_format == "latex") {
if (is.null(full_width)) {
@@ -182,7 +186,8 @@
position = c("center", "left", "right",
"float_left", "float_right"),
font_size = NULL,
- protect_latex = TRUE) {
+ protect_latex = TRUE,
+ fixed_thead = FALSE) {
if (protect_latex) {
kable_input <- extract_latex_from_kable(kable_input)
}
@@ -242,6 +247,19 @@
xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
}
+ fixed_thead <- get_fixed_thead(fixed_thead)
+ if (fixed_thead$enabled) {
+ all_header_cells <- xml2::xml_find_all(kable_xml, "//thead//th")
+ if (is.null(fixed_thead$background)) fixed_thead$background <- "#FFFFFF"
+ for (i in seq(length(all_header_cells))) {
+ xml_attr(all_header_cells[i], "style") <- paste0(
+ xml_attr(all_header_cells[i], "style"),
+ "position: sticky; top:0; background-color: ",
+ fixed_thead$background, ";"
+ )
+ }
+ }
+
out <- as_kable_xml(kable_xml)
if (protect_latex) {
out <- replace_latex_in_kable(out, kable_attrs$extracted_latex)
@@ -347,9 +365,9 @@
styling_latex_striped <- function(x, table_info, color, stripe_index) {
if (is.null(stripe_index)) {
- striped_index <- seq(1, table_info$nrow - table_info$position_offset, 2)
+ stripe_index <- seq(1, table_info$nrow - table_info$position_offset, 2)
}
- row_spec(x, striped_index, background = color)
+ row_spec(x, stripe_index, background = color)
}
styling_latex_hold_position <- function(x) {
diff --git a/R/row_spec.R b/R/row_spec.R
index 7bfb68e..ff95c0e 100644
--- a/R/row_spec.R
+++ b/R/row_spec.R
@@ -147,12 +147,12 @@
}
if (!is.null(color)) {
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
- "color: ", color, ";")
+ "color: ", html_color(color), " !important;")
}
if (!is.null(background)) {
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
"background-color: ",
- background, ";")
+ html_color(background), " !important;")
}
if (!is.null(align)) {
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
diff --git a/R/scroll_box.R b/R/scroll_box.R
index b0660ed..39405cf 100644
--- a/R/scroll_box.R
+++ b/R/scroll_box.R
@@ -8,8 +8,8 @@
#' @param width A character string indicating the width of the box, e.g. "100px"
#' @param box_css CSS text for the box
#' @param extra_css Extra CSS styles
-#' @param fixed_thead A list of two named element. enabled and background.
-#' Default is F and white, e.g. "list(enabled = T, background = "#fff")"
+#' @param fixed_thead HTML table option so table header row is fixed at top.
+#' Values can be either T/F or `list(enabled = T/F, background = "anycolor")`.
#'
#' @export
#'
@@ -25,23 +25,27 @@
#' kable_styling() %>%
#' scroll_box(width = "100%", height = "200px")
#' }
-
scroll_box <- function(kable_input, height = NULL, width = NULL,
box_css = "border: 1px solid #ddd; padding: 5px; ",
- extra_css = NULL, fixed_thead = list(enabled = F, background = "#fff")) {
+ extra_css = NULL,
+ fixed_thead = TRUE
+ ) {
kable_attrs <- attributes(kable_input)
+ fixed_thead <- get_fixed_thead(fixed_thead)
+ if (is.null(height)) fixed_thead$enabled <- FALSE
if (fixed_thead$enabled) {
box_css = "border: 1px solid #ddd; padding: 0px; "
kable_xml <- read_kable_as_xml(kable_input)
- kable_thead <- xml_tpart(kable_xml, "thead")
- original_header_row <- xml_child(kable_thead, length(xml_children(kable_thead)))
- for (theader_i in 1:length(xml_children(original_header_row))) {
- target_header_cell <- xml_child(original_header_row, theader_i)
- xml_attr(target_header_cell, "style") <- paste0(xml_attr(target_header_cell, "style"),
- "position: sticky; top:0; background: ",
- fixed_thead$background,";")
+ all_header_cells <- xml2::xml_find_all(kable_xml, "//thead//th")
+ if (is.null(fixed_thead$background)) fixed_thead$background <- "#FFFFFF"
+ for (i in seq(length(all_header_cells))) {
+ xml_attr(all_header_cells[i], "style") <- paste0(
+ xml_attr(all_header_cells[i], "style"),
+ "position: sticky; top:0; background-color: ",
+ fixed_thead$background, ";"
+ )
}
out <- as.character(as_kable_xml(kable_xml))
} else {
@@ -70,3 +74,11 @@
return(out)
}
+
+get_fixed_thead <- function(x) {
+ if (is.logical(x)) {
+ if (x) return(list(enabled = TRUE, background = "#FFFFFF"))
+ return(list(enabled = FALSE))
+ }
+ return(x)
+}
diff --git a/R/spec_tools.R b/R/spec_tools.R
index 76fb7dd..0ae4032 100644
--- a/R/spec_tools.R
+++ b/R/spec_tools.R
@@ -29,7 +29,7 @@
}
html_color <- function(colors) {
- colors <- as.character(colors)
+ colors <- trimws(gsub("\\!important", "", as.character(colors)))
sapply(colors, html_color_)
}
diff --git a/R/zzz.R b/R/zzz.R
index 2014072..dfd67f8 100644
--- a/R/zzz.R
+++ b/R/zzz.R
@@ -1,26 +1,29 @@
.onLoad <- function(libname = find.package("kableExtra"), pkgname = "kableExtra") {
- load_packages <- getOption("kableExtra.latex.load_packages", default = TRUE)
- if (load_packages) {
- usepackage_latex("booktabs")
- usepackage_latex("longtable")
- usepackage_latex("array")
- usepackage_latex("multirow")
- usepackage_latex("wrapfig")
- usepackage_latex("float")
- usepackage_latex("colortbl")
- usepackage_latex("pdflscape")
- usepackage_latex("tabu")
- usepackage_latex("threeparttable")
- usepackage_latex("threeparttablex")
- usepackage_latex("ulem", "normalem")
- usepackage_latex("makecell")
- usepackage_latex("xcolor")
+ if (knitr::is_latex_output()) {
+ load_packages <- getOption("kableExtra.latex.load_packages", default = TRUE)
+ if (load_packages) {
+ usepackage_latex("booktabs")
+ usepackage_latex("longtable")
+ usepackage_latex("array")
+ usepackage_latex("multirow")
+ usepackage_latex("wrapfig")
+ usepackage_latex("float")
+ usepackage_latex("colortbl")
+ usepackage_latex("pdflscape")
+ usepackage_latex("tabu")
+ usepackage_latex("threeparttable")
+ usepackage_latex("threeparttablex")
+ usepackage_latex("ulem", "normalem")
+ usepackage_latex("makecell")
+ usepackage_latex("xcolor")
+ }
}
auto_format <- getOption("kableExtra.auto_format", default = TRUE)
if (auto_format) auto_set_format()
if (!is.null(rmarkdown::metadata$output) &&
rmarkdown::metadata$output %in% c(
- "ioslides_presentation", "slidy_presentation"
+ "ioslides_presentation", "slidy_presentation",
+ "gitbook", "radix_article"
)) {
options(kableExtra.html.bsTable = TRUE)
}