Add function add_footnote to latex
diff --git a/R/footnote.R b/R/footnote.R
new file mode 100644
index 0000000..cca651a
--- /dev/null
+++ b/R/footnote.R
@@ -0,0 +1,88 @@
+#' Add footnote
+#'
+#' @description Add footnote to your favorite kable output. So far this function
+#' only works when you define \code{format} in your kable function or in the
+#' global knitr option \code{knitr.table.format}. In latex, we are using the
+#' \code{threeparttable} package so you need to import this package in your
+#' \code{YAML} header.
+#'
+#' @param input The direct output of your \code{kable} function or your last
+#' \code{kableExtra} function.
+#' @param label A vector of footnotes you want to add. You don't need to add
+#' notations in your notes.
+#' @param notation You can select the format of your footnote notation from
+#' "number", "alphabet" and "symbol".
+#'
+#' @export
+add_footnote <- function(input, label = NULL, notation = "alphabet") {
+ 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 "symbol". Now add_footnote is using "alphabet" as default.')
+ }
+ if (notation == "symbol") {notation = paste0(notation, ".", attr(input, "format"))}
+ ids.ops <- data.frame(
+ number = as.character(1:20),
+ alphabet = letters[1:20],
+ symbol.latex = c(
+ "*", "\\\\dag", "\\\\ddag", "\\\\S", "\\\\P",
+ "**", "\\\\dag\\\\dag", "\\\\ddag\\\\ddag", "\\\\S\\\\S", "\\\\P\\\\P",
+ "***", "\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag", "\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P",
+ "****", "\\\\dag\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag\\\\ddag", "\\\\S\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P\\\\P"
+ ),
+ symbol.html = c(
+ "*", "†", "‡", "§", "¶",
+ "**", "††", "‡‡", "§§", "¶¶",
+ "*", "†††", "‡‡‡", "§§§", "¶¶¶",
+ "**", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
+ )
+ )
+ ids <- ids.ops[,notation]
+
+ if(!attr(input, "format") %in% c("html", "latex")){
+ warning("Currently kableExtra only supports html and latex. You have to specify your kable export format or set it in the global option `knitr.table.format`.")
+ export <- input
+ }
+
+ # Generate latex table footnote using threeparttable --------------------------------
+ if(attr(input, "format")=="latex"){
+ #count the number of items in label and intable notation
+ count.label = length(label)
+ count.intablenoot = sum(gregexpr("\\[note\\]", input)[[1]]>0)
+ if (count.intablenoot != 0 & count.label != count.intablenoot){
+ warning(paste("You entered", count.label, "labels but you put",
+ count.intablenoot, "[note] in your table."))
+ }
+
+ # generate footer with appropriate symbol
+ footer <- ""
+ for(i in 1:count.label){
+ footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
+ }
+
+ # Replace in-table notation with appropriate symbol
+ for(i in 1:count.intablenoot){
+ input <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids[i], "}"), input)
+ }
+
+ #
+ if(grepl("\\\\caption\\{.*?\\}", input)){
+ if(grepl("\\\\begin\\{tabular\\}", input)){
+ export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", input)
+ }else{export <- input}
+ }else{
+ export <- sub("\\\\begin\\{tabular\\}", "\\\\begin{threeparttable}\n\\\\begin{tabular}", input)
+ }
+ export <- gsub(
+ "\\\\end\\{tabular\\}",
+ paste0(
+ "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
+ footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
+ ),
+ export)
+ }
+ if(attr(input, "format")=="html"){
+
+ }
+ return(export)
+}
diff --git a/R/hello.R b/R/hello.R
deleted file mode 100644
index 3c562c4..0000000
--- a/R/hello.R
+++ /dev/null
@@ -1,18 +0,0 @@
-# Hello, world!
-#
-# This is an example function named 'hello'
-# which prints 'Hello, world!'.
-#
-# You can learn more about package authoring with RStudio at:
-#
-# http://r-pkgs.had.co.nz/
-#
-# Some useful keyboard shortcuts for package authoring:
-#
-# Build and Reload Package: 'Ctrl + Shift + B'
-# Check Package: 'Ctrl + Shift + E'
-# Test Package: 'Ctrl + Shift + T'
-
-hello <- function() {
- print("Hello, world!")
-}
diff --git a/R/test-add_footnote.r b/R/test-add_footnote.r
deleted file mode 100644
index a17b3ca..0000000
--- a/R/test-add_footnote.r
+++ /dev/null
@@ -1,11 +0,0 @@
-context("Add_Footnote")
-#####
-
-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'")
-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)
-latextable_4 <- kable(rtable, format = "latex", booktab = T, caption = "Table")
-