add text & background color option in column_spec & row_spec as #43
diff --git a/R/column_spec.R b/R/column_spec.R
index 8381899..e13e685 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -23,7 +23,7 @@
#' @export
column_spec <- function(kable_input, column,
width = NULL, bold = FALSE, italic = FALSE,
- monospace = FALSE) {
+ monospace = FALSE, color = NULL, background = NULL) {
if (!is.numeric(column)) {
stop("column must be a numeric value")
}
@@ -33,14 +33,20 @@
return(kable_input)
}
if (kable_format == "html") {
- return(column_spec_html(kable_input, column, width, bold, italic, monospace))
+ return(column_spec_html(kable_input, column, width,
+ bold, italic, monospace,
+ color, background))
}
if (kable_format == "latex") {
- return(column_spec_latex(kable_input, column, width, bold, italic, monospace))
+ return(column_spec_latex(kable_input, column, width,
+ bold, italic, monospace,
+ color, background))
}
}
-column_spec_html <- function(kable_input, column, width, bold, italic, monospace) {
+column_spec_html <- function(kable_input, column, width,
+ bold, italic, monospace,
+ color, background) {
kable_attrs <- attributes(kable_input)
kable_xml <- read_kable_as_xml(kable_input)
kable_tbody <- xml_tpart(kable_xml, "tbody")
@@ -79,6 +85,15 @@
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
diff --git a/R/row_spec.R b/R/row_spec.R
index 55a8bed..6615365 100644
--- a/R/row_spec.R
+++ b/R/row_spec.R
@@ -19,7 +19,8 @@
#'
#' @export
row_spec <- function(kable_input, row,
- bold = FALSE, italic = FALSE, monospace = FALSE) {
+ bold = FALSE, italic = FALSE, monospace = FALSE,
+ color = NULL, background = NULL) {
if (!is.numeric(row)) {
stop("row must be a numeric value")
}
@@ -29,14 +30,17 @@
return(kable_input)
}
if (kable_format == "html") {
- return(row_spec_html(kable_input, row, bold, italic, monospace))
+ return(row_spec_html(kable_input, row, bold, italic, monospace,
+ color, background))
}
if (kable_format == "latex") {
- return(row_spec_latex(kable_input, row, bold, italic, monospace))
+ return(row_spec_latex(kable_input, row, bold, italic, monospace,
+ color, background))
}
}
-row_spec_html <- function(kable_input, row, bold, italic, monospace) {
+row_spec_html <- function(kable_input, row, bold, italic, monospace,
+ color, background) {
kable_attrs <- attributes(kable_input)
kable_xml <- read_kable_as_xml(kable_input)
kable_tbody <- xml_tpart(kable_xml, "tbody")
@@ -63,6 +67,15 @@
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
diff --git a/tests/visual_tests/column_row_spec_html.Rmd b/tests/visual_tests/column_row_spec_html.Rmd
index 7bbe229..d63ab49 100644
--- a/tests/visual_tests/column_row_spec_html.Rmd
+++ b/tests/visual_tests/column_row_spec_html.Rmd
@@ -40,7 +40,7 @@
kable(dt, "html") %>%
kable_styling(full_width = F) %>%
row_spec(1, bold = T) %>%
- column_spec(2, "5cm", bold = T) %>%
+ column_spec(2, "5cm", bold = T, color = "white", background = "red") %>%
column_spec(3, monospace = T) %>%
column_spec(4, "3cm", italic = T)
```
@@ -54,5 +54,5 @@
kable(dt, "html") %>%
kable_styling(full_width = F) %>%
column_spec(7, bold = T) %>%
- row_spec(5, bold = T, italic = T)
+ row_spec(5, bold = T, italic = T, color = "white", background = "black")
```