Added column_spec function for HTML and LaTeX
diff --git a/NAMESPACE b/NAMESPACE
index 37f140c..ca49b9d 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -4,6 +4,7 @@
export(add_footnote)
export(add_header_above)
export(add_indent)
+export(column_spec)
export(group_rows)
export(kable_styling)
export(landscape)
diff --git a/R/column_spec.R b/R/column_spec.R
index e69de29..bc8ef7e 100644
--- a/R/column_spec.R
+++ b/R/column_spec.R
@@ -0,0 +1,92 @@
+#' Specify the look of the selected column
+#'
+#' @description This function allows users to select a column and then specify
+#' its look. Right now it supports the following three properties: column width,
+#' bold text and italic text.
+#'
+#' @param kable_input Output of `knitr::kable()` with `format` specified
+#' @param column A numeric value indicating which column to be selected
+#' @param width A character string telling HTML & LaTeX how wide the column
+#' needs to be, e.g. "10cm", "3in" or "30em".
+#' @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.
+#' @export
+column_spec <- function(kable_input, column, width, bold = F, italic = F) {
+ if (!is.numeric(column)) {
+ stop("column 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(column_spec_html(kable_input, column, width, bold, italic))
+ }
+ if (kable_format == "latex") {
+ return(column_spec_latex(kable_input, column, width, bold, italic))
+ }
+}
+
+column_spec_html <- function(kable_input, column, width, 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")
+ all_contents_rows <- seq(1, length(kable_tbody))
+ if (!is.null(group_header_rows)) {
+ all_contents_rows <- all_contents_rows[!all_contents_rows %in%
+ group_header_rows]
+ }
+
+ for (i in all_contents_rows) {
+ target_cell <- xml_child(xml_child(kable_tbody, i), column)
+ if (!is.null(width)) {
+ xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, "style"),
+ "width: ", width, "; ")
+ }
+ 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)
+}
+
+column_spec_latex <- function(kable_input, column, width, 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 (!is.null(width)) {
+ table_info$align_vector[column] <- paste0("p\\{", width, "\\}")
+ }
+
+ 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[column] <- paste0(latex_array_options,
+ table_info$align_vector[column])
+ }
+
+ 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/R/group_rows.R b/R/group_rows.R
index cb0190f..c768386 100644
--- a/R/group_rows.R
+++ b/R/group_rows.R
@@ -86,12 +86,6 @@
"}}\\\\\\\\\n",
rowtext
)
- # last_row <- paste0(table_info$contents[end_row + 1], "\\\\\\\\")
- # out <- sub(
- # last_row,
- # paste0(last_row, "\n\\\\addlinespace[", gap_space, "]"),
- # out
- # )
} else {
rowtext <- paste0("\\\\hline\n", rowtext)
new_rowtext <- paste0(
diff --git a/R/kable_styling.R b/R/kable_styling.R
index 05ede9c..c04029a 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -169,14 +169,6 @@
out <- NULL
out <- as.character(kable_input)
table_info <- magic_mirror(kable_input)
- table_info$valign2 <- sub("\\[", "\\\\[", table_info$valign)
- table_info$valign2 <- sub("\\]", "\\\\]", table_info$valign2)
- table_info$valign3 <- sub("\\[", "", table_info$valign)
- table_info$valign3 <- sub("\\]", "", table_info$valign3)
- table_info$begin_tabular <- paste0("\\\\begin\\{", table_info$tabular, "\\}",
- table_info$valign2)
- table_info$end_tabular <- paste0("\\\\end\\{", table_info$tabular, "\\}")
-
if ("striped" %in% latex_options) {
out <- styling_latex_striped(out)
diff --git a/R/magic_mirror.R b/R/magic_mirror.R
index 2283a47..088ee90 100644
--- a/R/magic_mirror.R
+++ b/R/magic_mirror.R
@@ -39,10 +39,19 @@
kable_info$booktabs <- grepl("\\\\toprule", kable_input)
# Align
kable_info$align <- gsub("\\|", "", str_match(
- kable_input, paste0("\\\\begin\\{", kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2])
+ kable_input, paste0("\\\\begin\\{",
+ kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2])
+ kable_info$align_vector <- unlist(strsplit(kable_info$align, ""))
# valign
kable_info$valign <- gsub("\\|", "", str_match(
kable_input, paste0("\\\\begin\\{", kable_info$tabular,"\\}(.*)\\{.*?\\}"))[2])
+ kable_info$valign2 <- sub("\\[", "\\\\[", kable_info$valign)
+ kable_info$valign2 <- sub("\\]", "\\\\]", kable_info$valign2)
+ kable_info$valign3 <- sub("\\[", "", kable_info$valign)
+ kable_info$valign3 <- sub("\\]", "", kable_info$valign3)
+ kable_info$begin_tabular <- paste0("\\\\begin\\{", kable_info$tabular, "\\}",
+ kable_info$valign2)
+ kable_info$end_tabular <- paste0("\\\\end\\{", kable_info$tabular, "\\}")
# N of columns
kable_info$ncol <- nchar(kable_info$align)
# Caption
diff --git a/docs/awesome_table_in_pdf.Rmd b/docs/awesome_table_in_pdf.Rmd
index fc47b0d..8412e46 100644
--- a/docs/awesome_table_in_pdf.Rmd
+++ b/docs/awesome_table_in_pdf.Rmd
@@ -171,7 +171,7 @@
Sometimes when we have a wide table, we want it to sit on a designated landscape page. The new function `landscape()` can help you on that. Unlike other functions, this little function only serves LaTeX and doesn't have a HTML side.
```{r}
kable(dt, caption = "Demo Table (Landscape)[note]", booktabs = T) %>%
- kable_styling(latex_options = "hold_position") %>%
+ kable_styling(latex_options = c("hold_position", "scale_down")) %>%
add_header_above(c(" ", "Group 1[note]" = 3, "Group 2[note]" = 3)) %>%
add_footnote(c("This table is from mtcars",
"Group 1 contains mpg, cyl and disp",
diff --git a/docs/awesome_table_in_pdf.pdf b/docs/awesome_table_in_pdf.pdf
index b378f65..5e1cd46 100644
--- a/docs/awesome_table_in_pdf.pdf
+++ b/docs/awesome_table_in_pdf.pdf
Binary files differ
diff --git a/inst/NEWS b/inst/NEWS
index 6a418aa..4c6ed76 100644
--- a/inst/NEWS
+++ b/inst/NEWS
@@ -1,3 +1,10 @@
+kableExtra 0.2.1
+--------------------------------------------------------------------------------
+
+* Added `column_spec()` to customize the look of the selected column.
+
+* Improve language in docs.
+
kableExtra 0.2.0
--------------------------------------------------------------------------------
diff --git a/man/column_spec.Rd b/man/column_spec.Rd
new file mode 100644
index 0000000..ecfb48a
--- /dev/null
+++ b/man/column_spec.Rd
@@ -0,0 +1,27 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/column_spec.R
+\name{column_spec}
+\alias{column_spec}
+\title{Specify the look of the selected column}
+\usage{
+column_spec(kable_input, column, width, bold = F, italic = F)
+}
+\arguments{
+\item{kable_input}{Output of `knitr::kable()` with `format` specified}
+
+\item{column}{A numeric value indicating which column to be selected}
+
+\item{width}{A character string telling HTML & LaTeX how wide the column
+needs to be, e.g. "10cm", "3in" or "30em".}
+
+\item{bold}{A T/F value to control whether the text of the selected column
+need to be bolded.}
+
+\item{italic}{A T/F value to control whether the text of the selected column
+need to be emphasized.}
+}
+\description{
+This function allows users to select a column and then specify
+its look. Right now it supports the following three properties: column width,
+bold text and italic text.
+}
diff --git a/tests/visual_tests/column_width_html.Rmd b/tests/visual_tests/column_width_html.Rmd
new file mode 100644
index 0000000..54f5ab7
--- /dev/null
+++ b/tests/visual_tests/column_width_html.Rmd
@@ -0,0 +1,45 @@
+---
+title: "Untitled"
+output: html_document
+---
+
+```{r setup, include=FALSE}
+knitr::opts_chunk$set(echo = TRUE)
+```
+
+## R Markdown
+
+This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
+
+When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
+
+```{r cars}
+library(knitr)
+library(kableExtra)
+library(xml2)
+
+dt <- data.frame(
+ row_id = 1:3,
+ contents = c(
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. Sed suscipit, odio maximus fringilla rutrum, massa dolor elementum eros, at eleifend lectus augue vitae velit. Morbi congue congue lectus et auctor. Pellentesque tincidunt mi nec tristique malesuada. Cras pharetra lectus ut volutpat hendrerit. Ut dapibus orci ligula, id interdum metus mattis at. Sed egestas vitae nisi nec interdum. Nullam efficitur molestie aliquet. Nam cursus consectetur libero id pretium. Maecenas urna neque, dignissim ut leo vel, ultricies vestibulum nisi.",
+ "In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in. Etiam posuere enim a sodales congue. Curabitur ac vehicula tortor. Proin ac nulla in eros scelerisque volutpat in in mi. Nullam efficitur sem nec eros mattis, id venenatis dolor auctor. Nunc dignissim aliquet tincidunt. Nulla rhoncus massa leo, vitae mattis urna lobortis vitae. Curabitur fringilla sodales tincidunt. Cras arcu erat, hendrerit vitae arcu eget, laoreet hendrerit nibh. Maecenas vitae suscipit metus. Fusce ac enim posuere, scelerisque dui vitae, tristique libero. Nunc venenatis sodales nunc, in consectetur nisl hendrerit id. Fusce quis nibh vitae elit tristique blandit. Nunc accumsan pulvinar varius.",
+ "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. Suspendisse potenti. Etiam viverra, massa id lobortis facilisis, ex nibh dictum ligula, id interdum leo velit commodo velit. Nulla malesuada vel arcu at malesuada. Quisque feugiat nunc id nisl varius, nec dapibus tellus ultricies. Donec enim velit, semper in molestie vel, auctor porta lorem. Donec egestas venenatis augue sit amet tristique. Sed in justo ut est dictum consequat. Morbi dictum ornare magna vitae gravida. Nunc congue diam vitae lacinia vulputate. Curabitur hendrerit, erat quis tempus eleifend, dui turpis pellentesque libero, et pretium odio lorem eu magna."
+ ),
+ contents2 = c(
+ "Lorem ipsum dolor",
+ "In eu urna ",
+ "Vivamus venenatis"
+ ),
+ contents3 = c(
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. Sed suscipit, odio maximus fringilla rutrum, massa dolor elementum eros, at eleifend lectus augue vitae velit. Morbi congue congue lectus et auctor. Pellentesque tincidunt mi nec tristique malesuada. Cras pharetra lectus ut volutpat hendrerit. Ut dapibus orci ligula, id interdum metus mattis at. Sed egestas vitae nisi nec interdum. Nullam efficitur molestie aliquet. Nam cursus consectetur libero id pretium. Maecenas urna neque, dignissim ut leo vel, ultricies vestibulum nisi.",
+ "In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in. Etiam posuere enim a sodales congue. Curabitur ac vehicula tortor. Proin ac nulla in eros scelerisque volutpat in in mi. Nullam efficitur sem nec eros mattis, id venenatis dolor auctor. Nunc dignissim aliquet tincidunt. Nulla rhoncus massa leo, vitae mattis urna lobortis vitae. Curabitur fringilla sodales tincidunt. Cras arcu erat, hendrerit vitae arcu eget, laoreet hendrerit nibh. Maecenas vitae suscipit metus. Fusce ac enim posuere, scelerisque dui vitae, tristique libero. Nunc venenatis sodales nunc, in consectetur nisl hendrerit id. Fusce quis nibh vitae elit tristique blandit. Nunc accumsan pulvinar varius.",
+ "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. Suspendisse potenti. Etiam viverra, massa id lobortis facilisis, ex nibh dictum ligula, id interdum leo velit commodo velit. Nulla malesuada vel arcu at malesuada. Quisque feugiat nunc id nisl varius, nec dapibus tellus ultricies. Donec enim velit, semper in molestie vel, auctor porta lorem. Donec egestas venenatis augue sit amet tristique. Sed in justo ut est dictum consequat. Morbi dictum ornare magna vitae gravida. Nunc congue diam vitae lacinia vulputate. Curabitur hendrerit, erat quis tempus eleifend, dui turpis pellentesque libero, et pretium odio lorem eu magna."
+ )
+)
+
+kable(dt, "html") %>%
+ kable_styling() %>%
+ column_spec(2, "5cm", bold = T) %>%
+ column_spec(4, "3cm", italic = T)
+```
+
diff --git a/tests/visual_tests/column_width_pdf.Rmd b/tests/visual_tests/column_width_pdf.Rmd
new file mode 100644
index 0000000..d8e8a52
--- /dev/null
+++ b/tests/visual_tests/column_width_pdf.Rmd
@@ -0,0 +1,44 @@
+---
+title: "Untitled"
+output: pdf_document
+---
+
+```{r setup, include=FALSE}
+knitr::opts_chunk$set(echo = TRUE)
+```
+
+## R Markdown
+
+This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
+
+When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
+
+```{r cars}
+library(knitr)
+library(kableExtra)
+library(xml2)
+
+dt <- data.frame(
+ row_id = 1:3,
+ contents = c(
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. Sed suscipit, odio maximus fringilla rutrum, massa dolor elementum eros, at eleifend lectus augue vitae velit. Morbi congue congue lectus et auctor. Pellentesque tincidunt mi nec tristique malesuada. Cras pharetra lectus ut volutpat hendrerit. Ut dapibus orci ligula, id interdum metus mattis at. Sed egestas vitae nisi nec interdum. Nullam efficitur molestie aliquet. Nam cursus consectetur libero id pretium. Maecenas urna neque, dignissim ut leo vel, ultricies vestibulum nisi.",
+ "In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in. Etiam posuere enim a sodales congue. Curabitur ac vehicula tortor. Proin ac nulla in eros scelerisque volutpat in in mi. Nullam efficitur sem nec eros mattis, id venenatis dolor auctor. Nunc dignissim aliquet tincidunt. Nulla rhoncus massa leo, vitae mattis urna lobortis vitae. Curabitur fringilla sodales tincidunt. Cras arcu erat, hendrerit vitae arcu eget, laoreet hendrerit nibh. Maecenas vitae suscipit metus. Fusce ac enim posuere, scelerisque dui vitae, tristique libero. Nunc venenatis sodales nunc, in consectetur nisl hendrerit id. Fusce quis nibh vitae elit tristique blandit. Nunc accumsan pulvinar varius.",
+ "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. Suspendisse potenti. Etiam viverra, massa id lobortis facilisis, ex nibh dictum ligula, id interdum leo velit commodo velit. Nulla malesuada vel arcu at malesuada. Quisque feugiat nunc id nisl varius, nec dapibus tellus ultricies. Donec enim velit, semper in molestie vel, auctor porta lorem. Donec egestas venenatis augue sit amet tristique. Sed in justo ut est dictum consequat. Morbi dictum ornare magna vitae gravida. Nunc congue diam vitae lacinia vulputate. Curabitur hendrerit, erat quis tempus eleifend, dui turpis pellentesque libero, et pretium odio lorem eu magna."
+ ),
+ contents2 = c(
+ "Lorem ipsum dolor",
+ "In eu urna ",
+ "Vivamus venenatis"
+ ),
+ contents3 = c(
+ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. Sed suscipit, odio maximus fringilla rutrum, massa dolor elementum eros, at eleifend lectus augue vitae velit. Morbi congue congue lectus et auctor. Pellentesque tincidunt mi nec tristique malesuada. Cras pharetra lectus ut volutpat hendrerit. Ut dapibus orci ligula, id interdum metus mattis at. Sed egestas vitae nisi nec interdum. Nullam efficitur molestie aliquet. Nam cursus consectetur libero id pretium. Maecenas urna neque, dignissim ut leo vel, ultricies vestibulum nisi.",
+ "In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in. Etiam posuere enim a sodales congue. Curabitur ac vehicula tortor. Proin ac nulla in eros scelerisque volutpat in in mi. Nullam efficitur sem nec eros mattis, id venenatis dolor auctor. Nunc dignissim aliquet tincidunt. Nulla rhoncus massa leo, vitae mattis urna lobortis vitae. Curabitur fringilla sodales tincidunt. Cras arcu erat, hendrerit vitae arcu eget, laoreet hendrerit nibh. Maecenas vitae suscipit metus. Fusce ac enim posuere, scelerisque dui vitae, tristique libero. Nunc venenatis sodales nunc, in consectetur nisl hendrerit id. Fusce quis nibh vitae elit tristique blandit. Nunc accumsan pulvinar varius.",
+ "Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. Suspendisse potenti. Etiam viverra, massa id lobortis facilisis, ex nibh dictum ligula, id interdum leo velit commodo velit. Nulla malesuada vel arcu at malesuada. Quisque feugiat nunc id nisl varius, nec dapibus tellus ultricies. Donec enim velit, semper in molestie vel, auctor porta lorem. Donec egestas venenatis augue sit amet tristique. Sed in justo ut est dictum consequat. Morbi dictum ornare magna vitae gravida. Nunc congue diam vitae lacinia vulputate. Curabitur hendrerit, erat quis tempus eleifend, dui turpis pellentesque libero, et pretium odio lorem eu magna."
+ )
+)
+
+kable(dt, "latex", booktabs = T) %>%
+ column_spec(2, "5cm", bold = T) %>%
+ column_spec(4, "3cm", italic = T)
+```
+