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 | b1e2e3d | 2018-01-09 13:31:42 -0500 | [diff] [blame] | 17 | #' @examples dt <- mtcars[1:5, 1:5] |
Hao Zhu | 593f57e | 2018-01-09 13:30:01 -0500 | [diff] [blame] | 18 | #' colnames(dt)[1] <- paste0("mpg", footnote_marker_alphabet(2, "html")) |
| 19 | #' rownames(dt)[2] <- paste0(rownames(dt)[2], footnote_marker_alphabet(1, "html")) |
| 20 | #' footnote(knitr::kable(dt, "html"), alphabet = c("Note a", "Note b")) |
| 21 | #' |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 22 | #' @export |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 23 | footnote_marker_number <- function(x, format, double_escape = FALSE) { |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 24 | if (missing(format) || is.null(format)) { |
| 25 | format <- getOption('knitr.table.format') |
| 26 | } |
| 27 | if (is.null(format)) { |
| 28 | message("Setting footnote_marker_number format as html") |
| 29 | format <- "html" |
| 30 | } |
| 31 | if (format == "html") { |
| 32 | return(paste0("<sup>", x, "</sup>")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 33 | } else if (!double_escape) { |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 34 | return(paste0("\\textsuperscript{", x, "}")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 35 | } else { |
| 36 | return(paste0("\\\\textsuperscript{", x, "}")) |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 40 | #' @rdname footnote_marker_number |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 41 | #' @export |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 42 | footnote_marker_alphabet <- function(x, format, double_escape = FALSE) { |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 43 | if (missing(format) || is.null(format)) { |
| 44 | format <- getOption('knitr.table.format') |
| 45 | } |
| 46 | if (is.null(format)) { |
| 47 | message("Setting footnote_marker_alphabet format as html") |
| 48 | format <- "html" |
| 49 | } |
| 50 | if (is.numeric(x)) x <- letters[x] |
| 51 | if (format == "html") { |
| 52 | return(paste0("<sup>", x, "</sup>")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 53 | } else if (!double_escape) { |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 54 | return(paste0("\\textsuperscript{", x, "}")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 55 | } else { |
| 56 | return(paste0("\\\\textsuperscript{", x, "}")) |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Hao Zhu | e0782ab | 2018-01-09 13:24:13 -0500 | [diff] [blame] | 60 | #' @rdname footnote_marker_number |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 61 | #' @export |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 62 | footnote_marker_symbol <- function(x, format, double_escape = FALSE) { |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 63 | if (missing(format) || is.null(format)) { |
| 64 | format <- getOption('knitr.table.format') |
| 65 | } |
| 66 | if (is.null(format)) { |
| 67 | message("Setting footnote_marker_symbol format as html") |
| 68 | format <- "html" |
| 69 | } |
| 70 | number_index <- read.csv(system.file("symbol_index.csv", |
| 71 | package = "kableExtra")) |
| 72 | if (format == "html") { |
| 73 | x <- number_index$symbol.html[x] |
| 74 | return(paste0("<sup>", x, "</sup>")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 75 | } else if (!double_escape) { |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 76 | x <- number_index$symbol.latex[x] |
Hao Zhu | e809c56 | 2018-03-26 17:31:55 -0400 | [diff] [blame] | 77 | x <- gsub("\\\\\\\\", "\\\\", x) |
Hao Zhu | 19c4fa5 | 2018-01-09 12:01:14 -0500 | [diff] [blame] | 78 | return(paste0("\\textsuperscript{", x, "}")) |
Hao Zhu | f94a26f | 2018-04-05 17:42:55 -0400 | [diff] [blame] | 79 | } else { |
| 80 | x <- number_index$symbol.latex[x] |
| 81 | return(paste0("\\\\textsuperscript{", x, "}")) |
Hao Zhu | 1ac13ad | 2018-01-08 16:12:24 -0500 | [diff] [blame] | 82 | } |
| 83 | } |