blob: 8724141027ab6d66bdf16b1ee6df87a0a1152861 [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 Zhudb04e302015-11-15 16:57:38 -050017add_footnote <- function(input, label = NULL, notation = "alphabet", threeparttable = F) {
Hao Zhub1bc0aa2015-11-12 11:23:42 -050018 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;"
Hao Zhudb04e302015-11-15 16:57:38 -050038 ),
39 symbol.markdown = c(
40 "\\*", "†", "‡", "§", "¶",
41 "\\*\\*", "††", "‡‡", "§§", "¶¶",
42 "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶",
43 "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
44 ),
45 symbol.pandoc = c(
46 "\\*", "†", "‡", "§", "¶",
47 "\\*\\*", "††", "‡‡", "§§", "¶¶",
48 "\\*\\*\\*", "†††", "‡‡‡", "§§§", "¶¶¶",
49 "\\*\\*\\*\\*", "††††", "‡‡‡‡", "§§§§", "¶¶¶¶"
Hao Zhub1bc0aa2015-11-12 11:23:42 -050050 )
51 )
52 ids <- ids.ops[,notation]
Hao Zhudb04e302015-11-15 16:57:38 -050053 ids.intable <- gsub("\\*", "\\\\*", ids)
54
55 #count the number of items in label and intable notation
56 count.label = length(label)
57 count.intablenoot = sum(str_count(input, "\\[note\\]"))
58 if (count.intablenoot != 0 & count.label != count.intablenoot){
59 warning(paste("You entered", count.label, "labels but you put",
60 count.intablenoot, "[note] in your table."))
61 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050062
63 if(!attr(input, "format") %in% c("html", "latex")){
Hao Zhub1bc0aa2015-11-12 11:23:42 -050064 export <- input
Hao Zhudb04e302015-11-15 16:57:38 -050065 if(count.intablenoot != 0){
66 for(i in 1:count.intablenoot){
67 export[which(str_detect(export, "\\[note\\]"))[1]] <-
68 sub("\\[note\\]", paste0("^", ids.intable[i], "^",
69 paste0(rep(" ", 4 - nchar(as.character(ids[i]))),
70 collapse = "")), export[which(str_detect(export, "\\[note\\]"))[1]])
71 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -050072 }
73
Hao Zhudb04e302015-11-15 16:57:38 -050074 export[length(export)+1] <- ""
75 export[length(export)+1] <- "__Note:__"
76 export[length(export)+1] <- paste0(
77 paste0("^", ids[1:length(label)], "^ ", label), collapse = " "
78 )
79 }
80
81 # Generate latex table footnote --------------------------------
Hao Zhub1bc0aa2015-11-12 11:23:42 -050082 if(attr(input, "format")=="latex"){
Hao Zhudb04e302015-11-15 16:57:38 -050083 # If longtable is used, then use page footnote instead of threeparttable
84 # as it makes more sense to see the footnote at the bottom of page if
85 # table is longer than one page.
86 if(grepl("\\\\begin\\{longtable\\}", input)){
87
88 for(i in 1:count.intablenoot){
89 input <- sub("\\[note\\]", paste0("\\\\footnote[", ids[i], "]{", label[i], "}"), input)
90 }
91 }else{
92 # Regular cases other than longtable
93 # generate footer with appropriate symbol
94 footer <- ""
95 for(i in 1:count.label){
96 footer <- paste0(footer,"\\\\item [", ids[i], "] ", label[i], "\n")
Hao Zhub1bc0aa2015-11-12 11:23:42 -050097 }
98
Hao Zhudb04e302015-11-15 16:57:38 -050099 # Replace in-table notation with appropriate symbol
100 for(i in 1:count.intablenoot){
101 input <- sub("\\[note\\]", paste0("\\\\textsuperscript{", ids[i], "}"), input)
102 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500103
Hao Zhudb04e302015-11-15 16:57:38 -0500104 if(grepl("\\\\caption\\{.*?\\}", input)){
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500105 export <- sub("\\\\caption\\{", "\\\\begin{threeparttable}\n\\\\caption{", input)
Hao Zhudb04e302015-11-15 16:57:38 -0500106 }else{
107 export <- sub("\\\\begin\\{tabular\\}", "\\\\begin{threeparttable}\n\\\\begin{tabular}", input)
108 }
109 export <- gsub(
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500110 "\\\\end\\{tabular\\}",
111 paste0(
112 "\\\\end{tabular}\n\\\\begin{tablenotes}\n\\\\small\n",
113 footer, "\\\\end{tablenotes}\n\\\\end{threeparttable}"
Hao Zhudb04e302015-11-15 16:57:38 -0500114 ),
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500115 export)
Hao Zhudb04e302015-11-15 16:57:38 -0500116 }
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500117 }
118 if(attr(input, "format")=="html"){
Hao Zhudb04e302015-11-15 16:57:38 -0500119 export <- input
Hao Zhub1bc0aa2015-11-12 11:23:42 -0500120 }
121 return(export)
122}
Hao Zhudb04e302015-11-15 16:57:38 -0500123
124
125