Magic mirror for html; minor changes; escape for add footnote
diff --git a/.Rbuildignore b/.Rbuildignore
index 91114bf..99feece 100644
--- a/.Rbuildignore
+++ b/.Rbuildignore
@@ -1,2 +1,3 @@
^.*\.Rproj$
^\.Rproj\.user$
+^README.Rmd$
diff --git a/DESCRIPTION b/DESCRIPTION
index 38c3907..252622c 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -10,11 +10,12 @@
License: MIT + file LICENSE
LazyData: TRUE
Depends:
- R (>= 3.1.2),
- stringr (>= 1.0)
+ R (>= 3.1.2)
Imports:
knitr (>= 1.10),
- magrittr
+ magrittr,
+ stringr (>= 1.0),
+ XML
Suggests:
testthat
RoxygenNote: 5.0.1
diff --git a/NAMESPACE b/NAMESPACE
index 6d17c4a..ace2ce4 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -1,4 +1,8 @@
# Generated by roxygen2: do not edit by hand
+export("%>%")
export(add_footnote)
export(magic_mirror)
+import(stringr)
+importFrom(XML,readHTMLTable)
+importFrom(knitr,kable)
diff --git a/R/footnote.R b/R/footnote.R
index be0ea04..e995af1 100644
--- a/R/footnote.R
+++ b/R/footnote.R
@@ -14,9 +14,10 @@
#' "number", "alphabet" and "symbol".
#'
#' @export
-add_footnote <- function(input, label = NULL, notation = "alphabet",
+add_footnote <- function(input, label = NULL, notation = "alphabet", escape = T,
threeparttable = F) {
if (is.null(label)){return(input)}
+
# Define available id list
if (!notation %in% c("number", "alphabet", "symbol")){
warning('Please select your notation within "number", "alphabet" and ',
@@ -43,26 +44,32 @@
"§§§§", "¶¶¶¶"
),
symbol.markdown = c(
- "\\*", "†", "‡", "§", "¶",
- "\\*\\*", "††", "‡‡", "§§", "¶¶",
- "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶",
- "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
+ "\\*", "\u2020", "\u2021", "\u00A7", "\u00B6",
+ "\\*\\*", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
+ "\\*\\*\\*", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
+ "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
+ "\\*\\*\\*\\*", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
+ "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
),
symbol.pandoc = c(
- "\\*", "†", "‡", "§", "¶",
- "\\*\\*", "††", "‡‡", "§§", "¶¶",
- "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶",
- "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
+ "\\*", "\u2020", "\u2021", "\u00A7", "\u00B6",
+ "\\*\\*", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
+ "\\*\\*\\*", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
+ "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
+ "\\*\\*\\*\\*", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
+ "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
)
)
ids <- ids.ops[,notation]
# pandoc cannot recognize ^*^ as * is a special character. We have to use ^\*^
ids.intable <- gsub("\\*", "\\\\*", ids)
ids.simple <- c(
- "*", "†", "‡", "§", "¶",
- "**", "††", "‡‡", "§§", "¶¶",
- "***", "†††", "‡‡‡", "§§§", "¶¶¶",
- "****", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
+ "*", "\u2020", "\u2021", "\u00A7", "\u00B6",
+ "**", "\u2020\u2020", "\u2021\u2021", "\u00A7\u00A7", "\u00B6\u00B6",
+ "***", "\u2020\u2020\u2020", "\u2021\u2021\u2021",
+ "\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6",
+ "****", "\u2020\u2020\u2020\u2020", "\u2021\u2021\u2021\u2021",
+ "\u00A7\u00A7\u00A7\u00A7", "\u00B6\u00B6\u00B6\u00B6"
)
#count the number of items in label and intable notation
@@ -117,6 +124,9 @@
# Generate latex table footnote --------------------------------
if(attr(input, "format")=="latex"){
+ # Clean the entry for labels when escape is enabled
+ if (escape = T){label <- knitr:::escape_latex(label)}
+
kable_info <- magic_mirror(input)
if(kable_info$tabular == "longtable"){
if(notation != "number"){
@@ -220,6 +230,3 @@
}
return(export)
}
-
-
-
diff --git a/R/magic_mirror.R b/R/magic_mirror.R
index d9d78d1..9995180 100644
--- a/R/magic_mirror.R
+++ b/R/magic_mirror.R
@@ -1,7 +1,8 @@
#' Magic mirror that returns kable's attributes
#'
#' @param input The output of kable
-#'
+#' @importFrom knitr kable
+#' @import stringr
#' @export
magic_mirror <- function(input){
@@ -19,7 +20,8 @@
return(kable_info)
}
-#' Magic mirror for latex tables
+#' Magic mirror for latex tables --------------
+#' @param input The output of kable
magic_mirror_latex <- function(input){
kable_info <- list(tabular = NULL, booktabs = NULL, align = NULL,
ncol=NULL, nrow=NULL, colnames = NULL, rownames = NULL,
@@ -56,3 +58,46 @@
kable_info$rownames <- str_extract(kable_info$contents, "^[^ &]*")
return(kable_info)
}
+
+#' Magic Mirror for html table --------
+#'
+#' @param input The output of kable
+#'
+#' @importFrom XML readHTMLTable
+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 <- readHTMLTable(input[1])
+ # Caption
+ kable_info$caption <- names(kable_data)
+ # Contents
+ kable_info$contents <- kable_data[[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$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 = ""
+ )
+ return(kable_info)
+}
+
+#' @export
+magrittr::`%>%`
+
diff --git a/man/add_footnote.Rd b/man/add_footnote.Rd
index fd8457d..17122b1 100644
--- a/man/add_footnote.Rd
+++ b/man/add_footnote.Rd
@@ -4,7 +4,8 @@
\alias{add_footnote}
\title{Add footnote}
\usage{
-add_footnote(input, label = NULL, notation = "alphabet")
+add_footnote(input, label = NULL, notation = "alphabet",
+ threeparttable = F)
}
\arguments{
\item{input}{The direct output of your \code{kable} function or your last
diff --git a/man/magic_mirror_html.Rd b/man/magic_mirror_html.Rd
new file mode 100644
index 0000000..afdcf9a
--- /dev/null
+++ b/man/magic_mirror_html.Rd
@@ -0,0 +1,12 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/magic_mirror.R
+\name{magic_mirror_html}
+\alias{magic_mirror_html}
+\title{Magic Mirror for html table --------}
+\usage{
+magic_mirror_html(input)
+}
+\description{
+Magic Mirror for html table --------
+}
+
diff --git a/man/magic_mirror_latex.Rd b/man/magic_mirror_latex.Rd
index 8a2f0f3..c5874fd 100644
--- a/man/magic_mirror_latex.Rd
+++ b/man/magic_mirror_latex.Rd
@@ -2,11 +2,11 @@
% Please edit documentation in R/magic_mirror.R
\name{magic_mirror_latex}
\alias{magic_mirror_latex}
-\title{Magic mirror for latex tables}
+\title{Magic mirror for latex tables --------------}
\usage{
magic_mirror_latex(input)
}
\description{
-Magic mirror for latex tables
+Magic mirror for latex tables --------------
}
diff --git a/man/reexports.Rd b/man/reexports.Rd
new file mode 100644
index 0000000..c8f026e
--- /dev/null
+++ b/man/reexports.Rd
@@ -0,0 +1,16 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/magic_mirror.R
+\docType{import}
+\name{reexports}
+\alias{\%>\%}
+\alias{reexports}
+\title{Objects exported from other packages}
+\description{
+These objects are imported from other packages. Follow the links
+below to see their documentation.
+
+\describe{
+ \item{magrittr}{\code{\link[magrittr]{\%>\%}}}
+}}
+\keyword{internal}
+
diff --git a/tests/testthat/test-add_footnote.r b/tests/testthat/test-add_footnote.r
index 1083e6c..32783c9 100644
--- a/tests/testthat/test-add_footnote.r
+++ b/tests/testthat/test-add_footnote.r
@@ -4,6 +4,7 @@
rtable <- data.frame(variable = c("mpg", "wt"), mean = c(20.091, 3.217), sd = c(6.027, 0.978))
htmltable_1 <- kable(rtable, format = "html")
htmltable_2 <- kable(rtable, format = "html", caption = "Table", table.attr = "class = 'table table-striped table-hover'")
+htmltable_3 <- kable(rtable, row.names = T, format = "html", caption = "Table", table.attr = "class = 'table table-striped table-hover'")
latextable_1 <- kable(rtable, format = "latex")
latextable_2 <- kable(rtable, format = "latex", caption = "Table", row.names = T, align = c("l", "c", "c"))
latextable_3 <- kable(rtable, format = "latex", booktab = T)