Update readme and documentations
diff --git a/R/add_footnote.R b/R/add_footnote.R
index 5db0778..5977104 100644
--- a/R/add_footnote.R
+++ b/R/add_footnote.R
@@ -12,7 +12,8 @@
#' notations in your notes.
#' @param notation You can select the format of your footnote notation from
#' "number", "alphabet" and "symbol".
-#' @param threeparttable Boolean value indicating if a \href{https://www.ctan.org/pkg/threeparttable}{threeparttable} scheme should be used.
+#' @param threeparttable Boolean value indicating if a
+#' \href{https://www.ctan.org/pkg/threeparttable}{threeparttable} scheme should be used.
#'
#' @export
add_footnote <- function(input, label = NULL,
@@ -149,6 +150,7 @@
}
if (threeparttable == T) {
# generate footer with appropriate symbol
+ usepackage_latex("threeparttable")
footer <- ""
for (i in 1:count.label) {
footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
diff --git a/R/add_header_above.R b/R/add_header_above.R
index 4c83a8e..baba605 100644
--- a/R/add_header_above.R
+++ b/R/add_header_above.R
@@ -1,4 +1,17 @@
-#' Add an extra header row above the current header
+#' Add a header row on top of current header
+#'
+#' @description Tables with multiple rows of header rows are extremely useful
+#' to demonstrate grouped data. This function takes the output of a `kable()`
+#' function and adds an header row on top of it. This function can work with
+#' both `HTML` and `LaTeX` outputs
+#'
+#' @param kable_input Output of `knitr::kable()` with `format` specified
+#' @param header A (named) character vector with `colspan` as values. For
+#' example, `c(" " = 1, "title" = 2)` can be used to create a new header row
+#' for a 3-column table with "title" spanning across column 2 and 3. For
+#' convenience, when `colspan` equals to 1, users can drop the ` = 1` part.
+#' As a result, `c(" ", "title" = 2)` is the same as `c(" " = 1, "title" = 2)`.
+#'
#' @export
add_header_above <- function(kable_input, header = NULL) {
kable_format <- attr(kable_input, "format")
diff --git a/R/from_knitr.R b/R/from_knitr.R
index f782a19..1948795 100644
--- a/R/from_knitr.R
+++ b/R/from_knitr.R
@@ -1,8 +1,8 @@
# These functions are imported from knitr/highr as `:::` is not recommended by
# CRAN
-#' escape special LaTeX characters
-#' @author Yihui Xie
+# escape special LaTeX characters
+# @author Yihui Xie
escape_latex <- function(x, newlines = FALSE, spaces = FALSE) {
x = gsub('\\\\', '\\\\textbackslash', x)
x = gsub('([#$%&_{}])', '\\\\\\1', x)
@@ -14,8 +14,8 @@
x
}
-#' escape special HTML characters
-#' @author Yihui Xie
+# escape special HTML characters
+# @author Yihui Xie
escape_html <- function(x) {
x = gsub('&', '&', x)
x = gsub('<', '<', x)
diff --git a/R/kableExtra-package.R b/R/kableExtra-package.R
index 11528b5..904c032 100644
--- a/R/kableExtra-package.R
+++ b/R/kableExtra-package.R
@@ -1,11 +1,14 @@
#' kableExtra
#'
-#' @importFrom stringr str_count str_split
+#' @importFrom stringr str_count str_split str_match str_detect str_match_all
+#' str_extract str_replace_all str_trim str_extract_all
#' @importFrom xml2 read_xml xml_attr xml_has_attr xml_attr<- read_html
-#' xml_child xml_children xml_name xml_add_sibling xml_add_child
+#' xml_child xml_children xml_name xml_add_sibling xml_add_child xml_text
#' @importFrom rvest html_table
#' @importFrom knitr knit_meta_add
#' @importFrom rmarkdown latex_dependency
+#' @importFrom magrittr %>%
+#' @importFrom utils read.csv
#' @name kableExtra-package
#' @aliases kableExtra
#' @docType package
diff --git a/R/kable_styling.R b/R/kable_styling.R
index 2d4d148..33027a0 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -4,15 +4,32 @@
#' of HTML tables other than using the `table.attr` option in `knitr::kable()`.
#' Currenly, it assumes the HTML document has boot
#'
-#' @param bootstrap_options A character vector for bootstrap table options. For
-#' detailed information, please check the package vignette or visit the
-#' w3schools' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page}
-#' . Possible options include "basic", "striped", "bordered", "hover",
-#' "condensed" and "responsive".
+#' @param kable_input Output of `knitr::kable()` with `format` specified
+#' @param bootstrap_options A character vector for bootstrap table options.
+#' Please see package documentation site or visit the w3schools'
+#' \href{https://www.w3schools.com/bootstrap/bootstrap_tables.asp}{Bootstrap Page}
+#' for more information. Possible options include `basic`, `striped`,
+#' `bordered`, `hover`, `condensed` and `responsive`.
+#' @param latex_options A character vector for LaTeX table options. Please see
+#' package documentation site for more information. Possible options include
+#' `basic`, `striped`, `hold_position`, `scale_down`. `striped` will add
+#' alternative row colors to the table. It will imports `LaTeX` package `xcolor`
+#' if enabled. `hold_position` will "hold" the floating table to the exact
+#' position. It is useful when the `LaTeX` table is contained in a `table`
+#' environment after you specified captions in `kable()`. It will force the
+#' table to stay in the position where it was created in the document.
+#' `scale_down` is useful for super wide table. It will automatically adjust
+#' the table to page width.
#' @param full_width A `TRUE` or `FALSE` variable controlling whether the HTML
-#' table should have 100\% width.
-#' @param position A character string determining whether and how the HTML table
-#' should float on the page. Values could be "left", "center", "right"
+#' table should have 100\% width. Since HTML and pdf have different flavors on
+#' the preferable format for `full_width`. If not specified, a HTML table will
+#' have full width by default but this option will be set to `FALSE` for a
+#' LaTeX table
+#' @param position A character string determining how to position the table
+#' on a page. Possible values include `left`, `center`, `right`, `float_left`
+#' and `float_right`. Please see the package doc site for demonstrations. For
+#' a `LaTeX` table, if `float_*` is selected, `LaTeX` package `wrapfig` will be
+#' imported.
#' @param font_size A numeric input for table font size
#'
#' @export
@@ -214,11 +231,7 @@
styling_latex_position_center <- function(x, table_info, hold_position) {
if (!table_info$table_env & table_info$tabular == "tabular") {
- table_env_setup <- "\\begin{table}"
- if (hold_position) {
- table_env_setup <- paste0(table_env_setup, "[!h]")
- }
- return(paste0(table_env_setup, "\n\\centering", x, "\n\\end{table}"))
+ return(paste0("\\begin{table}[!h]\n\\centering", x, "\n\\end{table}"))
}
return(x)
}
@@ -242,17 +255,21 @@
if (option == "l") return(styling_latex_position_left(x, table_info))
if (option == "r") return(styling_latex_position_right(x, table_info, F))
}
+ usepackage_latex("wrapfig")
+ size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
+ col_max_length <- apply(size_matrix, 1, max) + 4
if (table_info$table_env) {
- usepackage_latex("wrapfig")
- size_matrix <- sapply(sapply(table_info$contents, str_split, " & "), nchar)
- col_max_length <- apply(size_matrix, 1, max) + 4
option <- sprintf("\\\\begin\\{wraptable\\}\\{%s\\}", option)
option <- paste0(option, "\\{",sum(col_max_length) * 0.15, "cm\\}")
x <- sub("\\\\begin\\{table\\}\\[\\!h\\]", "\\\\begin\\{table\\}", x)
x <- sub("\\\\begin\\{table\\}", option, x)
x <- sub("\\\\end\\{table\\}", "\\\\end\\{wraptable\\}", x)
- return(x)
+ } else {
+ option <- sprintf("\\begin{wraptable}{%s}", option)
+ option <- paste0(option, "{",sum(col_max_length) * 0.15, "cm}")
+ x <- paste0(option, x, "\\end{wraptable}")
}
+ return(x)
}
styling_latex_font_size <- function(x, table_info, font_size) {
diff --git a/R/magic_mirror.R b/R/magic_mirror.R
index 28be804..9b88457 100644
--- a/R/magic_mirror.R
+++ b/R/magic_mirror.R
@@ -1,60 +1,61 @@
#' Magic mirror that returns kable's attributes
#'
-#' @param input The output of kable
-#' @import stringr
+#' @description Mirror mirror tell me, how does this kable look like?
+#'
+#' @param kable_input The output of kable
#' @export
-magic_mirror <- function(input){
- if(!"knitr_kable" %in% attr(input, "class")){
+magic_mirror <- function(kable_input){
+ if (!"knitr_kable" %in% attr(kable_input, "class")) {
warning("magic_mirror may not be able to produce correct result if the",
" input table is not rendered by knitr::kable. ")
}
- kable_format <- attr(input, "format")
- if (kable_format == "latex"){
- kable_info <- magic_mirror_latex(input)
+ kable_format <- attr(kable_input, "format")
+ if (kable_format == "latex") {
+ kable_info <- magic_mirror_latex(kable_input)
}
- if (kable_format == "html"){
- kable_info <- magic_mirror_html(input)
+ if (kable_format == "html") {
+ kable_info <- magic_mirror_html(kable_input)
}
return(kable_info)
}
#' Magic mirror for latex tables --------------
-#' @param input The output of kable
-magic_mirror_latex <- function(input){
+#' @param kable_input The output of kable
+magic_mirror_latex <- function(kable_input){
kable_info <- list(tabular = NULL, booktabs = FALSE, align = NULL,
valign = NULL, ncol = NULL, nrow = NULL, colnames = NULL,
rownames = NULL, caption = NULL, contents = NULL,
centering = FALSE, table_env = FALSE)
# Tabular
kable_info$tabular <- ifelse(
- grepl("\\\\begin\\{tabular\\}", input),
+ grepl("\\\\begin\\{tabular\\}", kable_input),
"tabular", "longtable"
)
# Booktabs
- kable_info$booktabs <- grepl("\\\\toprule", input)
+ kable_info$booktabs <- grepl("\\\\toprule", kable_input)
# Align
kable_info$align <- gsub("\\|", "", str_match(
- input, paste0("\\\\begin\\{", kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2])
+ kable_input, paste0("\\\\begin\\{", kable_info$tabular,"\\}.*\\{(.*?)\\}"))[2])
# valign
kable_info$valign <- gsub("\\|", "", str_match(
- input, paste0("\\\\begin\\{", kable_info$tabular,"\\}(.*)\\{.*?\\}"))[2])
+ kable_input, paste0("\\\\begin\\{", kable_info$tabular,"\\}(.*)\\{.*?\\}"))[2])
# N of columns
kable_info$ncol <- nchar(kable_info$align)
# Caption
- kable_info$caption <- str_match(input, "caption\\{(.*?)\\n")[2]
+ kable_info$caption <- str_match(kable_input, "caption\\{(.*?)\\n")[2]
# N of rows
- kable_info$nrow <- str_count(input, "\\\\\n") -
+ kable_info$nrow <- str_count(kable_input, "\\\\\n") -
# in the dev version (currently as of 11.2015) of knitr, when longtable is
# enabled, caption is moved inside the tabular environment. As a result,
# the number of rows should be adjusted.
ifelse(
kable_info$tabular == "longtable" & !is.na(kable_info$caption) &
- !str_detect(input, "\\\\begin\\{table\\}\\n\\n\\\\caption"),
+ !str_detect(kable_input, "\\\\begin\\{table\\}\\n\\n\\\\caption"),
1,0
)
# Contents
- kable_info$contents <- str_match_all(input, "\n(.*)\\\\\\\\")[[1]][,2]
+ kable_info$contents <- str_match_all(kable_input, "\n(.*)\\\\\\\\")[[1]][,2]
if (kable_info$tabular == "longtable" & !is.na(kable_info$caption)) {
kable_info$contents <- kable_info$contents[-1]
}
@@ -63,7 +64,7 @@
# Row names
kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*")
- kable_info$centering <- grepl("\\\\centering", input)
+ kable_info$centering <- grepl("\\\\centering", kable_input)
kable_info$table_env <- (!is.na(kable_info$caption) &
kable_info$tabular != "longtable")
@@ -72,38 +73,24 @@
#' Magic Mirror for html table --------
#'
-#' @param input The output of kable
-magic_mirror_html <- function(input){
- kable_info <- list(table.attr = NULL, align = NULL,
- ncol = NULL, nrow = NULL, colnames = NULL, rownames = NULL,
- caption = NULL, contents = NULL)
- kable_data <- html_table(read_html(input))
+#' @param kable_input The output of kable
+magic_mirror_html <- function(kable_input){
+ kable_info <- list()
+ kable_xml <- read_xml(as.character(kable_input))
# Caption
- kable_info$caption <- names(kable_data)
+ kable_info$caption <- xml_text(xml_child(kable_xml, "caption"))
# Contents
- kable_info$contents <- kable_data[[1]]
+ kable_info$contents <- html_table(read_html(as.character(kable_input)))[[1]]
# colnames
- kable_info$colnames <- str_replace_all(
- str_trim(names(kable_data[[1]])), "V[0-9]{1,2}", ""
- )
- # rownames
- kable_info$rownames <- as.character(kable_data[[1]][,1])
- if(str_trim(names(kable_data[[1]])[1]) != "V1"){
- kable_info$rownames <- c(str_trim(names(kable_data[[1]])[1]),
- kable_info$rownames)}
- # ncol
+ kable_info$colnames <- lapply(xml_children(xml_child(kable_xml, "thead")),
+ xml_children)
+ kable_info$colnames <- kable_info$colnames[[length(kable_info$colnames)]]
+ kable_info$colnames <- trimws(xml_text(kable_info$colnames))
kable_info$ncol <- length(kable_info$colnames)
- # nrow
- kable_info$nrow <- length(kable_info$rownames)
- # table.attr
- kable_info$table.attr <- str_match(input, "<table class = '(.*)'>")[2]
- # align
- kable_info$align <- str_match_all(
- input, 'style=\\"text-align:([^;]*);'
- )[[1]][,2]
- kable_info$align <- paste0(
- str_extract(tail(kable_info$align, kable_info$ncol), "."), collapse = ""
- )
+ kable_info$nrow_header <- length(xml_children(xml_child(kable_xml, "thead")))
+ kable_info$nrow_body <- nrow(kable_info$contents)
+ kable_info$table_class <- xml_attr(kable_xml, "class")
+ kable_info$table_style <- xml_attr(kable_xml, "style")
return(kable_info)
}
diff --git a/R/zzz.R b/R/zzz.R
index fe9dd91..848679b 100644
--- a/R/zzz.R
+++ b/R/zzz.R
@@ -1,5 +1,4 @@
.onLoad <- function(libname = find.package("kableExtra"), pkgname = "kableExtra") {
usepackage_latex("booktabs")
usepackage_latex("longtable")
- message("LaTeX package booktabs and longtable will be loaded by default.")
}