Hao Zhu | cdd7f92 | 2018-01-08 11:39:40 -0500 | [diff] [blame] | 1 | #' Footnote marker |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 2 | #' |
| 3 | #' @description Put footnote mark in superscription in table. Unless you are |
| 4 | #' using it in the `caption` of `kable`, you will need to put `escape = F` |
| 5 | #' in `kable` (similar with `cell_spec`). Again, similar with `cell_spec`, the |
| 6 | #' `format` option here can read default value from global option |
| 7 | #' `knitr.table.format`. |
| 8 | #' |
| 9 | #' @param x a number. For example, for `footnote_marker_alphabet(2)` will |
| 10 | #' return "b" in HTML. |
| 11 | #' @param format Either `html` or `latex`. All functions here can read |
| 12 | #' default value from global option `knitr.table.format`. |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 13 | #' @param double_escape T/F if output is in LaTeX, whether it should be double |
| 14 | #' escaped. If you are using footnote_marker in `group_rows`` labeling row or |
| 15 | #' `add_header_above`, you need to set this to be `TRUE`. |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 16 | #' |
Hao Zhu | 9399dcc | 2020-08-26 17:27:38 -0400 | [diff] [blame^] | 17 | #' @examples |
| 18 | #' \dontrun{ |
| 19 | #' dt <- mtcars[1:5, 1:5] |
Hao Zhu | 593f57e | 2018-01-09 13:30:01 -0500 | [diff] [blame] | 20 | #' colnames(dt)[1] <- paste0("mpg", footnote_marker_alphabet(2, "html")) |
| 21 | #' rownames(dt)[2] <- paste0(rownames(dt)[2], footnote_marker_alphabet(1, "html")) |
| 22 | #' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b")) |
Hao Zhu | 9399dcc | 2020-08-26 17:27:38 -0400 | [diff] [blame^] | 23 | #' } |
Hao Zhu | 593f57e | 2018-01-09 13:30:01 -0500 | [diff] [blame] | 24 | #' |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 25 | #' @export |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 26 | footnote_marker_number <- function(x, format, double_escape = FALSE) { |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 27 | if (missing(format) || is.null(format)) { |
| 28 | format <- getOption('knitr.table.format') |
| 29 | } |
| 30 | if (is.null(format)) { |
| 31 | message("Setting footnote_marker_number format as html") |
| 32 | format <- "html" |
| 33 | } |
| 34 | if (format == "html") { |
| 35 | return(paste0("<sup>", x, "</sup>")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 36 | } else if (!double_escape) { |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 37 | return(paste0("\\textsuperscript{", x, "}")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 38 | } else { |
| 39 | return(paste0("\\\\textsuperscript{", x, "}")) |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 43 | #' @rdname footnote_marker_number |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 44 | #' @export |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 45 | footnote_marker_alphabet <- function(x, format, double_escape = FALSE) { |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 46 | if (missing(format) || is.null(format)) { |
| 47 | format <- getOption('knitr.table.format') |
| 48 | } |
| 49 | if (is.null(format)) { |
| 50 | message("Setting footnote_marker_alphabet format as html") |
| 51 | format <- "html" |
| 52 | } |
| 53 | if (is.numeric(x)) x <- letters[x] |
| 54 | if (format == "html") { |
| 55 | return(paste0("<sup>", x, "</sup>")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 56 | } else if (!double_escape) { |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 57 | return(paste0("\\textsuperscript{", x, "}")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 58 | } else { |
| 59 | return(paste0("\\\\textsuperscript{", x, "}")) |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 63 | #' @rdname footnote_marker_number |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 64 | #' @export |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 65 | footnote_marker_symbol <- function(x, format, double_escape = FALSE) { |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 66 | if (missing(format) || is.null(format)) { |
| 67 | format <- getOption('knitr.table.format') |
| 68 | } |
| 69 | if (is.null(format)) { |
| 70 | message("Setting footnote_marker_symbol format as html") |
| 71 | format <- "html" |
| 72 | } |
| 73 | number_index <- read.csv(system.file("symbol_index.csv", |
| 74 | package = "kableExtra")) |
| 75 | if (format == "html") { |
| 76 | x <- number_index$symbol.html[x] |
| 77 | return(paste0("<sup>", x, "</sup>")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 78 | } else if (!double_escape) { |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 79 | x <- number_index$symbol.latex[x] |
Hao Zhu | e809c56 | 2018-03-26 17:31:55 -0400 | [diff] [blame] | 80 | x <- gsub("\\\\\\\\", "\\\\", x) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 81 | return(paste0("\\textsuperscript{", x, "}")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 82 | } else { |
| 83 | x <- number_index$symbol.latex[x] |
| 84 | return(paste0("\\\\textsuperscript{", x, "}")) |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 85 | } |
| 86 | } |