html row_spec
diff --git a/NAMESPACE b/NAMESPACE
index 7d42cb5..a572229 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -10,6 +10,7 @@
export(landscape)
export(magic_mirror)
export(rmd_format)
+export(row_spec)
export(usepackage_latex)
importFrom(knitr,knit_meta_add)
importFrom(magrittr,"%>%")
diff --git a/R/row_spec.R b/R/row_spec.R
new file mode 100644
index 0000000..a276b33
--- /dev/null
+++ b/R/row_spec.R
@@ -0,0 +1,89 @@
+#' Specify the look of the selected row
+#'
+#' @description This function allows users to select a row and then specify
+#' its look. Right now it supports the following two properties: bold text and
+#' italic text.
+#'
+#' @param kable_input Output of `knitr::kable()` with `format` specified
+#' @param row A numeric value indicating which row to be selected
+#' @param bold A T/F value to control whether the text of the selected row
+#' need to be bolded.
+#' @param italic A T/F value to control whether the text of the selected row
+#' need to be emphasized.
+#'
+#' @examples x <- knitr::kable(head(mtcars), "html")
+#' row_spec(x, 1, bold = TRUE, italic = TRUE)
+#'
+#' @export
+row_spec <- function(kable_input, row,
+ bold = FALSE, italic = FALSE) {
+ if (!is.numeric(row)) {
+ stop("row must be a numeric value")
+ }
+ kable_format <- attr(kable_input, "format")
+ if (!kable_format %in% c("html", "latex")) {
+ message("Currently generic markdown table using pandoc is not supported.")
+ return(kable_input)
+ }
+ if (kable_format == "html") {
+ return(row_spec_html(kable_input, row, bold, italic))
+ }
+ if (kable_format == "latex") {
+ message("The LaTeX version of row_spec has not yet been implemented. ")
+ return(kable_input)
+ }
+}
+
+row_spec_html <- function(kable_input, row, bold, italic) {
+ kable_attrs <- attributes(kable_input)
+ kable_xml <- read_xml(as.character(kable_input), options = "COMPACT")
+ kable_tbody <- xml_tpart(kable_xml, "tbody")
+
+ group_header_rows <- attr(kable_input, "group_header_rows")
+ if (!is.null(group_header_rows)) {
+ row <- positions_corrector(row, group_header_rows,
+ length(xml_children(kable_tbody)))
+ }
+
+ target_row <- xml_child(kable_tbody, row)
+
+ for (i in 1:length(xml_children(target_row))) {
+ target_cell <- xml_child(target_row, i)
+ if (bold) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ "font-weight: bold;")
+ }
+ if (italic) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ "font-style: italic;")
+ }
+ }
+ out <- structure(as.character(kable_xml), format = "html",
+ class = "knitr_kable")
+ attributes(out) <- kable_attrs
+ return(out)
+}
+
+# row_spec_latex <- function(kable_input, row, bold, italic) {
+# table_info <- magic_mirror(kable_input)
+# align_collapse <- ifelse(table_info$booktabs, "", "\\|")
+# kable_align_old <- paste(table_info$align_vector, collapse = align_collapse)
+#
+# if (bold | italic) {
+# usepackage_latex("array")
+# latex_array_options <- c("\\\\bfseries", "\\\\em")[c(bold, italic)]
+# latex_array_options <- paste0(
+# "\\>\\{", paste(latex_array_options, collapse = ""), "\\}"
+# )
+# table_info$align_vector[row] <- paste0(latex_array_options,
+# table_info$align_vector[row])
+# }
+#
+# kable_align_new <- paste(table_info$align_vector, collapse = align_collapse)
+#
+# out <- sub(kable_align_old, kable_align_new, as.character(kable_input),
+# perl = T)
+# out <- structure(out, format = "latex", class = "knitr_kable")
+# attr(out, "original_kable_meta") <- table_info
+# return(out)
+# }
diff --git a/man/row_spec.Rd b/man/row_spec.Rd
new file mode 100644
index 0000000..169c05a
--- /dev/null
+++ b/man/row_spec.Rd
@@ -0,0 +1,29 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/row_spec.R
+\name{row_spec}
+\alias{row_spec}
+\title{Specify the look of the selected row}
+\usage{
+row_spec(kable_input, row, bold = FALSE, italic = FALSE)
+}
+\arguments{
+\item{kable_input}{Output of `knitr::kable()` with `format` specified}
+
+\item{row}{A numeric value indicating which row to be selected}
+
+\item{bold}{A T/F value to control whether the text of the selected row
+need to be bolded.}
+
+\item{italic}{A T/F value to control whether the text of the selected row
+need to be emphasized.}
+}
+\description{
+This function allows users to select a row and then specify
+its look. Right now it supports the following two properties: bold text and
+italic text.
+}
+\examples{
+x <- knitr::kable(head(mtcars), "html")
+row_spec(x, 1, bold = TRUE, italic = TRUE)
+
+}
diff --git a/tests/visual_tests/column_width_html.Rmd b/tests/visual_tests/column_row_spec_html.Rmd
similarity index 94%
rename from tests/visual_tests/column_width_html.Rmd
rename to tests/visual_tests/column_row_spec_html.Rmd
index 3360c47..a54ea5f 100644
--- a/tests/visual_tests/column_width_html.Rmd
+++ b/tests/visual_tests/column_row_spec_html.Rmd
@@ -38,8 +38,9 @@
)
kable(dt, "html") %>%
- kable_styling() %>%
+ kable_styling(full_width = F) %>%
column_spec(2, "5cm", bold = T) %>%
- column_spec(4, "3cm", italic = T)
+ column_spec(4, "3cm", italic = T) %>%
+ row_spec(1, italic = T)
```