Add function add_footnote to latex
diff --git a/DESCRIPTION b/DESCRIPTION
index 1e53a7a..d1a356a 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,9 +1,20 @@
Package: kableExtra
Type: Package
-Title: What the Package Does (Title Case)
-Version: 0.1.0
-Author: Who wrote it
-Maintainer: Who to complain to <yourfault@somewhere.net>
-Description: More about what it does (maybe more than one line)
-License: What license is it under?
-LazyData: TRUE
\ No newline at end of file
+Title: Decorate kable output using pipe syntax
+Version: 0.0.1
+Authors@R: c(
+ person("Hao", "Zhu", email = "haozhu@hsl.harvard.edu", role = c("aut", "cre"))
+ )
+Description: A set of functions aiming to strengthen the functionality of
+ knitr::kable without destroying its simplicity by adopting pipe syntax.
+License: MIT + file LICENSE
+LazyData: TRUE
+Depends:
+ R (>= 3.1.2)
+Imports:
+ knitr (>= 1.10),
+ magrittr,
+ stringr (>= 1.0)
+Suggests:
+ testthat
+RoxygenNote: 5.0.0
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..97676f7
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,2 @@
+YEAR: 2015
+COPYRIGHT HOLDER: Hao Zhu
diff --git a/NAMESPACE b/NAMESPACE
index d75f824..93e01c9 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -1 +1,3 @@
-exportPattern("^[[:alpha:]]+")
+# Generated by roxygen2: do not edit by hand
+
+export(add_footnote)
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/README.Rmd b/README.Rmd
index a57b769..dff12ac 100644
--- a/README.Rmd
+++ b/README.Rmd
@@ -5,4 +5,10 @@
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
-Everyone loves `kable`, so do I. Even though there are some other available packages in `R` to build beautiful tables, `kable` is still my go-to function whenever I want to build a table in `rmarkdown` or `Shiny`. After using `kable` for a long time, I can see the only reason that prevents people from using `kable` is its simplicityThis package is designed to enhance `kable`'s functionality without destorying its beauty of simplicity by using our favorite `pipe` syntax.
+#Introduction
+If CRAN allows, I really want to call this package `kable%>%`. Well, let's sit down and get back to the reality. Now you have `kableExtra`.
+
+Everyone loves `kable`, so do I. It is the most straight forward, convenient table generating function in R (in my opinion). At the same time, it won't through you some useless messages that you need to think of a way to get rid of. Also, if you are planning to "knit" a document, you probably have already have `knitr` loaded. Why would you want to load another "table generator" package if the original one can fulfill your need.
+
+
+Even though there are some other available packages in `R` to build beautiful tables, `kable` is still my go-to function whenever I want to build a table in `rmarkdown` or `Shiny`. After using `kable` for a long time, I can see the only reason that prevents people from using `kable` is its simplicityThis package is designed to enhance `kable`'s functionality without destorying its beauty of simplicity by using our favorite `pipe` syntax.
diff --git a/man/add_footnote.Rd b/man/add_footnote.Rd
new file mode 100644
index 0000000..fd8457d
--- /dev/null
+++ b/man/add_footnote.Rd
@@ -0,0 +1,26 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/footnote.R
+\name{add_footnote}
+\alias{add_footnote}
+\title{Add footnote}
+\usage{
+add_footnote(input, label = NULL, notation = "alphabet")
+}
+\arguments{
+\item{input}{The direct output of your \code{kable} function or your last
+\code{kableExtra} function.}
+
+\item{label}{A vector of footnotes you want to add. You don't need to add
+notations in your notes.}
+
+\item{notation}{You can select the format of your footnote notation from
+"number", "alphabet" and "symbol".}
+}
+\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.
+}
+
diff --git a/man/hello.Rd b/man/hello.Rd
deleted file mode 100644
index 0fa7c4b..0000000
--- a/man/hello.Rd
+++ /dev/null
@@ -1,12 +0,0 @@
-\name{hello}
-\alias{hello}
-\title{Hello, World!}
-\usage{
-hello()
-}
-\description{
-Prints 'Hello, world!'.
-}
-\examples{
-hello()
-}
diff --git a/R/test-add_footnote.r b/tests/testthat/test-add_footnote.r
similarity index 99%
rename from R/test-add_footnote.r
rename to tests/testthat/test-add_footnote.r
index a17b3ca..4a8e32c 100644
--- a/R/test-add_footnote.r
+++ b/tests/testthat/test-add_footnote.r
@@ -8,4 +8,3 @@
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")
-