| 1 |
library(testit) |
| 2 |
|
| 3 |
kable2 = function(...) as.character(kable(...)) |
| 4 |
|
| 5 |
assert('kable() works on data frames/matrices of one row', { |
| 6 |
(kable2(data.frame(x = 1, y = 1), format = 'pipe') %==% |
| 7 |
c('| x| y|', '|--:|--:|', '| 1| 1|')) |
| 8 |
}) |
| 9 |
|
| 10 |
m = matrix(1:2, nrow = 1, dimnames = list('a', c('x', 'y'))) |
| 11 |
assert('kable() does not discard row names when there is only one row', { |
| 12 |
(kable2(m) %==% c('| | x| y|', '|:--|--:|--:|', '|a | 1| 2|')) |
| 13 |
}) |
| 14 |
|
| 15 |
assert('kable() can assign a column name for row names', { |
| 16 |
(kable2(m, col.names = c('z', colnames(m))) %==% |
| 17 |
c('|z | x| y|', '|:--|--:|--:|', '|a | 1| 2|')) |
| 18 |
}) |
| 19 |
|
| 20 |
assert('kable() recycles the align argument correctly', { |
| 21 |
(kable2(m, align = 'c') %==% |
| 22 |
c('| | x | y |', '|:--|:-:|:-:|', '|a | 1 | 2 |')) |
| 23 |
}) |
| 24 |
|
| 25 |
assert('kable() align with strings correctly', { |
| 26 |
(kable2(m, align = c('c', 'r')) %==% kable2(m, align = 'cr')) |
| 27 |
}) |
| 28 |
|
| 29 |
assert('kable() works on character data frames', { |
| 30 |
(kable2(data.frame(x = 'a')) %==% c('|x |', '|:--|', '|a |')) |
| 31 |
}) |
| 32 |
|
| 33 |
assert("kable() works on NA's", { |
| 34 |
(kable2(data.frame(x = c(NA, FALSE))) %==% |
| 35 |
c('|x |', '|:-----|', '|NA |', '|FALSE |')) |
| 36 |
}) |
| 37 |
|
| 38 |
assert('kable() works with the jira format', { |
| 39 |
(kable2(m, 'jira') %==% c('|| || x|| y||', '|a | 1| 2|')) |
| 40 |
}) |
| 41 |
|
| 42 |
assert('kable() works with the org format', { |
| 43 |
(kable2(m, 'org') %==% |
| 44 |
c('| | x| y|', '|<l>|<r>|<r>|', '|---+---+---|', '|a | 1| 2|')) |
| 45 |
m2 = m; colnames(m2) = NULL # no column names |
| 46 |
(kable2(m2, 'org') %==% c('|<l>|<r>|<r>|', '|a | 1| 2|')) |
| 47 |
# the case of only one column |
| 48 |
(kable2(m[, 1, drop = FALSE], 'org', row.names = FALSE) %==% |
| 49 |
c('| x|', '|<r>|', '|---|', '| 1|')) |
| 50 |
}) |
| 51 |
|
| 52 |
assert('kable() does not add extra spaces to character columns', { |
| 53 |
(kable2(data.frame(x = c(1.2, 4.87), y = c('fooooo', 'bar')), 'latex') %==% ' |
| 54 |
\\begin{tabular}{r|l} |
| 55 |
\\hline |
| 56 |
x & y\\\\ |
| 57 |
\\hline |
| 58 |
1.20 & fooooo\\\\ |
| 59 |
\\hline |
| 60 |
4.87 & bar\\\\ |
| 61 |
\\hline |
| 62 |
\\end{tabular}') |
| 63 |
}) |
| 64 |
|
| 65 |
assert('kable() escapes LaTeX special characters by default', { |
| 66 |
(kable2(data.frame(x = c('10%', '5%'), col_name = c('3_8', '40_6')), 'latex') %==% ' |
| 67 |
\\begin{tabular}{l|l} |
| 68 |
\\hline |
| 69 |
x & col\\_name\\\\ |
| 70 |
\\hline |
| 71 |
10\\% & 3\\_8\\\\ |
| 72 |
\\hline |
| 73 |
5\\% & 40\\_6\\\\ |
| 74 |
\\hline |
| 75 |
\\end{tabular}') |
| 76 |
}) |
| 77 |
|
| 78 |
assert("kable() doesn't escape LaTeX special characters when escape = FALSE", { |
| 79 |
(kable2(data.frame(x = c('10%', '5%'), col_name = c('3_8', '40_6')), 'latex', escape = FALSE) %==% ' |
| 80 |
\\begin{tabular}{l|l} |
| 81 |
\\hline |
| 82 |
x & col_name\\\\ |
| 83 |
\\hline |
| 84 |
10% & 3_8\\\\ |
| 85 |
\\hline |
| 86 |
5% & 40_6\\\\ |
| 87 |
\\hline |
| 88 |
\\end{tabular}' |
| 89 |
) |
| 90 |
}) |
| 91 |
|
| 92 |
assert('kable() adds {} before [] when booktabs = TRUE', { |
| 93 |
(kable2(data.frame(x = c('[0, 1]', '(1, 2]'), y = c(35, 62)), 'latex', booktabs = TRUE) %==% ' |
| 94 |
\\begin{tabular}{lr} |
| 95 |
\\toprule |
| 96 |
x & y\\\\ |
| 97 |
\\midrule |
| 98 |
{}[0, 1] & 35\\\\ |
| 99 |
(1, 2] & 62\\\\ |
| 100 |
\\bottomrule |
| 101 |
\\end{tabular}' |
| 102 |
) |
| 103 |
}) |
| 104 |
|
| 105 |
assert('kable(format = "latex", linesep = ...) works', { |
| 106 |
(kable2(data.frame(x = 1:4), 'latex', linesep = c('', '', '\\midrule')) %==% ' |
| 107 |
\\begin{tabular}{r} |
| 108 |
\\hline |
| 109 |
x\\\\ |
| 110 |
\\hline |
| 111 |
1\\\\ |
| 112 |
2\\\\ |
| 113 |
3\\\\ |
| 114 |
\\midrule |
| 115 |
4\\\\ |
| 116 |
\\hline |
| 117 |
\\end{tabular}') |
| 118 |
}) |
| 119 |
|
| 120 |
assert('kable() does not trim escaped spaces at the end', { |
| 121 |
(kable2(data.frame(x = '\\ '), 'latex', escape =FALSE ) %==% ' |
| 122 |
\\begin{tabular}{l} |
| 123 |
\\hline |
| 124 |
x\\\\ |
| 125 |
\\hline |
| 126 |
\\ \\\\ |
| 127 |
\\hline |
| 128 |
\\end{tabular}') |
| 129 |
}) |
| 130 |
|
| 131 |
assert('kable() escapes HTML special characters by default', { |
| 132 |
(kable2(data.frame(x = c('10<>', '5&2'), y = c('3>8', '"40"')), 'html') %==% |
| 133 |
'<table>\n <thead>\n <tr>\n <th style="text-align:left;"> x </th>\n <th style="text-align:left;"> y </th>\n </tr>\n </thead>\n<tbody>\n <tr>\n <td style="text-align:left;"> 10<> </td>\n <td style="text-align:left;"> 3>8 </td>\n </tr>\n <tr>\n <td style="text-align:left;"> 5&2 </td>\n <td style="text-align:left;"> "40" </td>\n </tr>\n</tbody>\n</table>') |
| 134 |
}) |
| 135 |
|
| 136 |
assert('kable() doesn\'t escape HTML special characters when escape = FALSE', { |
| 137 |
(kable2(data.frame(x = c('10<>', '5&2'), y = c('3>8', '"40"')), 'html', escape = FALSE) %==% |
| 138 |
'<table>\n <thead>\n <tr>\n <th style="text-align:left;"> x </th>\n <th style="text-align:left;"> y </th>\n </tr>\n </thead>\n<tbody>\n <tr>\n <td style="text-align:left;"> 10<> </td>\n <td style="text-align:left;"> 3>8 </td>\n </tr>\n <tr>\n <td style="text-align:left;"> 5&2 </td>\n <td style="text-align:left;"> "40" </td>\n </tr>\n</tbody>\n</table>') |
| 139 |
}) |
| 140 |
|
| 141 |
assert('kable(digits = vector) works on numeric matrices', { |
| 142 |
(kable2(matrix(c(1.1, 1.2, 2.3, 2.4), 2, dimnames = list(NULL, c('a', 'b'))), digits = c(0, 1)) %==% |
| 143 |
c('| a| b|', '|--:|---:|', '| 1| 2.3|', '| 1| 2.4|')) |
| 144 |
}) |
| 145 |
|
| 146 |
assert('kable() works on matrices with duplicate row names', { |
| 147 |
(kable2(matrix(c(1, 1, 1, 1), ncol = 2, dimnames = list(c('A', 'A'), c('B', 'B')))) %==% |
| 148 |
c('| | B| B|', '|:--|--:|--:|', '|A | 1| 1|', '|A | 1| 1|')) |
| 149 |
}) |
| 150 |
|
| 151 |
assert('kable() works on matrices with NA colname', { |
| 152 |
(kable2(matrix(c(1, 1, 1, 1), ncol = 2, dimnames = list(c('A', NA), c('B', NA)))) %==% |
| 153 |
c('| | B| NA|', '|:--|--:|--:|', '|A | 1| 1|', '|NA | 1| 1|')) |
| 154 |
}) |
| 155 |
|
| 156 |
# edge cases (should not error) |
| 157 |
x1 = matrix(NA, 0, 0) |
| 158 |
x2 = matrix(NA, 0, 1) |
| 159 |
x3 = matrix(NA, 1, 0) |
| 160 |
for (f in c('simple', 'html', 'latex', 'rst', 'jira', 'org')) { |
| 161 |
kable(x1, f) |
| 162 |
kable(x2, f) |
| 163 |
kable(x3, f) |
| 164 |
} |
| 165 |
|
| 166 |
colnames(x2) = 'a' |
| 167 |
assert('kable(, "pipe") works for a 0 row 1 column matrix', { |
| 168 |
(kable2(x2, 'pipe') %==% c('|a |', '|:--|')) |
| 169 |
}) |
| 170 |
|
| 171 |
assert('kable(, "pipe") works for a 1-column matrix without column names', { |
| 172 |
(kable2(matrix(1), 'pipe') %==% c('| |', '|--:|', '| 1|')) |
| 173 |
}) |
| 174 |
|
| 175 |
assert('kable(, "simple") generates a pipe table for a 0 row data.frame', { |
| 176 |
(kable2(data.frame(x = character(0), y = integer(0)), 'simple') %==% |
| 177 |
c('|x | y|', '|:--|--:|')) |
| 178 |
}) |
| 179 |
|
| 180 |
assert('kable(, "simple") generates a simple table for a 1-column object', { |
| 181 |
# if the object has column names, indent the table by one space |
| 182 |
(kable2(data.frame(x = 1), 'simple') %==% c(' x', ' ---', ' 1')) |
| 183 |
# if it doesn't have column names, generate the usual table (without indent) |
| 184 |
(kable2(matrix(1), 'simple') %==% c('---', ' 1', '---')) |
| 185 |
}) |
| 186 |
|
| 187 |
assert('kable(, "simple", caption = "Table Caption") works for a 1-column matrix', { |
| 188 |
x4 = matrix(1:2, ncol = 1, dimnames = list(NULL, 'a')) |
| 189 |
(kable2(x4, 'simple', caption = 'Table Caption') %==% |
| 190 |
c('Table: Table Caption', '', ' a', ' ---', ' 1', ' 2')) |
| 191 |
}) |
| 192 |
|
| 193 |
assert('kable() works on an irregular matrix', { |
| 194 |
(kable2(matrix(list('a', 2, 3, 4), nrow = 2), col.names = c('a', 'b')) %==% |
| 195 |
c('|a |b |', '|:--|:--|', '|a |3 |', '|2 |4 |')) |
| 196 |
}) |
| 197 |
|
| 198 |
assert('has_rownames() works', { |
| 199 |
(!has_rownames(matrix(1:4, 2))) |
| 200 |
(!has_rownames(iris)) |
| 201 |
(has_rownames(mtcars)) |
| 202 |
(!has_rownames(as.data.frame(matrix(nrow = 0, ncol = 3)))) |
| 203 |
}) |
| 204 |
|
| 205 |
op = options(knitr.kable.NA = '') |
| 206 |
assert('kable() can display NA as empty strings', { |
| 207 |
(kable2(matrix(c(1, NA, 3, 4), nrow = 2), col.names = c('a', 'b')) %==% |
| 208 |
c('| a| b|', '|--:|--:|', '| 1| 3|', '| | 4|')) |
| 209 |
}) |
| 210 |
options(op) |
| 211 |
|
| 212 |
assert('kable() can apply formatting to custom objects', { |
| 213 |
d = tibble::tibble(x = structure( |
| 214 |
'print_me', .to_upper = TRUE, class = 'make_upper' |
| 215 |
)) |
| 216 |
|
| 217 |
format.make_upper = function(x, ...) { |
| 218 |
xout = unclass(x) |
| 219 |
if (isTRUE(attr(x, '.to_upper'))) { |
| 220 |
xout = toupper(xout) |
| 221 |
} |
| 222 |
as.character(xout) |
| 223 |
} |
| 224 |
registerS3method('format', 'make_upper', format.make_upper, environment(format)) |
| 225 |
|
| 226 |
(kable2(d) %==% c('|x |', '|:--------|', '|PRINT_ME |')) |
| 227 |
}) |