Hao Zhu | 2b739ac | 2020-08-15 01:38:51 -0400 | [diff] [blame] | 1 | #' Wrapper function of knitr::kable |
| 2 | #' |
| 3 | #' @description knitr's kable function is the foundation of this package. |
| 4 | #' However, it has many latex/html specific arguments hidden under the ground |
| 5 | #' unless you check its source code. This wrapper function is created to |
| 6 | #' provide better documentation (and auto-complete yay) and at the same time, |
| 7 | #' solve the auto format setting in a better way. |
| 8 | #' |
| 9 | #' @param table.attr A character string for addition HTML table attributes. |
| 10 | #' This is convenient if you simply want to add a few HTML classes or styles. |
| 11 | #' For example, you can put 'class="table" style="color: red"'. |
| 12 | #' @param booktabs T/F for whether to enable the booktabs format for tables. I |
| 13 | #' personally would recommend you turn this on for every latex table except |
| 14 | #' some special cases. |
| 15 | #' @param longtable T/F for whether to use the longtable format. If you have a |
| 16 | #' table that will span over two or more pages, you will have to turn this on. |
| 17 | #' @param valign You probably won't need to adjust this latex option very often. |
| 18 | #' If you are familar with latex tables, this is the optional position for the |
| 19 | #' tabular environment controling the vertical position of the table relative |
| 20 | #' to the baseline of the surrounding text. Possible choices are `b`, `c` and |
| 21 | #' `t` (default). |
| 22 | #' @param position This is the "real" or say floating position for the latex |
| 23 | #' table environment. The `kable` only puts tables in a table environment when |
| 24 | #' a caption is provided. That is also the reason why your tables will be |
| 25 | #' floating around if you specify captions for your table. Possible choices are |
| 26 | #' `h` (here), `t` (top, default), `b` (bottom) and `p` (on a dedicated page). |
| 27 | #' @param centering T (default)/F. Whether to center tables in the table |
| 28 | #' environment. |
| 29 | #' @param caption.short Another latex feature. Short captions for tables |
| 30 | #' @param linesep By default, in booktabs tables, `kable` insert an extra space |
| 31 | #' every five rows for clear display. If you don't want this feature or if you |
| 32 | #' want to do it in a different pattern, you can consider change this option. |
| 33 | #' The default is c('', '', '', '', '\\addlinespace'). Also, if you are not |
| 34 | #' using booktabs, but you want a cleaner display, you can change this to ''. |
| 35 | #' @param table.envir You probably don't need to change this as well. The |
| 36 | #' default setting is to put a table environment outside of tabular if a |
| 37 | #' caption is provided. |
| 38 | #' @param vline vertical separator. Default is nothing for booktabs |
| 39 | #' tables but "|" for normal tables. |
| 40 | #' @param toprule toprule. Default is hline for normal table but toprule for |
| 41 | #' booktabs tables. |
| 42 | #' @param bottomrule bottomrule. Default is hline for normal table but |
| 43 | #' bottomrule for booktabs tables. |
| 44 | #' @param midrule midrule. Default is hline for normal table but midrule for |
| 45 | #' booktabs tables. |
| 46 | #' |
| 47 | #' @inheritParams knitr::kable |
| 48 | #' @export |
| 49 | kable <- function(x, format, digits = getOption("digits"), |
| 50 | row.names = NA, col.names = NA, align, |
| 51 | caption = NULL, label = NULL, format.args = list(), |
| 52 | escape = TRUE, |
| 53 | table.attr = '', |
| 54 | booktabs = FALSE, longtable = FALSE, |
| 55 | valign = 't', position = '', centering = TRUE, |
| 56 | vline = getOption('knitr.table.vline', if (booktabs) '' else '|'), |
| 57 | toprule = getOption('knitr.table.toprule', if (booktabs) '\\toprule' else '\\hline'), |
| 58 | bottomrule = getOption('knitr.table.bottomrule', if (booktabs) '\\bottomrule' else '\\hline'), |
| 59 | midrule = getOption('knitr.table.midrule', if (booktabs) '\\midrule' else '\\hline'), |
| 60 | linesep = if (booktabs) c('', '', '', '', '\\addlinespace') else '\\hline', |
| 61 | caption.short = '', |
| 62 | table.envir = if (!is.null(caption)) 'table', ...) { |
| 63 | if (!missing(align) && length(align) == 1L && !grepl('[^lcr]', align)) { |
| 64 | align <- strsplit(align, '')[[1]] |
| 65 | } |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame] | 66 | if (missing(format) || is.null(format)) { |
Hao Zhu | 2b739ac | 2020-08-15 01:38:51 -0400 | [diff] [blame] | 67 | if (knitr::is_latex_output()) { |
| 68 | format <- "latex" |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame] | 69 | out <- knitr::kable( |
Hao Zhu | 2b739ac | 2020-08-15 01:38:51 -0400 | [diff] [blame] | 70 | x = x, format = format, digits = digits, |
| 71 | row.names = row.names, col.names = col.names, align = align, |
| 72 | caption = caption, label = label, format.args = format.args, |
| 73 | escape = escape, |
| 74 | booktabs = booktabs, longtable = longtable, |
| 75 | valign = valign, position = position, centering = centering, |
| 76 | vline = vline, toprule = toprule, bottomrule = bottomrule, |
| 77 | midrule = midrule, linesep = linesep, caption.short = caption.short, |
| 78 | table.envir = table.envir, ... |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame] | 79 | ) |
| 80 | table_info <- magic_mirror(out) |
| 81 | if (is.null(col.names)) { |
| 82 | table_info$position_offset <- 0 |
| 83 | } |
| 84 | return(out) |
Hao Zhu | 2b739ac | 2020-08-15 01:38:51 -0400 | [diff] [blame] | 85 | } else { |
| 86 | format <- "html" |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame] | 87 | out <- knitr::kable( |
Hao Zhu | 2b739ac | 2020-08-15 01:38:51 -0400 | [diff] [blame] | 88 | x = x, format = format, digits = digits, |
| 89 | row.names = row.names, col.names = col.names, align = align, |
| 90 | caption = caption, label = label, format.args = format.args, |
| 91 | escape = escape, |
| 92 | table.attr = table.attr, ... |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame] | 93 | ) |
| 94 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
| 95 | return(out) |
Hao Zhu | 2b739ac | 2020-08-15 01:38:51 -0400 | [diff] [blame] | 96 | } |
| 97 | } else { |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame] | 98 | if (format == "latex") { |
| 99 | out <- knitr::kable( |
| 100 | x = x, format = format, digits = digits, |
| 101 | row.names = row.names, col.names = col.names, align = align, |
| 102 | caption = caption, label = label, format.args = format.args, |
| 103 | escape = escape, |
| 104 | booktabs = booktabs, longtable = longtable, |
| 105 | valign = valign, position = position, centering = centering, |
| 106 | vline = vline, toprule = toprule, bottomrule = bottomrule, |
| 107 | midrule = midrule, linesep = linesep, caption.short = caption.short, |
| 108 | table.envir = table.envir, ... |
| 109 | ) |
| 110 | table_info <- magic_mirror(out) |
| 111 | if (is.null(col.names)) { |
| 112 | table_info$position_offset <- 0 |
| 113 | } |
| 114 | return(out) |
| 115 | } |
| 116 | if (format == "html") { |
| 117 | out <- knitr::kable( |
| 118 | x = x, format = format, digits = digits, |
| 119 | row.names = row.names, col.names = col.names, align = align, |
| 120 | caption = caption, label = label, format.args = format.args, |
| 121 | escape = escape, |
| 122 | table.attr = table.attr, ... |
| 123 | ) |
| 124 | if (!"kableExtra" %in% class(out)) class(out) <- c("kableExtra", class(out)) |
| 125 | return(out) |
| 126 | } |
Hao Zhu | 2b739ac | 2020-08-15 01:38:51 -0400 | [diff] [blame] | 127 | return(knitr::kable( |
| 128 | x = x, format = format, digits = digits, |
| 129 | row.names = row.names, col.names = col.names, align = align, |
| 130 | caption = caption, label = label, format.args = format.args, |
Hao Zhu | 33b865f | 2020-08-18 02:10:43 -0400 | [diff] [blame] | 131 | escape = escape, ... |
Hao Zhu | 2b739ac | 2020-08-15 01:38:51 -0400 | [diff] [blame] | 132 | )) |
| 133 | } |
| 134 | |
| 135 | |
| 136 | } |