blob: cca651a7bcf180b8c06b74d5feb4ea375b0480ee [file] [log] [blame]
Hao Zhub1bc0aa2015-11-12 11:23:42 -05001#' Add footnote
2#'
3#' @description Add footnote to your favorite kable output. So far this function
4#' only works when you define \code{format} in your kable function or in the
5#' global knitr option \code{knitr.table.format}. In latex, we are using the
6#' \code{threeparttable} package so you need to import this package in your
7#' \code{YAML} header.
8#'
9#' @param input The direct output of your \code{kable} function or your last
10#' \code{kableExtra} function.
11#' @param label A vector of footnotes you want to add. You don't need to add
12#' notations in your notes.
13#' @param notation You can select the format of your footnote notation from
14#' "number", "alphabet" and "symbol".
15#'
16#' @export
17add_footnote <- function(input, label = NULL, notation = "alphabet") {
18 if (is.null(label)){return(input)}
19 # Define available id list
20 if (!notation %in% c("number", "alphabet", "symbol")){
21 warning('Please select your notation within "number", "alphabet" and "symbol". Now add_footnote is using "alphabet" as default.')
22 }
23 if (notation == "symbol") {notation = paste0(notation, ".", attr(input, "format"))}
24 ids.ops <- data.frame(
25 number = as.character(1:20),
26 alphabet = letters[1:20],
27 symbol.latex = c(
28 "*", "\\\\dag", "\\\\ddag", "\\\\S", "\\\\P",
29 "**", "\\\\dag\\\\dag", "\\\\ddag\\\\ddag", "\\\\S\\\\S", "\\\\P\\\\P",
30 "***", "\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag", "\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P",
31 "****", "\\\\dag\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag\\\\ddag", "\\\\S\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P\\\\P"
32 ),
33 symbol.html = c(
34 "*", "&dagger;", "&Dagger;", "&sect;", "&para;",
35 "**", "&dagger;&dagger;", "&Dagger;&Dagger;", "&sect;&sect;", "&para;&para;",
36 "*", "&dagger;&dagger;&dagger;", "&Dagger;&Dagger;&Dagger;", "&sect;&sect;&sect;", "&para;&para;&para;",
37 "**", "&dagger;&dagger;&dagger;&dagger;", "&Dagger;&Dagger;&Dagger;&Dagger;", "&sect;&sect;&sect;&sect;", "&para;&para;&para;&para;"
38 )
39 )
40 ids <- ids.ops[,notation]
41
42 if(!attr(input, "format") %in% c("html", "latex")){
43 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`.")
44 export <- input
45 }
46
47 # Generate latex table footnote using threeparttable --------------------------------
48 if(attr(input, "format")=="latex"){
49 #count the number of items in label and intable notation
50 count.label = length(label)
51 count.intablenoot = sum(gregexpr("\\[note\\]", input)[[1]]>0)
52 if (count.intablenoot != 0 & count.label != count.intablenoot){
53 warning(paste("You entered", count.label, "labels but you put",
54 count.intablenoot, "[note] in your table."))
55 }
56
57 # generate footer with appropriate symbol
58 footer <- ""
59 for(i in 1:count.label){
60 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
61 }
62
63 # Replace in-table notation with appropriate symbol
64 for(i in 1:count.intablenoot){
65 input <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids[i], "}"), input)
66 }
67
68 #
69 if(grepl("\\\\caption\\{.*?\\}", input)){
70 if(grepl("\\\\begin\\{tabular\\}", input)){
71 export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", input)
72 }else{export <- input}
73 }else{
74 export <- sub("\\\\begin\\{tabular\\}", "\\\\begin{threeparttable}\n\\\\begin{tabular}", input)
75 }
76 export <- gsub(
77 "\\\\end\\{tabular\\}",
78 paste0(
79 "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
80 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
81 ),
82 export)
83 }
84 if(attr(input, "format")=="html"){
85
86 }
87 return(export)
88}