Add extra_css to spec family
diff --git a/R/cell_spec.R b/R/cell_spec.R
index 0774fcb..bc7af1e 100644
--- a/R/cell_spec.R
+++ b/R/cell_spec.R
@@ -32,6 +32,7 @@
#' bootstrap module manually. Read the package vignette to see how.
#' @param link A vector of strings for url links. Can be used together with
#' tooltip and popover.
+#' @param extra_css Extra css text to be passed into the cell
#' @param escape T/F value showing whether special characters should be escaped.
#' @param background_as_tile T/F value indicating if you want to have round
#' cornered tile as background in HTML.
@@ -46,6 +47,7 @@
color = NULL, background = NULL,
align = NULL, font_size = NULL, angle = NULL,
tooltip = NULL, popover = NULL, link = NULL,
+ extra_css = NULL,
escape = TRUE,
background_as_tile = TRUE,
latex_background_in_cell = TRUE) {
@@ -61,7 +63,7 @@
if (tolower(format) == "html") {
return(cell_spec_html(x, bold, italic, monospace,
color, background, align, font_size, angle,
- tooltip, popover, link,
+ tooltip, popover, link, extra_css,
escape, background_as_tile))
}
if (tolower(format) == "latex") {
@@ -73,7 +75,7 @@
cell_spec_html <- function(x, bold, italic, monospace,
color, background, align, font_size, angle,
- tooltip, popover, link,
+ tooltip, popover, link, extra_css,
escape, background_as_tile) {
if (escape) x <- escape_html(x)
cell_style <- NULL
@@ -92,6 +94,9 @@
"background-color: ", html_color(background), ";"
)
}
+ if (!is.null(extra_css)) {
+ cell_style <- paste0(cell_style, extra_css)
+ }
if (!is.null(align)) {
cell_style <- paste0(cell_style, "text-align: ", align, ";")
}
diff --git a/R/column_spec.R b/R/column_spec.R
index 55e01a1..a640a4d 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -23,6 +23,8 @@
#' @param border_right A logical variable indicating whether there should be a
#' border line on the right of the selected column. In HTML, you can also pass
#' in a character string for the CSS of the border line
+#' @param extra_css Extra css text to be passed into the cells of the row. Note
+#' that it's not for the whole column but to each individual cells
#'
#' @examples x <- knitr::kable(head(mtcars), "html")
#' column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE)
@@ -31,7 +33,8 @@
column_spec <- function(kable_input, column,
width = NULL, bold = FALSE, italic = FALSE,
monospace = FALSE, color = NULL, background = NULL,
- border_left = FALSE, border_right = FALSE) {
+ border_left = FALSE, border_right = FALSE,
+ extra_css = NULL) {
if (!is.numeric(column)) {
stop("column must be numeric. ")
}
@@ -44,7 +47,7 @@
return(column_spec_html(kable_input, column, width,
bold, italic, monospace,
color, background,
- border_left, border_right))
+ border_left, border_right, extra_css))
}
if (kable_format == "latex") {
return(column_spec_latex(kable_input, column, width,
@@ -57,7 +60,7 @@
column_spec_html <- function(kable_input, column, width,
bold, italic, monospace,
color, background,
- border_left, border_right) {
+ border_left, border_right, extra_css) {
kable_attrs <- attributes(kable_input)
kable_xml <- read_kable_as_xml(kable_input)
kable_tbody <- xml_tpart(kable_xml, "tbody")
@@ -118,6 +121,10 @@
xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
"border-right:", border_r_css, ";")
}
+ if (!is.null(extra_css)) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ extra_css)
+ }
}
}
diff --git a/R/row_spec.R b/R/row_spec.R
index 6ab31ec..d58493c 100644
--- a/R/row_spec.R
+++ b/R/row_spec.R
@@ -23,6 +23,8 @@
#' options including `xx-small`, `x-small`, `small`, `medium`, `large`,
#' `x-large`, `xx-large`, `smaller`, `larger`, `initial` and `inherit`.
#' @param angle 0-360, degree that the text will rotate.
+#' @param extra_css Extra css text to be passed into the cells of the row. Note
+#' that it's not for the whole row.
#'
#' @examples x <- knitr::kable(head(mtcars), "html")
#' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
@@ -31,7 +33,7 @@
row_spec <- function(kable_input, row,
bold = FALSE, italic = FALSE, monospace = FALSE,
color = NULL, background = NULL, align = NULL,
- font_size = NULL, angle = NULL) {
+ font_size = NULL, angle = NULL, extra_css) {
if (!is.numeric(row)) {
stop("row must be numeric. ")
}
@@ -42,7 +44,8 @@
}
if (kable_format == "html") {
return(row_spec_html(kable_input, row, bold, italic, monospace,
- color, background, align, font_size, angle))
+ color, background, align, font_size, angle,
+ extra_css))
}
if (kable_format == "latex") {
return(row_spec_latex(kable_input, row, bold, italic, monospace,
@@ -51,7 +54,8 @@
}
row_spec_html <- function(kable_input, row, bold, italic, monospace,
- color, background, align, font_size, angle) {
+ color, background, align, font_size, angle,
+ extra_css) {
kable_attrs <- attributes(kable_input)
kable_xml <- read_kable_as_xml(kable_input)
@@ -67,7 +71,7 @@
for (theader_i in 1:length(xml_children(original_header_row))) {
target_header_cell <- xml_child(original_header_row, theader_i)
xml_cell_style(target_header_cell, bold, italic, monospace, color, background,
- align, font_size, angle)
+ align, font_size, angle, extra_css)
}
row <- row[row != 0]
}
@@ -86,7 +90,7 @@
for (i in 1:length(xml_children(target_row))) {
target_cell <- xml_child(target_row, i)
xml_cell_style(target_cell, bold, italic, monospace, color, background,
- align, font_size, angle)
+ align, font_size, angle, extra_css)
}
}
}
@@ -97,7 +101,7 @@
}
xml_cell_style <- function(x, bold, italic, monospace, color, background,
- align, font_size, angle) {
+ align, font_size, angle, extra_css) {
if (bold) {
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
"font-weight: bold;")
@@ -136,6 +140,9 @@
"deg); transform: rotate(", angle,
"deg);")
}
+ if (!is.null(extra_css)) {
+ xml_attr(x, "style") <- paste0(xml_attr(x, "style"), extra_css)
+ }
return(x)
}
diff --git a/inst/NEWS.md b/inst/NEWS.md
index 5f6052f..48bb7b9 100644
--- a/inst/NEWS.md
+++ b/inst/NEWS.md
@@ -1,5 +1,8 @@
kableExtra 0.7.0
-* Add HTML color code support to kableExtra
+* Added HTML color code support to kableExtra
+* Fixed bug #105: bold/italic/monospace cannot accept T/F as vector
+* Added extra_css to cell_spec, row_spec & column_spec
+
kableExtra 0.6.1
--------------------------------------------------------------------------------
diff --git a/man/cell_spec.Rd b/man/cell_spec.Rd
index 40966ca..d50b580 100644
--- a/man/cell_spec.Rd
+++ b/man/cell_spec.Rd
@@ -8,7 +8,7 @@
cell_spec(x, format, bold = FALSE, italic = FALSE, monospace = FALSE,
color = NULL, background = NULL, align = NULL, font_size = NULL,
angle = NULL, tooltip = NULL, popover = NULL, link = NULL,
- escape = TRUE, background_as_tile = TRUE,
+ extra_css = NULL, escape = TRUE, background_as_tile = TRUE,
latex_background_in_cell = TRUE)
text_spec(x, format, bold = FALSE, italic = FALSE, monospace = FALSE,
@@ -61,6 +61,8 @@
\item{link}{A vector of strings for url links. Can be used together with
tooltip and popover.}
+\item{extra_css}{Extra css text to be passed into the cell}
+
\item{escape}{T/F value showing whether special characters should be escaped.}
\item{background_as_tile}{T/F value indicating if you want to have round
diff --git a/man/column_spec.Rd b/man/column_spec.Rd
index bbbd3a4..c3f0e35 100644
--- a/man/column_spec.Rd
+++ b/man/column_spec.Rd
@@ -6,7 +6,7 @@
\usage{
column_spec(kable_input, column, width = NULL, bold = FALSE,
italic = FALSE, monospace = FALSE, color = NULL, background = NULL,
- border_left = FALSE, border_right = FALSE)
+ border_left = FALSE, border_right = FALSE, extra_css = NULL)
}
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
@@ -38,6 +38,9 @@
\item{border_right}{A logical variable indicating whether there should be a
border line on the right of the selected column. In HTML, you can also pass
in a character string for the CSS of the border line}
+
+\item{extra_css}{Extra css text to be passed into the cells of the row. Note
+that it's not for the whole column but to each individual cells}
}
\description{
This function allows users to select a column and then specify
diff --git a/man/row_spec.Rd b/man/row_spec.Rd
index 6b037ea..0338b13 100644
--- a/man/row_spec.Rd
+++ b/man/row_spec.Rd
@@ -6,7 +6,7 @@
\usage{
row_spec(kable_input, row, bold = FALSE, italic = FALSE,
monospace = FALSE, color = NULL, background = NULL, align = NULL,
- font_size = NULL, angle = NULL)
+ font_size = NULL, angle = NULL, extra_css)
}
\arguments{
\item{kable_input}{Output of \code{knitr::kable()} with \code{format} specified}
@@ -38,6 +38,9 @@
\code{x-large}, \code{xx-large}, \code{smaller}, \code{larger}, \code{initial} and \code{inherit}.}
\item{angle}{0-360, degree that the text will rotate.}
+
+\item{extra_css}{Extra css text to be passed into the cells of the row. Note
+that it's not for the whole row.}
}
\description{
This function allows users to select a row and then specify
diff --git a/tests/visual_tests/cell_spec_html.Rmd b/tests/visual_tests/cell_spec_html.Rmd
index e56a260..233c13a 100644
--- a/tests/visual_tests/cell_spec_html.Rmd
+++ b/tests/visual_tests/cell_spec_html.Rmd
@@ -43,3 +43,18 @@
row_spec(0, angle = 270, align = "right") %>%
kable_styling(full_width = F)
```
+
+```{r}
+mtcars[1:10, 1:2] %>%
+ mutate(
+ car = row.names(.),
+ # You don't need format = "html" if you have ever defined options(knitr.table.format)
+ mpg = cell_spec(mpg, "html", bold = ifelse(mpg > 20, TRUE, FALSE)),
+ cyl = cell_spec(cyl, "html", color = "white", align = "c", angle = 45,
+ background = factor(cyl, c(4, 6, 8),
+ c("#666666", "#999999", "#BBBBBB")))
+ ) %>%
+ select(car, mpg, cyl) %>%
+ knitr::kable("html", escape = F) %>%
+ kable_styling("striped", full_width = F)
+```