Debian Codegraph @

inst/tests/test-S3.R

raw
1 library(testthat)
2 library(pander)
3
4 ## reset later
5 original_contrasts_options <- getOption('contrasts')
6
7 context('pandoc.table')
8
9 tables <- list(
10 mtcars,
11 mtcars$am,
12 mtcars[1:2, ],
13 mtcars[1:2, 5],
14 summary(mtcars$am),
15 table(mtcars$am) + 0.1,
16 table(mtcars$am, mtcars$gear) + 0.1,
17 summary(lm(mtcars$hp~1))$coeff,
18 table(mtcars$am, mtcars$gear, mtcars$carb),
19 addmargins(table(mtcars$gear, mtcars$carb)),
20 summary(Sys.Date() + 0:5)
21 )
22
23 test_that('no error: multiline', {
24 for (t in tables)
25 expect_that(pandoc.table.return(t, style = 'multiline'), is_a('character'))
26 })
27 test_that('no error: simple', {
28 for (t in tables)
29 expect_that(pandoc.table.return(t, style = 'simple'), is_a('character'))
30 })
31 test_that('no error: grid', {
32 for (t in tables)
33 expect_that(pandoc.table.return(t, style = 'grid'), is_a('character'))
34 })
35 test_that('no error: rmarkdown', {
36 for (t in tables)
37 expect_that(pandoc.table.return(t, style = 'rmarkdown'), is_a('character'))
38 })
39
40 tables <- list(
41 mtcars,
42 mtcars$am,
43 mtcars[1:2, ],
44 mtcars[1:2, 5],
45 summary(mtcars$am),
46 table(mtcars$am) + 0.1,
47 table(mtcars$am, mtcars$gear) + 0.1,
48 lm(mtcars$hp~1),
49 t.test(extra ~ group, data = sleep),
50 prcomp(USArrests),
51 density(mtcars$hp),
52 table(mtcars$am, mtcars$gear, mtcars$carb) + 0.1
53 )
54
55 test_that('split.tables', {
56 # test exact values (#164)
57 t <- data.frame(a = '7 chars', b = paste(rep('Δ', 5), collapse = ' '))
58 expect_equal(nchar('Δ Δ Δ Δ Δ', type = 'width'), 9)
59 res_grid <- pander_return(t, style = 'grid', split.tables = 23)
60 expect_equal(length(res_grid), 8)
61 expect_false(any(grepl('Table', res_grid)))
62 res_grid <- pander_return(t, style = 'grid', split.tables = 22)
63 expect_equal(length(res_grid), 18)
64 expect_true(any(grepl('Table', res_grid)))
65 res_simple <- pander_return(t, style = 'simple', split.tables = 21)
66 expect_equal(length(res_simple), 6)
67 expect_false(any(grepl('Table', res_simple)))
68 res_simple <- pander_return(t, style = 'simple', split.tables = 20)
69 expect_equal(length(res_simple), 14)
70 expect_true(any(grepl('Table', res_simple)))
71 })
72
73 test_that('rmarkdown pipe-delimited table is correct (#186)', {
74 d <- data.frame(a = 'foo|bar', b = 'my missing cell')
75 res <- pander_return(d, style = 'rmarkdown')
76 expect_true(any(grep('foo', res)))
77 expect_true(any(grep('bar', res)))
78 expect_true(any(grep('my missing cell', res)))
79 rownames(d) <- 'x|y'
80 res <- pander_return(d, style = 'rmarkdown')
81 expect_true(any(grep('x', res)))
82 expect_true(any(grep('y', res)))
83 expect_true(any(grep('foo', res)))
84 expect_true(any(grep('bar', res)))
85 expect_true(any(grep('my missing cell', res)))
86 })
87
88 test_that('pandoc.table.return behaves correctly', {
89 expect_warning(pander_return(mtcars[, 1:2], split.tables = Inf, split.cells = c('50%', '50%')))
90 t <- mtcars[1:3, 1:2]
91 attr(t, 'alignment') <- 'left'
92 attr(t, 'caption') <- 'simplified mtcars'
93 res <- pander_return(t, emphasize.rownames = FALSE)
94 expect_false(any(grep('^[[:space:]]', res))) # because of left alignment
95 expect_true(any(grep('simplified mtcars', res)))
96 expect_error(pander_return(t, justify = c('left', 'right'))) # needs 3 because of rownames
97 expect_error(pander_return(t, justify = 'lr')) # needs 3 because of rownames
98 res <- pander_return(t, justify = 'lrr')
99 expect_false(any(grep('[[:space:]]$', res)))
100 expect_error(pander_return(t, justify = 'laft'))
101 res <- pander_return(t, split.tables = 1)
102 expect_equal(length(res), 24)
103 res <- pander_return(t, split.cells = c(10, 10, 10))
104 expect_equal(length(res), 16)
105 expect_warning(pander_return(t, split.cells = vector()))
106 })
107
108 dm <- panderOptions('decimal.mark')
109 panderOptions('decimal.mark', ',')
110 test_that('decimal mark', {
111 for (t in tables)
112 expect_true(grepl(',', paste(pander_return(t), collapse = '\n')))
113 })
114
115 panderOptions('decimal.mark', dm)
116
117 test_that('Date is preserved', {
118 expect_equal(grep('2015', pander_return(summary(as.Date('2015-01-01') + 0:5))), 5)
119 })
120
121 context('highlight tables')
122
123 t <- mtcars[1:3, 1:5]
124 test_that('highlight 1D: no error', {
125 expect_that(pandoc.table.return(t$mpg, emphasize.italics.cells = 1), is_a('character'))
126 expect_that(pandoc.table.return(t$mpg, emphasize.italics.cells = 1:2), is_a('character'))
127 expect_that(pandoc.table.return(t$mpg, emphasize.strong.cells = 1), is_a('character'))
128 expect_that(pandoc.table.return(t$mpg, emphasize.strong.cells = 1:2), is_a('character'))
129 })
130
131 t <- table(mtcars$am, mtcars$gear)
132 test_that('emphasize 2D: no error', {
133 expect_that(pandoc.table.return(t, emphasize.italics.rows = 1), is_a('character'))
134 expect_that(pandoc.table.return(t, emphasize.italics.rows = 1:2), is_a('character'))
135 expect_that(pandoc.table.return(t, emphasize.italics.cols = 1), is_a('character'))
136 expect_that(pandoc.table.return(t, emphasize.italics.cols = 1:2), is_a('character'))
137 expect_that(pandoc.table.return(t, emphasize.italics.cells = which(t > 10, arr.ind = TRUE)), is_a('character'))
138 expect_that(pandoc.table.return(t, emphasize.italics.cells = which(t > 20, arr.ind = TRUE)), is_a('character'))
139 expect_that(pandoc.table.return(t, emphasize.strong.rows = 1), is_a('character'))
140 expect_that(pandoc.table.return(t, emphasize.strong.rows = 1:2), is_a('character'))
141 expect_that(pandoc.table.return(t, emphasize.strong.cols = 1), is_a('character'))
142 expect_that(pandoc.table.return(t, emphasize.strong.cols = 1:2), is_a('character'))
143 expect_that(pandoc.table.return(t, emphasize.strong.cells = which(t > 10, arr.ind = TRUE)), is_a('character'))
144 expect_that(pandoc.table.return(t, emphasize.strong.cells = which(t > 20, arr.ind = TRUE)), is_a('character'))
145 })
146
147 test_that('emphasize: error', {
148 expect_that(pandoc.table(t, emphasize.italics.cols = 1:5), throws_error())
149 expect_that(pandoc.table(t, emphasize.italics.cols = 1.5), throws_error())
150 expect_that(pandoc.table(t, emphasize.strong.cols = 1:5), throws_error())
151 expect_that(pandoc.table(t, emphasize.strong.cols = 1.5), throws_error())
152 })
153
154 test_that('no warning for highlight NA/empty strings', {
155 expect_warning(pandoc.table(data.frame(x = 1:2, y = c(1, NA)), emphasize.italics.cols = 2), regexp = NA)
156 })
157
158 test_that('emphasize.italics.rows works correctly', {
159 # test for issue 176
160 df <- data.frame(a=1:3, b=1:3, c=1:3)
161 res <- pander_return(df, emphasize.italics.rows = c(1,2), style = 'simple')
162 expect_equal(res[5], ' *1* *1* *1* ')
163 expect_equal(res[6], ' *2* *2* *2* ')
164 res <- pander_return(df, emphasize.strong.rows = c(1,2), style = 'simple')
165 expect_equal(res[5], ' **1** **1** **1** ')
166 expect_equal(res[6], ' **2** **2** **2** ')
167 })
168
169 test_that('emphasize.verbatim works correctly', {
170 df <- data.frame(a = 1:3, b = 4:6, c = 7:9)
171 res <- pander_return(df, emphasize.verbatim.rows = c(1, 2),
172 style = 'simple')
173 expect_equal(res[5], ' `1` `4` `7` ')
174 expect_equal(res[6], ' `2` `5` `8` ')
175 res <- pander_return(df, emphasize.verbatim.cols = c(1,2), style = 'simple')
176 expect_equal(res[5], ' `1` `4` 7 ')
177 expect_equal(res[6], ' `2` `5` 8 ')
178 df <- data.frame(a = 1:3, b = 1:3, c = 1:3)
179 res <- pander_return(df, emphasize.italics.rows = c(1, 2),
180 style = 'simple')
181 expect_equal(res[5], ' *1* *1* *1* ')
182 expect_equal(res[6], ' *2* *2* *2* ')
183 res <- pander_return(df, emphasize.strong.rows = c(1, 2),
184 style = 'simple')
185 expect_equal(res[5], ' **1** **1** **1** ')
186 expect_equal(res[6], ' **2** **2** **2** ')
187 })
188
189 test_that('emphasize.verbatim works correctly', {
190 df <- data.frame(a = 1:3, b = 4:6, c = 7:9)
191 res <- pander_return(df, emphasize.verbatim.rows = c(1, 2),
192 style = 'simple')
193 expect_equal(res[5], ' `1` `4` `7` ')
194 expect_equal(res[6], ' `2` `5` `8` ')
195 res <- pander_return(df, emphasize.verbatim.cols = c(1, 2),
196 style = 'simple')
197 expect_equal(res[5], ' `1` `4` 7 ')
198 expect_equal(res[6], ' `2` `5` 8 ')
199 res <- pander_return(df, emphasize.verbatim.cells =
200 which(df > 5, arr.ind = TRUE),
201 style = 'simple')
202 expect_equal(res[5], ' 1 4 `7` ')
203 expect_equal(res[6], ' 2 5 `8` ')
204 res <- pander_return(df, emphasize.verbatim.cells =
205 which(df > 5, arr.ind = TRUE),
206 emphasize.strong.rows = c(1, 2),
207 style = 'simple')
208 expect_equal(res[5], ' **1** **4** **`7`** ')
209 expect_equal(res[7], ' 3 `6` `9` ')
210 })
211
212 test_that('emphasize.strong.rows works correctly with no rows to emphasize #354', {
213 df <- data.frame(a = 1:3, b = 4:6, c = 7:9)
214 res <- pander_return(df, emphasize.strong.rows = integer(0), style = 'simple')
215 expect_equal(res[5], ' 1 4 7 ')
216 expect_equal(res[6], ' 2 5 8 ')
217 expect_equal(res[7], ' 3 6 9 ')
218 })
219
220 context('captions')
221
222 tables <- list(
223 mtcars,
224 mtcars[1:2, ],
225 summary(mtcars$am),
226 table(mtcars$am) + 0.1,
227 table(mtcars$am, mtcars$gear) + 0.1,
228 lm(mtcars$hp~1),
229 t.test(extra ~ group, data = sleep),
230 prcomp(USArrests),
231 density(mtcars$hp)
232 )
233
234 has.caption <- function(ttt, evals = FALSE) {
235 set.caption('foo')
236 if (!evals)
237 any(grepl('Table:', pander_return(ttt)))
238 else
239 !is.null(attr(evals('get("ttt")', env = parent.frame())[[1]]$result, 'caption'))
240 }
241
242 test_that('direct call', {
243 for (ttt in tables)
244 expect_that(has.caption(ttt), is_true())
245 })
246
247 cache.dir <- evalsOptions('cache.dir')
248 graph.dir <- evalsOptions('graph.dir')
249 wd <- getwd()
250 setwd(tempdir())
251 evalsOptions('cache.dir', file.path(tempdir(), '.cache'))
252 evalsOptions('graph.dir', file.path(tempdir(), 'plots'))
253 test_that('evals', {
254 for (ttt in tables)
255 expect_that(has.caption(ttt, evals = TRUE), is_true())
256 })
257 evalsOptions('cache.dir', cache.dir)
258 evalsOptions('graph.dir', graph.dir)
259 setwd(wd)
260
261 context('row and column names')
262
263 library(testthat)
264 library(pander)
265
266 tables <- list(
267 mtcars[1:2, 1:2],
268 data.frame(x = 1:2, y = 2:3),
269 data.frame(x = 1:3, y = 2:4)[c(1, 3), ]
270 )
271
272 t <- tables[[1]]
273
274 test_that('row names can be suppressed', {
275 for (t in tables) {
276 expect_false(grepl("&nbsp;", pandoc.table.return(t, row.names = FALSE)))
277 }
278 })
279
280 test_that('row names can be set', {
281 for (t in tables) {
282 expect_true(grepl("\\n \\*\\*a\\*\\*", pandoc.table.return(t, row.names = c("a", "b"))))
283 }
284 })
285
286 test_that('obvious row names can be preserved', {
287 expect_true(grepl(".*1.*2.*3.*", pandoc.table.return(data.frame(a = 7:9), row.names = 1:3)))
288 })
289
290 test_that('column names can be set', {
291 for (t in tables) {
292 res <- capture.output(pandoc.table(t, col.names = c("a", "b")))
293 expect_true(grepl("a *b", res[3]))
294 }
295 })
296
297 context('default alignments')
298
299 tables <- list(
300 mtcars,
301 mtcars$am,
302 mtcars[1:2, ],
303 mtcars[1:2, 5],
304 summary(mtcars$am),
305 table(mtcars$am) + 0.1,
306 table(mtcars$am, mtcars$gear) + 0.1,
307 summary(lm(mtcars$hp~1))$coeff,
308 table(mtcars$am, mtcars$gear, mtcars$carb)
309 )
310
311 tad <- panderOptions('table.alignment.default')
312 tar <- panderOptions('table.alignment.rownames')
313
314 panderOptions('table.alignment.default', 'centre')
315 panderOptions('table.alignment.rownames', 'right')
316 test_that('no error: allright', {
317 for (t in tables)
318 expect_that(pandoc.table.return(t, style = 'multiline'), is_a('character'))
319 })
320
321 f <- function(df) {
322 if (class(df) != 'matrix' && !is.table(df) || length(dim(df)) < 2)
323 ifelse(sapply(df, is.numeric), 'right', 'left')
324 else
325 ifelse(apply(df, 2, is.numeric), 'right', 'left')
326 }
327 panderOptions('table.alignment.default', f)
328 test_that('no error: functions', {
329 for (t in tables)
330 expect_that(pandoc.table.return(t, style = 'multiline'), is_a('character'))
331 })
332 panderOptions('table.alignment.default', tad)
333 panderOptions('table.alignment.rownames', tar)
334
335 test_that('digits param', {
336 m <- matrix(rep(0.111111, 6), nrow = 2)
337 res <- pander_return(m, digits = c(2, 4, 1))
338 expect_equal(res[3], ' 0.11 0.1111 0.1 ')
339 expect_warning(pander_return(m, digits = c(1, 2)))
340 res <- pander_return(m, digits = 2)
341 expect_equal(res[3],' 0.11 0.11 0.11 ')
342 mt <- mtcars[1:4, 5:8]
343 res <- pander_return(mt, digits = c(1, 4, 3, 4),
344 keep.trailing.zeros = TRUE)
345 expect_equal(res[5],
346 ' **Mazda RX4** 4 2.620 16.5 0 ')
347 })
348
349 test_that('round param', {
350 m <- matrix(rep(0.111111, 6), nrow = 2)
351 res <- pander_return(m, round = c(2, 4, 1))
352 expect_equal(res[3], ' 0.11 0.1111 0.1 ')
353 expect_warning(pander_return(m, round = c(1, 2)))
354 res <- pander_return(m, round = 2)
355 expect_equal(res[3],' 0.11 0.11 0.11 ')
356 })
357
358 context('keep.trailing.zeros')
359 test_that('digits param', {
360 expect_true(grepl('3.90', pander_return(mtcars[2:3, 1:5], keep.trailing.zeros = TRUE)[5]))
361 expect_false(grepl('3.90', pander_return(mtcars[2:3, 1:5], keep.trailing.zeros = FALSE)[5]))
362 })
363
364 context('keep.line.breaks')
365 test_that('keep.line.breaks works correctly', {
366 # keeping line breaks in a simple data.frame with one line breaks differs lines amount by one
367 x <- data.frame(a = 'Pander\nPackage')
368 lines.x.no.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = FALSE), '\n')[[1]])
369 lines.x.keep.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = TRUE), '\n')[[1]])
370 expect_equal(lines.x.no.breaks + 1, lines.x.keep.breaks)
371
372 # keeping line breaks in a simple data.frame with 2 rows with 1 line breaks differst lines amount by 2
373 x <- data.frame(a = c('Pander\nPackage', 'Pander\nPackage'))
374 lines.x.no.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = FALSE), '\n')[[1]])
375 lines.x.keep.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = TRUE), '\n')[[1]])
376 expect_equal(lines.x.no.breaks + 2, lines.x.keep.breaks)
377
378 #if there are no line breaks originally, they do not get introduced
379 x <- data.frame(a = c('Pander Package', 'Pander Package'))
380 lines.x.no.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = FALSE), '\n')[[1]])
381 lines.x.keep.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = TRUE), '\n')[[1]])
382 expect_equal(lines.x.no.breaks, lines.x.keep.breaks)
383
384 # works with random number of rows added
385 rows <- sample(1:100, 1)
386 x <- data.frame(a = rep('Pander\nPackage', rows))
387 lines.x.no.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = FALSE), '\n')[[1]])
388 lines.x.keep.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = TRUE), '\n')[[1]])
389 expect_equal(lines.x.no.breaks + rows, lines.x.keep.breaks)
390
391 # random number of line breaks in one cell
392 n <- sample(1:10, 1)
393 x <- data.frame(a = paste(rep('pander', n), collapse = '\n'))
394 lines.x.no.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = FALSE, split.cells = Inf), '\n')[[1]])
395 lines.x.keep.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = TRUE), '\n')[[1]])
396 expect_equal(lines.x.no.breaks + n - 1, lines.x.keep.breaks)
397
398 # random number of line breaks in cells, 3 columns
399 n <- sample(1:10, 3)
400 x <- data.frame(a = paste(rep('pander', n[1]), collapse = '\n'),
401 b = paste(rep('pander', n[2]), collapse = '\n'),
402 c = paste(rep('pander', n[3]), collapse = '\n'))
403 lines.x.no.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = FALSE, split.cells = Inf, split.tables = Inf), '\n')[[1]]) #nolint
404 lines.x.keep.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = TRUE), '\n')[[1]])
405 expect_equal(lines.x.no.breaks + max(n) - 1, lines.x.keep.breaks)
406
407 # 3 columns, 2 rows
408 x <- rbind(x, x)
409 lines.x.no.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = FALSE, split.cells = Inf, split.tables = Inf), '\n')[[1]]) #nolint
410 lines.x.keep.breaks <- length(strsplit(pandoc.table.return(x, keep.line.breaks = TRUE), '\n')[[1]])
411 expect_equal(lines.x.no.breaks + 2 * max(n) - 2, lines.x.keep.breaks)
412 })
413
414 context('split.cells')
415 test_that('split.cells works correctly', {
416 x <- data.frame(a = 'foo bar\nfo bar')
417 # single line break behaves correctly combines with keep line breaks
418 expect_equal(pandoc.table.return(x, keep.line.breaks = TRUE, split.cells = 7),
419 '\n---------\n a \n---------\n foo bar \n fo bar \n---------\n\n')
420 expect_equal(pandoc.table.return(x, keep.line.breaks = TRUE, split.cells = 6),
421 '\n--------\n a \n--------\n foo \n bar \n fo bar \n--------\n\n')
422 # Corner values of split.cells
423 x <- data.frame(a = 'foo bar', b = 'foo bar')
424 expect_equal(length(strsplit(pandoc.table.return(x, split.cells = c(6, Inf)), '\n')[[1]]), 8)
425 expect_equal(pandoc.table.return(x, split.cells = c(6, Inf)),
426 '\n---------------\n a b \n----- ---------\n foo foo bar \n bar \n---------------\n\n')
427 expect_equal(pandoc.table.return(x, split.cells = c(Inf, 6)),
428 '\n---------------\n a b \n--------- -----\n foo bar foo \n bar \n---------------\n\n')
429 expect_equal(pandoc.table.return(x, split.cells = c(7, 7)),
430 '\n-------------------\n a b \n--------- ---------\n foo bar foo bar \n-------------------\n\n')
431 expect_equal(pandoc.table.return(x, split.cells = c(7, 7)), pandoc.table.return(x, split.cells = Inf))
432 expect_equal(pandoc.table.return(x, split.cells = c(6, 6)), pandoc.table.return(x, split.cells = 6))
433 expect_equal(pandoc.table.return(x, split.cells = c(7, 7)), pandoc.table.return(x, split.cells = 7))
434 expect_equal(pandoc.table.return(x, split.cells = c(7, 7)), pandoc.table.return(x, split.cells = 7))
435 # relative split.cells
436 expect_equal(pandoc.table.return(x, split.cells = c('50%', '50%')),
437 pandoc.table.return(x, split.cells = c(7)))
438 expect_equal(pandoc.table.return(x, split.cells = c('20%', '80%'), split.tables = 30),
439 pandoc.table.return(x, split.cells = c(6, Inf)))
440 expect_equal(pandoc.table.return(x, split.cells = c('80%', '20%'), split.tables = 30),
441 pandoc.table.return(x, split.cells = c(Inf, 6)))
442 })
443
444 test_that('split.cells param produces expected warnings', {
445 mt <- mtcars[1:2, 1:4]
446 expect_warning(pander(mt, split.cells = c(1, 2)))
447 expect_warning(pander(mt, split.cells = c(1, 2, 3)))
448 expect_warning(pander(mt, split.cells = c('10%', '10%', '10%')))
449 expect_warning(pander(mt, split.cells = c('30%', '30%', '40%')))
450 })
451
452 context('tableExpand')
453
454 test_that('produces.errors', {
455 x <- data.frame(a = 'Pander\nPackage')
456 expect_error(pander(x, style = 'simple', keep.line.breaks = TRUE))
457 expect_error(pander(x, style = 'rmarkdown', keep.line.breaks = TRUE))
458 })
459
460 test_that('tableExpand behaves correctly', {
461 ## multiline style check
462 argv <- structure(list(txt = structure(c(1L, 4L, 2L, 3L), .Label = c('&nbsp;', 'cyl', 'disp', 'mpg'), class = 'factor'), width = c(19, 5, 5, 6), justify = structure(c(1L, 1L, 1L, 1L), .Label = 'centre', class = 'factor')), .Names = c('txt', 'width', 'justify'), row.names = c(NA, -4L), class = 'data.frame') #nolint
463 sep.cols <- c('', ' ', '')
464 style <- 'multiline'
465 res <- tableExpand_cpp(argv[, 1], argv[, 2], argv[, 3], sep.cols, style)
466 # max number of line breaks equals number of lines in the result
467 expect_equal(max(sapply(strsplit(as.character(argv[, 1]), '\n'), length)),
468 length(strsplit(res, '\n')[[1]]))
469 expect_equal(nchar(res),
470 nchar(sep.cols)[1] + (length(argv[, 2]) - 1) * nchar(sep.cols)[2] + nchar(sep.cols)[3] + sum(argv[, 2]))
471 expect_equal(res, ' &nbsp; mpg cyl disp ');
472
473 ## grid style check
474 argv <- structure(list(txt = structure(c(1L, 5L, 2L, 3L, 4L), .Label = c('&nbsp;', 'cyl', 'disp', 'hp', 'mpg'), class = 'factor'), width = c(20, 5, 5, 6, 4), justify = structure(c(1L, 1L, 1L, 1L, 1L), .Label = 'centre', class = 'factor')), .Names = c('txt', 'width', 'justify'), row.names = c(NA, -5L), class = 'data.frame') #nolint
475 sep.cols <- c('| ', ' | ', ' |')
476 res <- tableExpand_cpp(argv[, 1], argv[, 2], argv[, 3], sep.cols, style)
477 # max number of line breaks equals number of lines in the result
478 expect_equal(max(sapply(strsplit(as.character(argv[, 1]), '\n'), length)), length(strsplit(res, '\n')[[1]]))
479 expect_equal(nchar(res), nchar(sep.cols)[1] + (length(argv[, 2]) - 1) * nchar(sep.cols)[2] + nchar(sep.cols)[3] + sum(argv[, 2])) #nolint
480 expect_equal(res, '| &nbsp; | mpg | cyl | disp | hp |' )
481
482 ## rmarkdown style check
483 argv <- structure(list(txt = structure(c(1L, 4L, 5L, 3L, 2L), .Label = c('**Mazda RX4**', '110', '160', '21', '6'), class = 'factor'), width = c(20, 5, 5, 6, 4), justify = structure(c(1L, 1L, 1L, 1L, 1L), .Label = 'centre', class = 'factor')), .Names = c('txt', 'width', 'justify'), row.names = c('t.rownames', 'mpg', 'cyl', 'disp', 'hp'), class = 'data.frame') #nolint
484 sep.cols <- c('| ', ' | ', ' |')
485 style <- 'rmarkdown'
486 res <- tableExpand_cpp(argv[, 1], argv[, 2], argv[, 3], sep.cols, style)
487 # max number of line breaks equals number of lines in the result
488 expect_equal(max(sapply(strsplit(as.character(argv[, 1]), '\n'), length)), length(strsplit(res, '\n')[[1]]))
489 expect_equal(nchar(res),
490 nchar(sep.cols)[1] + (length(argv[, 2]) - 1) * nchar(sep.cols)[2] + nchar(sep.cols)[3] + sum(argv[, 2]))
491 expect_equal(res, '| **Mazda RX4** | 21 | 6 | 160 | 110 |')
492
493 ## simple style check
494 argv <- structure(list(txt = structure(c(1L, 3L, 4L, 2L, 5L), .Label = c('**Datsun 710**', '108', '22.8', '4', '93'), class = 'factor'), width = c(20, 5, 5, 6, 4), justify = structure(c(1L, 1L, 1L, 1L, 1L), .Label = 'centre', class = 'factor')), .Names = c('txt', 'width', 'justify'), row.names = c('t.rownames', 'mpg', 'cyl', 'disp', 'hp'), class = 'data.frame') #nolint
495 sep.cols <- c('', ' ', '')
496 style <- 'simple'
497 res <- tableExpand_cpp(argv[, 1], argv[, 2], argv[, 3], sep.cols, style)
498 # max number of line breaks equals number of lines in the result
499 expect_equal(max(sapply(strsplit(as.character(argv[, 1]), '\n'), length)), length(strsplit(res, '\n')[[1]]))
500 expect_equal(nchar(res),
501 nchar(sep.cols)[1] + (length(argv[, 2]) - 1) * nchar(sep.cols)[2] + nchar(sep.cols)[3] + sum(argv[, 2]))
502 expect_equal(res, ' **Datsun 710** 22.8 4 108 93 ')
503
504 ## left justification
505 argv <- structure(list(txt = structure(c(1L, 4L, 5L, 3L, 2L), .Label = c('**Mazda', '110', '160', '21', '6'), class = 'factor'), width = c(10, 5, 5, 6, 4), justify = structure(c(1L, 1L, 1L, 1L, 1L), .Label = 'left', class = 'factor')), .Names = c('txt', 'width', 'justify'), row.names = c(NA, -5L), class = 'data.frame') #nolint
506 sep.cols <- c('', ' ', '')
507 style <- 'multiline'
508 res <- tableExpand_cpp(argv[, 1], argv[, 2], argv[, 3], sep.cols, style)
509 # max number of line breaks equals number of lines in the result
510 expect_equal(max(sapply(strsplit(as.character(argv[, 1]), '\n'), length)), length(strsplit(res, '\n')[[1]]))
511 expect_equal(nchar(res),
512 nchar(sep.cols)[1] + (length(argv[, 2]) - 1) * nchar(sep.cols)[2] + nchar(sep.cols)[3] + sum(argv[, 2]))
513 expect_equal(res, '**Mazda 21 6 160 110 ');
514
515 ## right justification
516 argv <- structure(list(txt = structure(c(1L, 3L, 5L, 4L, 2L), .Label = c('**Hornet 4 Drive**', '110', '21.4', '258', '6'), class = 'factor'), width = c(20, 5, 5, 6, 4), justify = structure(c(1L, 1L, 1L, 1L, 1L), .Label = 'right', class = 'factor')), .Names = c('txt', 'width', 'justify'), row.names = c('t.rownames', 'mpg', 'cyl', 'disp', 'hp'), class = 'data.frame') #nolint
517 sep.cols <- c('', ' ', '')
518 style <- 'simple'
519 res <- tableExpand_cpp(argv[, 1], argv[, 2], argv[, 3], sep.cols, style)
520 # max number of line breaks equals number of lines in the result
521 expect_equal(max(sapply(strsplit(as.character(argv[, 1]), '\n'), length)), length(strsplit(res, '\n')[[1]]))
522 expect_equal(nchar(res),
523 nchar(sep.cols)[1] + (length(argv[, 2]) - 1) * nchar(sep.cols)[2] + nchar(sep.cols)[3] + sum(argv[, 2]))
524 expect_equal(res, ' **Hornet 4 Drive** 21.4 6 258 110' );
525
526 ## multiple lines
527 argv <- structure(list(txt = structure(c(1L, 3L, 5L, 4L, 2L), .Label = c('**Hornet\n4\nDrive**', '110', '21.4', '258', '6'), class = 'factor'), width = c(10, 5, 5, 6, 4), justify = structure(c(1L, 1L, 1L, 1L, 1L), .Label = 'left', class = 'factor')), .Names = c('txt', 'width', 'justify'), row.names = c('t.rownames', 'mpg', 'cyl', 'disp', 'hp'), class = 'data.frame') #nolint
528 sep.cols <- c('', ' ', '')
529 style <- 'multiline'
530 res <- tableExpand_cpp(argv[, 1], argv[, 2], argv[, 3], sep.cols, style)
531 # max number of line breaks equals number of lines in the result
532 expect_equal(max(sapply(strsplit(as.character(argv[, 1]), '\n'), length)),
533 length(strsplit(res, '\n')[[1]]))
534 # max number of line breaks equals number of lines in the result
535 expect_equal(res, '**Hornet 21.4 6 258 110 \n4 \nDrive** '); #nolint
536
537
538 argv <- structure(list(txt = structure(c(1L, 3L, 4L, 2L, 5L), .Label = c('**Datsun\n710**', '108', '22.8', '4', '93'), class = 'factor'), width = c(10, 5, 5, 6, 4), justify = structure(c(1L, 1L, 1L, 1L, 1L), .Label = 'left', class = 'factor')), .Names = c('txt', 'width', 'justify'), row.names = c('t.rownames', 'mpg', 'cyl', 'disp', 'hp'), class = 'data.frame') #nolint
539 sep.cols <- c('', ' ', '')
540 style <- 'multiline'
541 res <- tableExpand_cpp(argv[, 1], argv[, 2], argv[, 3], sep.cols, style)
542 # max number of line breaks equals number of lines in the result
543 expect_equal(max(sapply(strsplit(as.character(argv[, 1]), '\n'), length)),
544 length(strsplit(res, '\n')[[1]]))
545 expect_equal(res, '**Datsun 22.8 4 108 93 \n710** ');
546
547 argv <- structure(list(txt = structure(c(1L, 4L, 5L, 3L, 2L), .Label = c('**Mazda\nRX4\nWag**', '110', '160', '21', '6'), class = 'factor'), width = c(10, 5, 5, 6, 4), justify = structure(c(1L, 1L, 1L, 1L, 1L), .Label = 'left', class = 'factor')), .Names = c('txt', 'width', 'justify'), row.names = c('t.rownames', 'mpg', 'cyl', 'disp', 'hp'), class = 'data.frame') #nolint
548 sep.cols <- c('', ' ', '')
549 style <- 'multiline'
550 res <- tableExpand_cpp(argv[, 1], argv[, 2], argv[, 3], sep.cols, style)
551 # max number of line breaks equals number of lines in the result
552 expect_equal(max(sapply(strsplit(as.character(argv[, 1]), '\n'), length)), length(strsplit(res, '\n')[[1]]))
553 expect_equal(res, '**Mazda 21 6 160 110 \nRX4 \nWag** '); #nolint
554
555 # empty cells
556 cells <- c('', '', '')
557 cols.width <- c(2, 2, 2)
558 justify <- c('centre', 'centre', 'centre')
559 sep.cols <- c('', ' ', '')
560 res <- tableExpand_cpp(cells, cols.width, justify, sep.cols, 'multiline')
561 # when max.width param is small, every word is a line
562 expect_equal(max(sapply(strsplit(as.character(cells), '\n'), length)), 0)
563 expect_equal(nchar(res),
564 nchar(sep.cols)[1] + (length(cells) - 1) * nchar(sep.cols)[2] + nchar(sep.cols)[3] + sum(cols.width))
565 expect_equal(res, ' ')
566
567 # backslashes issue (#22)
568 expect_equal(pandoc.table.return(
569 data.frame(a = '\\1 \\ 32', b ='23')),
570 '\n--------------\n a b \n--------- ----\n \\1 \\ 32 23 \n--------------\n\n') #nolint
571 expect_equal(pandoc.table.return(
572 data.frame(a = '\\1 \\ 32', b = '23'),
573 justify = 'right'),
574 '\n--------------\n a b\n--------- ----\n \\1 \\ 32 23\n--------------\n\n') #nolint
575 expect_equal(pandoc.table.return(data.frame(a = '\\1', b = '23')),
576 '\n---------\n a b \n---- ----\n \\1 23 \n---------\n\n') #nolint
577
578 # unicode string issue
579 expect_equal(pandoc.table.return(data.frame(a = 'ßß')),
580 '\n----\n a \n----\n ßß \n----\n\n')
581 })
582
583 context('Empty objects')
584
585 test_that('Behavior for empty objects is correct', {
586 mt <- mtcars[mtcars$mpg < 0, 1:4]
587 res <- pander_return(mt)
588 expect_equal(res[3], ' **mpg** **cyl** **disp** **hp** ')
589 expect_equal(length(res), 6)
590 colnames(mt) <- NULL
591 res <- suppressWarnings(pander_return(mt))
592 expect_equal(length(res), 0)
593 expect_warning(pander_return(mt))
594 mt <- matrix(0, nrow = 0, ncol = 5)
595 res <- suppressWarnings(pander_return(mt))
596 expect_equal(length(res), 0)
597 expect_warning(pander_return(mt))
598 colnames(mt) <- 1:5
599 res <- pander_return(mt)
600 expect_equal(res[3], ' **1** **2** **3** **4** **5** ')
601 expect_equal(length(res), 6)
602 expect_equal(length(suppressWarnings(pander_return(data.frame()))), 0)
603 expect_warning(pander_return(data.frame()))
604 })
605
606 context('plain.ascii')
607
608 test_that('plain.ascii option works correctly', {
609 # dim is NULL
610 x <- 1:10
611 res <- pandoc.table.return(x, emphasize.cells = c(3, 4), plain.ascii = TRUE)
612 res <- strsplit(res, '\n')[[1]]
613 expect_false(any(grepl('\\*', res)))
614 expect_equal(res[3], ' 1 2 3 4 5 6 7 8 9 10 ')
615 # length(dim) == 1
616 x <- array(1:10)
617 res <- pandoc.table.return(x, emphasize.cells = c(3, 4), plain.ascii = TRUE)
618 res <- strsplit(res, '\n')[[1]]
619 expect_false(any(grepl('\\*', res)))
620 expect_equal(res[3], ' 1 2 3 4 5 6 7 8 9 10 ')
621 # length(dim) > 1
622 x <- mtcars[1:3, 1:4]
623 res <- pandoc.table.return(x, emphasize.rows = 2, plain.ascii = TRUE)
624 expect_false(grepl('&nbsp;', res))
625 expect_false(grepl('\\*', res))
626 })
627
628 context('S3 methods')
629
630 test_that('pander.tabular behaves correctly', {
631 suppressMessages(require(tables))
632 tab <- pander_return(tables::tabular(as.factor(am) ~ (mpg + hp + qsec) * (mean + median),
633 data = mtcars),
634 emphasize.rownames = FALSE,
635 split.tables = Inf)
636 expect_equal(length(tab), 10)
637 tab <- pander_return(tables::tabular( (Species + 1) ~ (n = 1) + Format(digits = 2) * (Sepal.Length + Sepal.Width) * (mean + sd), data = iris ), #nolint
638 split.tables = Inf)
639 expect_equal(length(tab), 14)
640 })
641
642 test_that('pander.CrossTable behaves correctly', {
643 suppressMessages(require(descr))
644 # issue https://github.com/Rapporter/pander/issues/163
645 x <- CrossTable(mtcars$cyl, mtcars$gear, prop.c = FALSE, prop.t = FALSE, chisq = FALSE, prop.chisq = FALSE)
646 res <- pander_return(x)
647 expect_true(any(grepl(gsub('\\$', '\\\\$', x$ColData), res))) #nolint
648 expect_true(any(grepl(gsub('\\$', '\\\\$', x$RowData), res))) #nolint
649 # expected N, residual, std residual, adj std residual rownames was not included
650 x <- suppressWarnings(CrossTable(mtcars$cyl, mtcars$gear, expected = TRUE,
651 resid = TRUE, sresid = TRUE, asresid = TRUE))
652 res <- pander_return(x)
653 expect_true(any(grepl('Expected N', res)))
654 expect_true(any(grepl('Residual', res)))
655 expect_true(any(grepl('Std Residual', res)))
656 expect_true(any(grepl('Adj Std Resid', res)))
657 # issue 211 support for total.r total.c
658 x <- suppressWarnings(CrossTable(mtcars$cyl, mtcars$gear))
659 res <- pander_return(x, total.c = FALSE)
660 expect_equal(length(res), 27)
661 expect_equal(length(grep('Total', res)), 4)
662 res <- pander_return(x, total.r = FALSE)
663 expect_equal(length(res), 30)
664 expect_equal(length(grep('Total', res)), 4)
665 res <- pander_return(x)
666 expect_equal(length(res), 30)
667 expect_equal(length(grep('Total', res)), 5)
668 })
669
670 test_that('pander.NULL behaves correctly', {
671 expect_equal(length(pander_return(NULL)), 0)
672 expect_equal(length(pander_return(c(NULL, NULL))), 0)
673 })
674
675 test_that('pander.cast_df behaves correctly', {
676 df <- data.frame(type = c(1, 1, 2, 2, 3, 3), variable = 'n', value = c(71, 72, 68, 80, 21, 20))
677 df.cast <- reshape::cast(df, type~., sum)
678 expect_equal(pander_return(df.cast, style = 'simple'),
679 c('', '', ' type (all) ', '------ -------',
680 ' 1 143 ', ' 2 148 ', ' 3 41 ', ''))
681 })
682
683 test_that('pander.lm/pander.summary.lm behaves correctly', {
684 ctl <- c(4.17, 5.58, 5.18, 6.11, 4.50, 4.61, 5.17, 4.53, 5.33, 5.14)
685 trt <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69)
686 group <- gl(2, 10, 20, labels = c('Ctl', 'Trt'))
687 weight <- c(ctl, trt)
688 lm.D9 <- lm(weight ~ group)
689 res1 <- pander_return(lm.D9)
690 expect_equal(length(res1), 11)
691 expect_equal(max(nchar(res1)), 63)
692 res <- pander_return(summary(lm.D9))
693 expect_true(any(grepl('Fitting linear model', res)))
694 expect_true(any(grepl('Observations', res)))
695 expect_equal(length(res), 18)
696 })
697
698 test_that('pander.glm/pander.summary.glm behaves correctly', {
699 clotting <- data.frame(
700 u = c(5, 10, 15, 20, 30, 40, 60, 80, 100),
701 lot1 = c(118, 58, 42, 35, 27, 25, 21, 19, 18),
702 lot2 = c(69, 35, 26, 21, 18, 16, 13, 12, 12))
703 glmm <- glm(lot1 ~ log(u), data = clotting, family = Gamma)
704 pglmm <- pander_return(glmm)
705 expect_equal(length(pglmm), 11)
706 expect_equal(max(nchar(pglmm)), 70)
707 res <- pander_return(summary(glmm))
708 expect_true(any(grepl('Null deviance', res)))
709 expect_equal(length(res), 21)
710 })
711
712 test_that('pander.aov/pander.summary.aov behaves correctly', {
713 npk.aovE <- aov(yield ~ N * P * K + Error(block), npk)
714 paov <- pander_return(npk.aovE)
715 psaov <- pander_return(summary(npk.aovE))
716 expect_equal(paov, psaov) # choice of similar result for summary and standard
717 expect_equal(length(psaov), 23)
718 })
719
720 test_that('pander.anova behaves correctly', {
721 fit <- lm(sr ~ ., data = LifeCycleSavings)
722 a <- anova(fit)
723 pa <- pander_return(a, style = 'simple')
724 expect_true(all(sapply(names(a)[-5], grepl, pa[3])))
725 #more complicated run
726 fit0 <- lm(sr ~ 1, data = LifeCycleSavings)
727 fit1 <- update(fit0, . ~ . + pop15)
728 fit2 <- update(fit1, . ~ . + pop75)
729 fit3 <- update(fit2, . ~ . + dpi)
730 fit4 <- update(fit3, . ~ . + ddpi)
731 a <- anova(fit0, fit1, fit2, fit3, fit4, test = 'F')
732 pa <- pander_return(a, style = 'simple')
733 expect_true(all(sapply(names(a)[-6], grepl, pa[3])))
734 })
735
736 test_that('pander.aovlist/pander.summary.aovlist behaves correctly', {
737 options(contrasts = c('contr.helmert', 'contr.poly'))
738 N <- c(0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0)
739 P <- c(1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0)
740 K <- c(1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0)
741 yield <- c(49.5, 62.8, 46.8, 57.0, 59.8, 58.5, 55.5, 56.0, 62.8, 55.8, 69.5,
742 55.0, 62.0, 48.8, 45.5, 44.2, 52.0, 51.5, 49.8, 48.8, 57.2, 59.0, 53.2, 56.0)
743 npk <- data.frame(block = gl(6, 4), N = factor(N), P = factor(P),
744 K = factor(K), yield = yield)
745 a <- aov(yield ~ N * P * K + Error(block), npk)
746 pa <- pander_return(a, style = 'simple')
747 expect_equal(length(pa), 14)
748 })
749
750 test_that('pander.ts behaves correctly', {
751 # ncol NULL
752 res <- pander_return(ts(1:10, frequency = 4, start = c(1959, 2)), style = 'simple')
753 expect_equal(res[3], ' &nbsp; Q1 Q2 Q3 Q4 ')
754 expect_equal(length(res), 8)
755 # ncol not NULL
756 res <- pander_return(ts(matrix(rnorm(30), 10, 3), start = c(1961, 1), frequency = 12), style = 'simple')
757 expect_equal(res[3], ' &nbsp; Series.1 Series.2 Series.3 ')
758 expect_equal(length(res), 15)
759 })
760
761 test_that('pander.coxph behaves correctly', {
762 suppressMessages(require(survival))
763 test1 <- list(time = c(4, 3, 1, 1, 2, 2, 3),
764 status = c(1, 1, 1, 0, 1, 1, 0),
765 x = c(0, 2, 1, 1, 1, 0, 0),
766 sex = c(0, 0, 0, 0, 1, 1, 1))
767 # Fit a stratified model
768 res <- pander_return(coxph(Surv(time, status) ~ x + strata(sex), test1))
769 expect_equal(res[3], ' &nbsp; coef exp(coef) se(coef) z p ')
770 expect_equal(length(res), 10)
771 })
772
773 test_that('pander.clogit works correctly', {
774 resp <- levels(logan$occupation)
775 n <- nrow(logan)
776 indx <- rep(1:n, length(resp))
777 logan2 <- data.frame(logan[indx, ],
778 id = indx,
779 tocc = factor(rep(resp, each = n)))
780 logan2$case <- (logan2$occupation == logan2$tocc)
781 res <- pander_return(suppressWarnings(clogit(case ~ tocc + tocc:education + strata(id), logan2)), split.table = Inf)
782 expect_true(grepl('Fitting Conditional logistic regression', res[24]))
783 expect_equal(length(res), 26)
784 })
785
786 test_that('pander.zoo works correctly', {
787 suppressMessages(require(zoo))
788 x.Date <- as.Date('2003-02-01') + c(1, 3, 7, 9, 14) - 1
789 res <- pander_return(zoo(rnorm(5), x.Date), style = 'simple')
790 expect_true(grepl('Value', res[3]))
791 expect_equal(length(res), 10)
792 # more complex example with colnames
793 res <- pander_return(zoo(cbind(foo = rnorm(5), bar = rnorm(5))), style = 'simple')
794 expect_true(grepl('foo', res[3]))
795 expect_equal(length(res), 10)
796 })
797
798 test_that('pander.lme/pander.summary.lme behaves correctly', {
799 suppressMessages(require(nlme))
800 l1 <- lme(distance ~ age, Orthodont, random = ~ age | Subject)
801 sl <- summary(l1)
802 pl <- pander_return(l1)
803 spl <- pander_return(sl)
804 expect_equal(length(pl), 11)
805 expect_equal(length(grep('Table', pl)), 1)
806 expect_equal(length(spl), 29)
807 expect_equal(length(grep('Table', spl)), 3)
808 })
809
810 # test_that('pander.describe works correctly', {
811 # suppressMessages(require(psych))
812 # x <- data.frame(a = rnorm(10), b = rnorm(10, 2, 2), c = rnorm(10, 3, 4))
813 # res <- pander_return(describe(x))
814 # expect_equal(length(res), 37)
815 # expect_equal(length(grep('Table', res)), 2)
816 # res <- pander_return(describe(x), split.tables = Inf)
817 # expect_equal(length(res), 11)
818 # })
819
820 test_that('pander.survdiff works correctly', {
821 suppressMessages(require(survival))
822 res <- pander_return(survdiff(Surv(futime, fustat) ~ rx, data = ovarian))
823 expect_equal(length(res), 12)
824 expect_equal(res[3], ' &nbsp; N Observed Expected (O-E)^2/E (O-E)^2/V ')
825 # length(x$n) == 1
826 expect <- survexp(futime ~ ratetable(age = (accept.dt - birth.dt),
827 sex = 1,
828 year = accept.dt,
829 race = 'white'),
830 jasa, cohort = FALSE, ratetable = survexp.usr)
831 res <- pander_return(survdiff(Surv(jasa$futime, jasa$fustat) ~ offset(expect)))
832 expect_equal(res[3], ' Observed Expected Z p ')
833 expect_equal(length(res), 9)
834 })
835
836 test_that('pander.survfit works correctly', {
837 suppressMessages(require(survival))
838 res <- pander_return(survfit(Surv(time, status) ~ x, data = aml))
839 expect_equal(length(res), 20)
840 expect_true(any(grepl('Table', res)))
841 # using additional options
842 res <- pander_return(survfit(Surv(time, status) ~ x, data = aml), print.rmean = TRUE)
843 expect_equal(length(res), 21)
844 expect_equal(res[21], '* restricted mean with upper limit = 161')
845 })
846
847 test_that('pander.sessionInfo works correctly', {
848 suppressMessages(require(utils))
849 res <- pander_return(sessionInfo())
850 expect_true(any(grepl('locale', res)))
851 expect_true(any(grepl('attached base package', res)))
852 res <- pander_return(sessionInfo(), locale = FALSE, compact = FALSE)
853 expect_false(any(grepl('locale', res)))
854 expect_true(any(grepl('utils', res)))
855 })
856
857 test_that('pander.stat.table works correctly', {
858 suppressMessages(require(Epi))
859 res <- pander_return(stat.table(
860 tension,
861 list(count(), mean(breaks)),
862 data = warpbreaks))
863 expect_equal(length(res), 11)
864 expect_equal(res[3], ' &nbsp; count() mean(breaks) ')
865 res <- pander_return(stat.table(index = list(tension, wool),
866 mean(breaks), data = warpbreaks))
867 expect_equal(length(res), 13)
868 # here add test
869 })
870
871 test_that('pander.microbenchmark works correctly', {
872 suppressMessages(require(microbenchmark))
873 res <- pander_return(microbenchmark(paste(1:10), paste0(1:10)))
874 expect_true(any(grepl('Unit', res)))
875 expect_equal(length(res), 11)
876 res <- pander_return(microbenchmark(paste(1:10), paste0(1:10)), split.tables = Inf, expr.labels = c('A'))
877 expect_true(any(grepl('A', res)))
878 expect_true(any(grepl('paste0\\(1:10\\)', res)))
879 res <- pander_return(microbenchmark(paste(1:10), paste0(1:10)), split.tables = Inf, expr.labels = c('A', 'B'))
880 expect_true(any(grepl('A', res)))
881 expect_true(any(grepl('B', res)))
882 expect_false(any(grepl('paste0\\(1:10\\)', res)))
883 res <- pander_return(microbenchmark(paste(1:10), paste0(1:10)), split.tables = Inf, expr.labels = c('A', 'B', 'C'))
884 expect_true(any(grepl('A', res)))
885 expect_true(any(grepl('B', res)))
886 expect_false(any(grepl('paste0\\(1:10\\)', res)))
887 })
888
889 test_that('pander.function works correctly', {
890 testf <- function(x) {
891 y <- x + 1
892 y
893 }
894 res <- pander_return(testf)
895 expect_true(all(grepl('\t', res)))
896 expect_equal(length(res), 5)
897 res <- pander_return(testf, syntax.highlighting = TRUE, add.name = TRUE)
898 expect_equal(length(res), 7)
899 expect_equal(res[1], '```r')
900 expect_true(grepl('testf', res[2]))
901 })
902
903 test_that('pander.rlm works correctly', {
904 suppressMessages(require(MASS))
905 res <- pander_return(rlm(stack.loss ~ ., stackloss))
906 expect_equal(res[4], ' (Intercept) Air.Flow Water.Temp Acid.Conc. ')
907 expect_equal(length(res), 13)
908 res <- pander_return(rlm(stack.loss ~ ., stackloss, psi = psi.hampel, init = 'lts'))
909 expect_equal(res[11], 'Degrees of freedom: 21 total; 17 residual')
910 expect_equal(length(res), 13)
911 })
912
913 test_that('pander.summary.table works correctly', {
914 ts <- summary(xtabs(cbind(ncases, ncontrols) ~ ., data = esoph))
915 res <- pander_return(ts)
916 expect_equal(res[6], ' Chisq df p.value ')
917 expect_equal(length(res), 13)
918 res <- pander_return(ts, print.call = FALSE)
919 expect_false(any(grepl('Calls', res)))
920 expect_equal(length(res), 12)
921 res <- pander_return(ts, caption = 'Factor')
922 expect_equal(res[11], 'Table: Factor')
923 res <- pander_return(summary(table(1:3)))
924 expect_false(any(grepl('-', res)))
925 expect_equal(length(res), 2)
926 })
927
928 test_that('pander.randomForest works correctly', {
929 suppressMessages(require(randomForest))
930 iris.rf <- randomForest(Species ~ ., data = iris, importance = TRUE,
931 proximity = TRUE)
932 res <- pander_return(iris.rf)
933 expect_equal(length(res), 20)
934 expect_equal(res[19], 'Table: Confusion Matrix')
935 ozone.rf <- randomForest(Ozone ~ ., data = airquality, mtry = 3,
936 importance = TRUE, na.action = na.omit)
937 res <- pander_return(ozone.rf)
938 expect_equal(length(res), 8)
939 expect_false(any(grep('-', res)))
940 # with test
941 index <- 1:nrow(iris)
942 trainindex <- sample(index, trunc(length(index) / 2))
943 trainset <- iris[trainindex, ]
944 testset <- iris[-trainindex, ]
945 res <- pander_return(randomForest(x = trainset[, -1],
946 y = trainset[, 1],
947 xtest = testset[, -1],
948 ytest = testset[, 1]))
949 expect_equal(length(res), 10)
950 expect_true(any(grepl('Test set MSE', res)))
951 res <- pander_return(randomForest(x = trainset[, -ncol(trainset)],
952 y = trainset[, ncol(trainset)],
953 xtest = testset[, -ncol(testset)],
954 ytest = testset[, ncol(testset)]))
955 expect_equal(length(res), 34)
956 expect_equal(res[33], 'Table: Test Confusion Matrix')
957 })
958
959 test_that('pander.irts works correctly', {
960 suppressMessages(require(tseries))
961 n <- 10
962 t <- cumsum(rexp(n, rate = 0.1))
963 v <- rnorm(n)
964 res <- pander_return(as.irts(cbind(t, v)))
965 expect_equal(length(res), 23)
966 u <- rnorm(n)
967 res <- pander_return(irts(t, cbind(u, v)))
968 expect_equal(length(res), 23)
969 })
970
971 test_that('pander.manova/summary.manova works correctly', {
972 npk2 <- within(npk, foo <- rnorm(24))
973 x <- manova(cbind(yield, foo) ~ block + N * P * K, npk2)
974 res1 <- pander_return(x)
975 xs <- summary(x)
976 res2 <- pander_return(xs)
977 expect_equal(res1, res2)
978 expect_equal(length(res1), 21)
979 })
980
981 test_that('pander.gtable works correctly', {
982 suppressMessages(require(gtable))
983 suppressMessages(require(grid))
984 a <- gtable(unit(1:3, c('cm')), unit(5, 'cm'))
985 rect <- rectGrob(gp = gpar(fill = 'black'))
986 a <- gtable_add_grob(a, rect, 1, 1)
987 res <- pander_return(a)
988 expect_equal(length(res), 7)
989 })
990
991 test_that('pander.nls/pander.summary.nls works correctly', {
992 utils::data(muscle, package = 'MASS')
993 with(muscle, table(Strip))
994 musc.1 <- nls(Length ~ cbind(1, exp(-Conc / th)), muscle,
995 start = list(th = 1), algorithm = 'plinear')
996 res <- pander_return(musc.1, show.convergence = TRUE)
997 expect_equal(length(res), 17)
998 res <- pander_return(musc.1)
999 expect_equal(length(res), 14)
1000 musc.1.s <- summary(musc.1)
1001 res <- pander_return(musc.1.s)
1002 expect_equal(length(res), 18)
1003 res <- pander_return(musc.1.s, show.convergence = TRUE)
1004 expect_equal(length(res), 21)
1005 musc.1.s <- summary(musc.1, correlation = TRUE)
1006 res <- pander_return(musc.1.s)
1007 expect_true(any(grepl('Correlation', res)))
1008 expect_equal(length(res), 31)
1009 })
1010
1011 test_that('pander.arima works correctly', {
1012 res <- pander_return(arima(lh, order = c(1, 0, 0)))
1013 expect_equal(length(res), 17)
1014 expect_true(any(grep('s\\.e', res)))
1015 res <- pander_return(arima(lh, order = c(3, 0, 0)))
1016 expect_equal(length(res), 17)
1017 expect_true(any(grep('s\\.e', res)))
1018 res <- pander_return(arima(lh, order = c(3, 0, 0), method = 'CSS'), se = FALSE)
1019 expect_equal(length(res), 15)
1020 expect_false(any(grep('s\\.e', res)))
1021 })
1022
1023 test_that('pander.polr/summary.polr works correctly', {
1024 house.plr <- polr(Sat ~ Infl + Type + Cont, weights = Freq, data = housing)
1025 res <- pander_return(house.plr)
1026 expect_equal(length(res), 25)
1027 expect_equal(length(grep('Table', res)), 2)
1028 res <- pander_return(suppressMessages(summary(house.plr)))
1029 expect_equal(length(res), 39)
1030 expect_equal(length(grep('Table', res)), 2)
1031 res <- pander_return(suppressMessages(summary(house.plr, correlation = TRUE)), split.table = Inf)
1032 expect_equal(length(res), 60)
1033 expect_equal(length(grep('Table', res)), 3)
1034 })
1035
1036 test_that('pander.survreg/summary.survreg works correctly', {
1037 suppressMessages(require(survival))
1038 x <- survreg(Surv(futime, fustat) ~ ecog.ps + rx, ovarian, dist = 'exponential')
1039 res <- pander_return(x)
1040 expect_equal(length(res), 27)
1041 expect_equal(length(grep('Table', res)), 1)
1042 res <- pander_return(suppressMessages(summary(x)))
1043 expect_equal(length(res), 33)
1044 expect_equal(length(grep('Table', res)), 1)
1045 res <- pander_return(suppressMessages(summary(x, correlation = TRUE)), split.table = Inf)
1046 expect_equal(length(res), 44)
1047 expect_equal(length(grep('Table', res)), 2)
1048 })
1049
1050 test_that('pander.ols works correctly', {
1051 suppressMessages(require(rms))
1052 set.seed(1)
1053 x1 <- runif(200)
1054 x2 <- sample(0:3, 200, TRUE)
1055 distance <- (x1 + x2 / 3 + rnorm(200)) ^ 2
1056 d <- datadist(x1, x2)
1057 f <- ols(sqrt(distance) ~ rcs(x1, 4) + scored(x2), x = TRUE)
1058 res <- pander_return(f)
1059 expect_equal(length(res), 50)
1060 expect_equal(length(grep('Table', res)), 3)
1061 })
1062
1063 test_that('pander.lrm works correctly', {
1064 suppressMessages(require(rms))
1065 x <- 1:5
1066 y <- c(0, 1, 0, 1, 0)
1067 reps <- c(1, 2, 3, 2, 1)
1068 res <- pander_return(lrm(y ~ x))
1069 expect_equal(length(res), 39)
1070 expect_true(any(grep('y ~ x', res)))
1071 expect_equal(length(grep('Table', res)), 2)
1072
1073 x <- rep(x, reps)
1074 y <- rep(y, reps)
1075 res <- pander_return(lrm(y ~ x), coefs = FALSE)
1076 expect_equal(length(res), 28)
1077 expect_true(any(grep('y ~ x', res)))
1078 expect_equal(length(grep('Table', res)), 1)
1079
1080 res <- pander_return(lrm(y ~ x, penalty = 0.1), coefs = TRUE)
1081 expect_true(any(grep('y ~ x', res)))
1082 expect_equal(length(grep('Table', res)), 3)
1083 })
1084
1085 test_that('pander.orm works correctly', {
1086 suppressMessages(require(rms))
1087 n <- 100
1088 y <- round(runif(n), 2)
1089 x1 <- sample(c(-1, 0, 1), n, TRUE)
1090 x2 <- sample(c(-1, 0, 1), n, TRUE)
1091 res <- pander_return(orm(y ~ x1 + x2, eps = 1e-5))
1092 expect_equal(length(res), 39)
1093 expect_true(any(grep('y ~ x1 \\+ x2', res)))
1094 expect_equal(length(grep('Table', res)), 2)
1095
1096 res <- pander_return(orm(y ~ x1 + x2, eps = 1e-5), coefs = FALSE)
1097 expect_equal(length(res), 28)
1098 expect_true(any(grep('y ~ x1 \\+ x2', res)))
1099 expect_equal(length(grep('Table', res)), 1)
1100 })
1101
1102 test_that('pander.Glm works correctly', {
1103 suppressMessages(require(rms))
1104 clotting <- data.frame(
1105 u = c(5, 10, 15, 20, 30, 40, 60, 80, 100),
1106 lot1 = c(118, 58, 42, 35, 27, 25, 21, 19, 18),
1107 lot2 = c(69, 35, 26, 21, 18, 16, 13, 12, 12))
1108 f <- Glm(lot1 ~ log(u), data = clotting, family = Gamma)
1109 res <- pander_return(f)
1110 expect_equal(length(res), 31)
1111 expect_true(any(grep('lot1 ~ log\\(u\\)', res)))
1112 expect_equal(length(grep('Table', res)), 2)
1113 res <- pander_return(f, coefs = FALSE)
1114 expect_equal(length(res), 20)
1115 expect_true(any(grep('ot1 ~ log\\(u\\)', res)))
1116 expect_equal(length(grep('Table', res)), 1)
1117 })
1118
1119 test_that('pander.cph', {
1120 options(contrasts = original_contrasts_options)
1121 suppressMessages(require(rms))
1122 n <- 1000
1123 set.seed(731)
1124 age <- 50 + 12 * rnorm(n)
1125 label(age) <- 'Age'
1126 sex <- factor(sample(c('Male', 'Female'), n, rep = TRUE, prob = c(.6, .4)))
1127 cens <- 15 * runif(n)
1128 h <- .02 * exp(.04 * (age - 50) + .8 * (sex == 'Female'))
1129 dt <- -log(runif(n)) / h
1130 label(dt) <- 'Follow-up Time'
1131 e <- ifelse(dt <= cens, 1, 0)
1132 dt <- pmin(dt, cens)
1133 units(dt) <- 'Year'
1134 dd <- datadist(age, sex)
1135 S <- Surv(dt, e)
1136 f <- cph(S ~ rcs(age, 4) + sex, x = TRUE, y = TRUE)
1137 res <- pander_return(f)
1138 expect_equal(length(res), 43)
1139 expect_true(any(grep('S ~ rcs\\(age, 4\\) \\+ sex', res)))
1140 expect_equal(length(grep('Table', res)), 2)
1141 res <- pander_return(f, conf.int = 0.95)
1142 expect_equal(length(res), 58)
1143 expect_true(any(grep('S ~ rcs\\(age, 4\\) \\+ sex', res)))
1144 expect_equal(length(grep('Table', res)), 3)
1145 })
1146
1147 test_that('pander.ets works correctly', {
1148 suppressMessages(require(forecast))
1149 res <- pander_return(ets(lh, model = 'AAN', lambda = .5))
1150 expect_equal(length(res), 27)
1151 expect_true(any(grep('Box-Cox', res)))
1152 res <- pander_return(ets(lh, model = 'ANN'))
1153 expect_equal(length(res), 25)
1154 expect_false(any(grep('Box-Cox', res)))
1155 res <- pander_return(ets(lh, model = 'AAN', damped = TRUE))
1156 expect_equal(length(res), 25)
1157 expect_true(any(grep('phi', res)))
1158 })
1159
1160 test_that('pander.list works correctly', {
1161 res <- pander_return(list(1:10))
1162 expect_equal(length(res), 7)
1163 expect_true(any(grep('end of list', res)))
1164 res <- pander_return(list(a = 1:10, b = list()))
1165 expect_equal(length(res), 8)
1166 expect_true(any(grep('end of list', res)))
1167 res <- pander_return(list(a = list(b = list(c = 5))))
1168 expect_equal(length(res), 13)
1169 expect_true(any(grep('end of list', res)))
1170 })