Adding cell_spec
diff --git a/R/cell_spec.R b/R/cell_spec.R
new file mode 100644
index 0000000..f5bff2a
--- /dev/null
+++ b/R/cell_spec.R
@@ -0,0 +1,89 @@
+#' Specify Cell format
+#'
+#' @description Specify Cell format before it gets into kable
+#'
+#' @param x Things to be formated. It could be a vector of numbers or strings.
+#' @param format Either "html" or "latex". It can also be set through
+#' `option(knitr.table.format)`, same as `knitr::kable()`.
+#' @param bold A T/F value to control whether the text of the selected column
+#' need to be bolded.
+#' @param italic A T/F value to control whether the text of the selected column
+#' need to be emphasized.
+#' @param monospace A T/F value to control whether the text of the selected column
+#' need to be monospaced (verbatim)
+#' @param color A character string for column text color. Here please pay
+#' attention to the differences in color codes between HTML and LaTeX.
+#' @param background A character string for column background color. Here please
+#' pay attention to the differences in color codes between HTML and LaTeX.
+#' @param align A character string for cell alignment. For HTML, possible values could
+#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`
+#' while for LaTeX, you can only choose from `l`, `c` & `r`.
+#' @param font_size Only if you want to specify font size locally in HTML.
+#' This feature is not available in LaTeX
+#' @param angle 0-360, degree that the text will rotate.
+#'
+#' @export
+cell_spec <- function(x, format,
+ bold = F, italic = F, monospace = F,
+ color = NULL, background = NULL,
+ align = NULL, font_size = NULL, angle = NULL) {
+
+ if (missing(format) || is.null(format)) format = getOption('knitr.table.format')
+ if (is.null(format)) {
+ warning("Output format for cell_formatter was not specified. Using ",
+ "html as default. You may consider to set it up via option knitr.table.format.",
+ "See ?cell_formatter for details. ")
+ return(x)
+ }
+
+ if (tolower(format) == "html") {
+ return(cell_spec_html(x, bold, italic, monospace,
+ color, background, align, font_size, angle))
+ }
+ if (tolower(format) == "latex") {
+ return(cell_spec_latex(x, bold, italic, monospace,
+ color, background, align, angle))
+ }
+}
+
+cell_spec_html <- function(x, bold, italic, monospace,
+ color, background, align, font_size, angle) {
+ cell_style <- NULL
+ if (bold) cell_style <- paste(cell_style,"font-weight: bold;")
+ if (italic) cell_style <- paste(cell_style, "font-style: italic;")
+ if (monospace) cell_style <- paste(cell_style, "font-family: monospace;")
+ if (!is.null(color)) cell_style <- paste0(cell_style, " color: ", color, ";")
+ if (!is.null(background)) {
+ cell_style <- paste0(cell_style, " border-radius: 4px; padding-right: 2px",
+ "; background-color: ", background, ";")
+ }
+ if (!is.null(align)) {
+ cell_style <- paste0(cell_style, " text-align: ", align, ";")
+ }
+ if (!is.null(font_size)) {
+ cell_style <- paste0(cell_style, " font-size: ", font_size, "px;")
+ }
+ if (!is.null(angle)) {
+ cell_style <- paste0(cell_style,
+ " -webkit-transform: rotate(", angle,
+ "deg); -moz-transform: rotate(", angle,
+ "deg); -ms-transform: rotate(", angle,
+ "deg); -o-transform: rotate(", angle, "deg);")
+ }
+ out <- paste0(
+ '<div style="', cell_style, '">', x, '</div>'
+ )
+ return(out)
+}
+
+cell_spec_latex <- function(x, bold, italic, monospace,
+ color, background, align, angle) {
+ if (bold) x <- paste0("\\bfseries{", x, "}")
+ if (italic) x <-paste0("\\em{", x, "}")
+ if (monospace) x <- paste0("\\ttfamily{", x, "}")
+ if (!is.null(color)) x <- paste0("\\textcolor{", color, "}{", x, "}")
+ if (!is.null(background)) x <- paste0("\\cellcolor{", background, "}{", x, "}")
+ if (!is.null(align)) x <- paste0("\\multicolumn{1}{", align, "}{", x, "}")
+ if (!is.null(angle)) x <- paste0("\\rotatebox{", angle, "}{", x, "}")
+ return(x)
+}
diff --git a/R/row_spec.R b/R/row_spec.R
index 19c5d00..a6bdb59 100644
--- a/R/row_spec.R
+++ b/R/row_spec.R
@@ -19,8 +19,9 @@
#' @param align A character string for cell alignment. For HTML, possible values could
#' be `l`, `c`, `r` plus `left`, `center`, `right`, `justify`, `initial` and `inherit`
#' while for LaTeX, you can only choose from `l`, `c` & `r`.
-#' @param font_size Only if you want to specify font size locally in HTML. This feature
-#' is not available in LaTeX
+#' @param font_size Only if you want to specify font size locally in HTML.
+#' This feature is not available in LaTeX
+#' @param angle 0-360, degree that the text will rotate.
#'
#' @examples x <- knitr::kable(head(mtcars), "html")
#' row_spec(x, 1:2, bold = TRUE, italic = TRUE)
@@ -28,7 +29,8 @@
#' @export
row_spec <- function(kable_input, row,
bold = FALSE, italic = FALSE, monospace = FALSE,
- color = NULL, background = NULL, align = NULL, font_size = NULL) {
+ color = NULL, background = NULL, align = NULL,
+ font_size = NULL, angle = NULL) {
if (!is.numeric(row)) {
stop("row must be numeric. ")
}
@@ -39,16 +41,16 @@
}
if (kable_format == "html") {
return(row_spec_html(kable_input, row, bold, italic, monospace,
- color, background, align, font_size))
+ color, background, align, font_size, angle))
}
if (kable_format == "latex") {
return(row_spec_latex(kable_input, row, bold, italic, monospace,
- color, background, align))
+ color, background, align, angle))
}
}
row_spec_html <- function(kable_input, row, bold, italic, monospace,
- color, background, align, font_size) {
+ color, background, align, font_size, angle) {
kable_attrs <- attributes(kable_input)
kable_xml <- read_kable_as_xml(kable_input)
@@ -64,7 +66,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)
+ align, font_size, angle)
}
row <- row[row != 0]
}
@@ -83,7 +85,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)
+ align, font_size, angle)
}
}
}
@@ -93,27 +95,28 @@
return(out)
}
-xml_cell_style <- function(x, bold, italic, monospace, color, background, align, font_size) {
+xml_cell_style <- function(x, bold, italic, monospace, color, background,
+ align, font_size, angle) {
if (bold) {
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
- "font-weight: bold;")
+ "font-weight: bold;")
}
if (italic) {
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
- "font-style: italic;")
+ "font-style: italic;")
}
if (monospace) {
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
- "font-family: monospace;")
+ "font-family: monospace;")
}
if (!is.null(color)) {
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
- "color: ", color, ";")
+ "color: ", color, ";")
}
if (!is.null(background)) {
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
- "background-color: ",
- background, ";")
+ "background-color: ",
+ background, ";")
}
if (!is.null(align)) {
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
@@ -123,18 +126,26 @@
xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
"font-size: ", font_size, "px;")
}
+ if (!is.null(angle)) {
+ xml_attr(x, "style") <- paste0(xml_attr(x, "style"),
+ "-webkit-transform: rotate(", angle,
+ "deg); -moz-transform: rotate(", angle,
+ "deg); -ms-transform: rotate(", angle,
+ "deg); -o-transform: rotate(", angle,
+ "deg);")
+ }
return(x)
}
row_spec_latex <- function(kable_input, row, bold, italic, monospace,
- color, background, align) {
+ color, background, align, angle) {
table_info <- magic_mirror(kable_input)
out <- enc2utf8(as.character(kable_input))
row <- row + 1
for (i in row) {
target_row <- table_info$contents[i]
new_row <- latex_new_row_builder(target_row, bold, italic, monospace,
- color, background, align)
+ color, background, align, angle)
out <- sub(target_row, new_row, out, perl = T)
}
@@ -144,7 +155,7 @@
}
latex_new_row_builder <- function(target_row, bold, italic, monospace,
- color, background, align) {
+ color, background, align, angle) {
new_row <- latex_row_cells(target_row)
if (bold) {
new_row <- lapply(new_row, function(x) {
@@ -173,13 +184,18 @@
paste0("\\\\multicolumn{1}{", align, "}{", x, "}")
})
}
+
+ if (!is.null(angle)) {
+ new_row <- lapply(new_row, function(x) {
+ paste0("\\\\rotatebox{", angle, "}{", x, "}")
+ })
+ }
+
new_row <- paste(unlist(new_row), collapse = " & ")
if (!is.null(background)) {
new_row <- paste0("\\\\rowcolor{", background, "} ", new_row)
}
-
-
return(new_row)
}