Add support for global options
diff --git a/R/add_footnote.R b/R/add_footnote.R
index 5977104..dd1bd4b 100644
--- a/R/add_footnote.R
+++ b/R/add_footnote.R
@@ -17,15 +17,24 @@
#'
#' @export
add_footnote <- function(input, label = NULL,
- notation = c("alphabet", "number", "symbol"),
+ notation = "alphabet",
threeparttable = FALSE) {
if (is.null(label)) return(input)
- notation <- match.arg(notation)
+ if (notation == "alphabet") {
+ notation <- getOption("kable_footnote_notation", "alphabet")
+ }
+ if (!threeparttable) {
+ threeparttable <- getOption("kable_footnote_threeparttable", FALSE)
+ }
+
+ notation <- match.arg(notation, c("alphabet", "number", "symbol"))
if (notation == "symbol") {
notation <- paste0(notation, ".", attr(input, "format"))
}
+ table_info <- NULL
+
ids.ops <- read.csv(system.file("symbol_index.csv", package = "kableExtra"))
ids <- ids.ops[, notation]
ids.intable <- gsub("\\*", "\\\\*", ids)
@@ -86,8 +95,8 @@
label <- escape_latex(label)
label <- gsub("\\\\", "\\\\\\\\", label)
- kable_info <- magic_mirror(input)
- if (kable_info$tabular == "longtable") {
+ table_info <- magic_mirror(input)
+ if (table_info$tabular == "longtable") {
if (notation != "number") {
warning("Notation is set to 'number' and other formats are not supported.")
notation <- "number"
@@ -104,8 +113,8 @@
# See http://tex.stackexchange.com/questions/50151/footnotes-in-longtable-captions
count.in.caption.note <- 0
- if (!is.na(kable_info$caption)) {
- count.in.caption.note <- str_count(kable_info$caption, "\\[note\\]")
+ if (!is.na(table_info$caption)) {
+ count.in.caption.note <- str_count(table_info$caption, "\\[note\\]")
}
if (count.in.caption.note != 0) {
caption.footnote <- paste0("\\\\addtocounter{footnote}{-",
@@ -173,17 +182,17 @@
} else {
table.width <- max(nchar(
str_replace_all(
- str_replace_all(kable_info$contents, "\\[note\\]", ""),
- "\\[note[0-9]{1,2}\\]", ""))) + 2 * (kable_info$ncol - 1)
+ str_replace_all(table_info$contents, "\\[note\\]", ""),
+ "\\[note[0-9]{1,2}\\]", ""))) + 2 * (table_info$ncol - 1)
footer <- ""
for (i in 1:count.label) {
label.wrap <- strwrap(label[i], table.width)
- footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
+ footer <- paste0(footer, "\\\\multicolumn{", table_info$ncol,
"}{l}{\\\\textsuperscript{", ids[i], "} ",
label.wrap[1], "}\\\\\\\\\n")
if (length(label.wrap) > 1) {
for (j in 2:length(label.wrap)) {
- footer <- paste0(footer, "\\\\multicolumn{", kable_info$ncol,
+ footer <- paste0(footer, "\\\\multicolumn{", table_info$ncol,
"}{l}{", label.wrap[j], "}\\\\\\\\\n")
}
}
@@ -198,8 +207,8 @@
# HTML Tables -------------------
if (attr(input, "format") == "html") {
# Clean the entry for labels
+ table_info <- magic_mirror(input)
label <- escape_html(label)
-
# Replace in-table notation with appropriate symbol
for (i in 1:count.intablenote) {
export <- sub("\\[note\\]",
@@ -226,5 +235,6 @@
# Paste footer to the table
export[1] <- gsub("</tbody>\n", paste0("</tbody>\n", footer), export[1])
}
+ attr(export, "original_kable_meta") <- table_info
return(export)
}
diff --git a/R/add_header_above.R b/R/add_header_above.R
index baba605..5c9531b 100644
--- a/R/add_header_above.R
+++ b/R/add_header_above.R
@@ -30,6 +30,7 @@
# HTML
htmlTable_add_header_above <- function(kable_input, header = NULL) {
if (is.null(header)) return(kable_input)
+ table_info <- magic_mirror(kable_input)
kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
# somehow xml2 cannot directly search by name here (it will result in a crash)
kable_xml_thead <- xml_child(kable_xml, 1)
@@ -49,8 +50,10 @@
new_header_row <- htmlTable_new_header_generator(header)
xml_add_child(kable_xml_thead, new_header_row, .where = 0)
- return(structure(as.character(kable_xml), format = "html",
- class = "knitr_kable"))
+ out <- structure(as.character(kable_xml), format = "html",
+ class = "knitr_kable")
+ attr(out, "original_kable_meta") <- table_info
+ return()
}
standardize_header_input <- function(header) {
@@ -88,6 +91,7 @@
pdfTable_new_header_generator(header, table_info$booktabs)),
as.character(kable_input))
out <- structure(out, format = "latex", class = "knitr_kable")
+ attr(out, "original_kable_meta") <- table_info
return(out)
}
diff --git a/R/kable_styling.R b/R/kable_styling.R
index 33027a0..44b4d0f 100644
--- a/R/kable_styling.R
+++ b/R/kable_styling.R
@@ -37,16 +37,34 @@
bootstrap_options = "basic",
latex_options = "basic",
full_width = NULL,
- position = c("center", "left", "right",
- "float_left", "float_right"),
+ position = "center",
font_size = NULL) {
+
+ if (length(bootstrap_options) == 1 && bootstrap_options == "basic") {
+ bootstrap_options <- getOption("kable_styling_bootstrap_options", "basic")
+ }
+ if (length(latex_options) == 1 && latex_options == "basic") {
+ latex_options <- getOption("kable_styling_latex_options", "basic")
+ }
+ if (position == "center") {
+ position <- getOption("kable_styling_position", "center")
+ }
+ position <- match.arg(position,
+ c("center", "left", "right", "float_left", "float_right"))
+ if (is.null(font_size)) {
+ font_size <- getOption("kable_styling_font_size", NULL)
+ }
+
kable_format <- attr(kable_input, "format")
+
if (!kable_format %in% c("html", "latex")) {
stop("Please specify output format in your kable function. Currently ",
"generic markdown table using pandoc is not supported.")
}
if (kable_format == "html") {
- if (is.null(full_width)) full_width <- T
+ if (is.null(full_width)) {
+ full_width <- getOption("kable_styling_full_width", T)
+ }
return(htmlTable_styling(kable_input,
bootstrap_options = bootstrap_options,
full_width = full_width,
@@ -54,7 +72,9 @@
font_size = font_size))
}
if (kable_format == "latex") {
- if (is.null(full_width)) full_width <- F
+ if (is.null(full_width)) {
+ full_width <- getOption("kable_styling_full_width", F)
+ }
return(pdfTable_styling(kable_input,
latex_options = latex_options,
full_width = full_width,
@@ -70,7 +90,7 @@
position = c("center", "left", "right",
"float_left", "float_right"),
font_size = NULL) {
-
+ table_info <- magic_mirror(kable_input)
kable_xml <- read_xml(as.character(kable_input), options = c("COMPACT"))
# Modify class
@@ -121,8 +141,11 @@
if (length(kable_xml_style) != 0) {
xml_attr(kable_xml, "style") <- paste(kable_xml_style, collapse = " ")
}
- return(structure(as.character(kable_xml), format = "html",
- class = "knitr_kable"))
+
+ out <- structure(as.character(kable_xml), format = "html",
+ class = "knitr_kable")
+ attr(out, "original_kable_meta") <- table_info
+ return(out)
}
# LaTeX table style
@@ -176,6 +199,7 @@
out <- styling_latex_position(out, table_info, position, latex_options)
out <- structure(out, format = "latex", class = "knitr_kable")
+ attr(out, "original_kable_meta") <- table_info
return(out)
}
diff --git a/R/magic_mirror.R b/R/magic_mirror.R
index 9b88457..eeeb896 100644
--- a/R/magic_mirror.R
+++ b/R/magic_mirror.R
@@ -10,6 +10,9 @@
warning("magic_mirror may not be able to produce correct result if the",
" input table is not rendered by knitr::kable. ")
}
+ if ("original_kable_meta" %in% names(attributes(kable_input))) {
+ return(attr(kable_input, "original_kable_meta"))
+ }
kable_format <- attr(kable_input, "format")
if (kable_format == "latex") {
kable_info <- magic_mirror_latex(kable_input)
@@ -95,4 +98,3 @@
}
-
diff --git a/README.md b/README.md
index b79bbb1..56c24ef 100644
--- a/README.md
+++ b/README.md
@@ -22,6 +22,7 @@
```r
devtools::install_github("haozhu233/kableExtra")
```
+`kableExtra` will be submitted to CRAN soon.
## Basic Usage
```r
diff --git a/tests/visual_tests/add_header_above_pdf.Rmd b/tests/visual_tests/add_header_above_pdf.Rmd
index 62e5370..39797bf 100644
--- a/tests/visual_tests/add_header_above_pdf.Rmd
+++ b/tests/visual_tests/add_header_above_pdf.Rmd
@@ -1,35 +1,41 @@
---
title: "add_header_above"
-output: pdf_document
+output:
+ pdf_document:
+ keep_tex: true
---
```{r}
library(knitr)
library(kableExtra)
-options(knitr.table.format = "latex")
+options(knitr.table.format = "latex",
+ kable_styling_latex_options = c("striped", "hold_position"),
+ kable_styling_full_width = T)
# switch to "latex" in a pdf environment
dt <- mtcars[1:5, 1:4]
kable(dt, format = "latex", booktabs = T, caption = "Demo Table") %>%
- kable_styling(latex_options = "striped",
- full_width = F) %>%
+ kable_styling() %>%
add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>%
add_footnote(c("table footnote"))
```
```{r}
kable(dt, format = "latex", booktabs = T) %>%
- add_header_above(c(" ", "a" = 2, "b" = 2))
+ add_header_above(c(" ", "a" = 2, "b" = 2))%>%
+ kable_styling()
```
```{r}
kable(dt, format = "latex", booktabs = T, longtable = T, caption = "aaa") %>%
- add_header_above(c(" ", "a" = 2, "b" = 2))
+ add_header_above(c(" ", "a" = 2, "b" = 2))%>%
+ kable_styling()
```
```{r}
kable(dt, format = "latex", booktabs = T, caption = "aaa") %>%
- add_header_above(c(" ", "a" = 2, "b" = 2))
+ add_header_above(c(" ", "a" = 2, "b" = 2)) %>%
+ kable_styling()
```