| 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 | } | 
|  | 66 | if (missing(format) | is.null(format)) { | 
|  | 67 | if (knitr::is_latex_output()) { | 
|  | 68 | format <- "latex" | 
|  | 69 | return(knitr::kable( | 
|  | 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, ... | 
|  | 79 | )) | 
|  | 80 | } else { | 
|  | 81 | format <- "html" | 
|  | 82 | return(knitr::kable( | 
|  | 83 | x = x, format = format, digits = digits, | 
|  | 84 | row.names = row.names, col.names = col.names, align = align, | 
|  | 85 | caption = caption, label = label, format.args = format.args, | 
|  | 86 | escape = escape, | 
|  | 87 | table.attr = table.attr, ... | 
|  | 88 | )) | 
|  | 89 | } | 
|  | 90 | } else { | 
|  | 91 | return(knitr::kable( | 
|  | 92 | x = x, format = format, digits = digits, | 
|  | 93 | row.names = row.names, col.names = col.names, align = align, | 
|  | 94 | caption = caption, label = label, format.args = format.args, | 
|  | 95 | escape = escape, table.attr = table.attr, | 
|  | 96 | booktabs = booktabs, longtable = longtable, | 
|  | 97 | valign = valign, position = position, centering = centering, | 
|  | 98 | vline = vline, toprule = toprule, bottomrule = bottomrule, | 
|  | 99 | midrule = midrule, linesep = linesep, caption.short = caption.short, | 
|  | 100 | table.envir = table.envir, ... | 
|  | 101 | )) | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 |  | 
|  | 105 | } |