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