blob: e35a9f19be5ebeff9e1b6d2ea46030a27010843d [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
Hao Zhu4adea852015-11-16 16:38:34 -050017add_footnote <- function(input, label = NULL, notation = "alphabet",
18 threeparttable = F) {
Hao Zhub1bc0aa2015-11-12 11:23:42 -050019 if (is.null(label)){return(input)}
20 # Define available id list
21 if (!notation %in% c("number", "alphabet", "symbol")){
Hao Zhu4adea852015-11-16 16:38:34 -050022 warning('Please select your notation within "number", "alphabet" and ',
23 '"symbol". Now add_footnote is using "alphabet" as default.')
Hao Zhub1bc0aa2015-11-12 11:23:42 -050024 }
25 if (notation == "symbol") {notation = paste0(notation, ".", attr(input, "format"))}
26 ids.ops <- data.frame(
27 number = as.character(1:20),
28 alphabet = letters[1:20],
29 symbol.latex = c(
30 "*", "\\\\dag", "\\\\ddag", "\\\\S", "\\\\P",
31 "**", "\\\\dag\\\\dag", "\\\\ddag\\\\ddag", "\\\\S\\\\S", "\\\\P\\\\P",
Hao Zhu4adea852015-11-16 16:38:34 -050032 "***", "\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag",
33 "\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P",
34 "****", "\\\\dag\\\\dag\\\\dag\\\\dag", "\\\\ddag\\\\ddag\\\\ddag\\\\ddag",
35 "\\\\S\\\\S\\\\S\\\\S", "\\\\P\\\\P\\\\P\\\\P"
Hao Zhub1bc0aa2015-11-12 11:23:42 -050036 ),
37 symbol.html = c(
38 "*", "&dagger;", "&Dagger;", "&sect;", "&para;",
39 "**", "&dagger;&dagger;", "&Dagger;&Dagger;", "&sect;&sect;", "&para;&para;",
Hao Zhu4adea852015-11-16 16:38:34 -050040 "*", "&dagger;&dagger;&dagger;", "&Dagger;&Dagger;&Dagger;",
41 "&sect;&sect;&sect;", "&para;&para;&para;",
42 "**", "&dagger;&dagger;&dagger;&dagger;", "&Dagger;&Dagger;&Dagger;&Dagger;",
43 "&sect;&sect;&sect;&sect;", "&para;&para;&para;&para;"
Hao Zhudb04e302015-11-15 16:57:38 -050044 ),
45 symbol.markdown = c(
46 "\\*", "†", "‡", "§", "¶",
47 "\\*\\*", "††", "‡‡", "§§", "¶¶",
48 "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶",
49 "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
50 ),
51 symbol.pandoc = c(
52 "\\*", "†", "‡", "§", "¶",
53 "\\*\\*", "††", "‡‡", "§§", "¶¶",
54 "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶",
55 "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
Hao Zhub1bc0aa2015-11-12 11:23:42 -050056 )
57 )
58 ids <- ids.ops[,notation]
Hao Zhu4adea852015-11-16 16:38:34 -050059 # pandoc cannot recognize ^*^ as * is a special character. We have to use ^\*^
Hao Zhudb04e302015-11-15 16:57:38 -050060 ids.intable <- gsub("\\*", "\\\\*", ids)
61
62 #count the number of items in label and intable notation
63 count.label = length(label)
64 count.intablenoot = sum(str_count(input, "\\[note\\]"))
65 if (count.intablenoot != 0 & count.label != count.intablenoot){
66 warning(paste("You entered", count.label, "labels but you put",
67 count.intablenoot, "[note] in your table."))
68 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050069
Hao Zhu4adea852015-11-16 16:38:34 -050070 export <- input
71
72 # Footnote solution for markdown and pandoc. It is not perfect as
73 # markdown doesn't support complex table formats but this solution
74 # should be able to satisfy people who don't want to spend extra
75 # time to define their `kable` output.
Hao Zhub1bc0aa2015-11-12 11:23:42 -050076 if(!attr(input, "format") %in% c("html", "latex")){
Hao Zhu4adea852015-11-16 16:38:34 -050077 # In table notation
Hao Zhudb04e302015-11-15 16:57:38 -050078 if(count.intablenoot != 0){
79 for(i in 1:count.intablenoot){
80 export[which(str_detect(export, "\\[note\\]"))[1]] <-
81 sub("\\[note\\]", paste0("^", ids.intable[i], "^",
82 paste0(rep(" ", 4 - nchar(as.character(ids[i]))),
83 collapse = "")), export[which(str_detect(export, "\\[note\\]"))[1]])
84 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050085 }
Hao Zhu4adea852015-11-16 16:38:34 -050086 # Fix extra in table notation
87 extra.notation <- as.numeric(
88 str_extract(
89 str_extract_all(
90 paste0(export, collapse = ""), "\\[note[0-9]{1,2}\\]"
91 )[[1]],
92 "[0-9]{1,2}"))
93 for(i in extra.notation){
94 export <- gsub(paste0("\\[note", i, "\\]"),
95 paste0("^", ids.intable[i], "^",
96 paste0(rep(" ", 4 - nchar(as.character(ids[i]))),
97 collapse = "")),
98 export)
99 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500100
Hao Zhudb04e302015-11-15 16:57:38 -0500101 export[length(export)+1] <- ""
102 export[length(export)+1] <- "__Note:__"
103 export[length(export)+1] <- paste0(
104 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
105 )
106 }
107
108 # Generate latex table footnote --------------------------------
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500109 if(attr(input, "format")=="latex"){
Hao Zhu4adea852015-11-16 16:38:34 -0500110 kable_info <- magic_mirror(input)
111 if(threeparttable == F | latex.tabular == "longtable"){
112
113 }
Hao Zhudb04e302015-11-15 16:57:38 -0500114 # If longtable is used, then use page footnote instead of threeparttable
115 # as it makes more sense to see the footnote at the bottom of page if
116 # table is longer than one page.
117 if(grepl("\\\\begin\\{longtable\\}", input)){
Hao Zhudb04e302015-11-15 16:57:38 -0500118 for(i in 1:count.intablenoot){
Hao Zhu4adea852015-11-16 16:38:34 -0500119 export <- sub("\\[note\\]",
120 paste0("\\\\footnote[", ids[i], "]{", label[i], "}"), export)
Hao Zhudb04e302015-11-15 16:57:38 -0500121 }
122 }else{
123 # Regular cases other than longtable
124 # generate footer with appropriate symbol
125 footer <- ""
126 for(i in 1:count.label){
127 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500128 }
129
Hao Zhudb04e302015-11-15 16:57:38 -0500130 # Replace in-table notation with appropriate symbol
131 for(i in 1:count.intablenoot){
Hao Zhu4adea852015-11-16 16:38:34 -0500132 export <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids[i], "}"), export)
Hao Zhudb04e302015-11-15 16:57:38 -0500133 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500134
Hao Zhu4adea852015-11-16 16:38:34 -0500135 if(grepl("\\\\caption\\{.*?\\}", export)){
136 export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", export)
Hao Zhudb04e302015-11-15 16:57:38 -0500137 }else{
Hao Zhu4adea852015-11-16 16:38:34 -0500138 export <- sub("\\\\begin\\{tabular\\}",
139 "\\\\begin{threeparttable}\n\\\\begin{tabular}", export)
Hao Zhudb04e302015-11-15 16:57:38 -0500140 }
141 export <- gsub(
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500142 "\\\\end\\{tabular\\}",
143 paste0(
144 "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
145 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
Hao Zhudb04e302015-11-15 16:57:38 -0500146 ),
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500147 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500148 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500149 }
150 if(attr(input, "format")=="html"){
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500151 }
152 return(export)
153}
Hao Zhudb04e302015-11-15 16:57:38 -0500154
155
156