-
Notifications
You must be signed in to change notification settings - Fork 65
/
S3.R
2342 lines (2068 loc) · 82.1 KB
/
S3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' Generic pander method
#'
#' Prints an R object in Pandoc's markdown.
#' @param x an R object
#' @param ... optional parameters passed to special methods and/or raw \code{pandoc.*} functions
#' @return By default this function outputs (see: \code{cat}) the result. If you would want to catch the result instead, then call the function ending in \code{.return}.
#' @note This function can be called by \code{pander} and \code{pandoc} too.
#' @references \itemize{
#' \item John MacFarlane (2013): _Pandoc User's Guide_. \url{https://johnmacfarlane.net/pandoc/README.html}
#' \item David Hajage (2011): _ascii. Export R objects to several markup languages._ \url{https://cran.r-project.org/package=ascii}
#' \item Hlavac, Marek (2013): _stargazer: LaTeX code for well-formatted regression and summary statistics tables._ \url{https://cran.r-project.org/package=stargazer}
#' }
#' @export
#' @examples
#'
#' ## Vectors
#' pander(1:10)
#' pander(letters)
#' pander(mtcars$am)
#' pander(factor(mtcars$am))
#'
#' ## Lists
#' pander(list(1, 2, 3, c(1, 2)))
#' pander(list(a = 1, b = 2, c = table(mtcars$am)))
#' pander(list(1, 2, 3, list(1, 2)))
#' pander(list(a = 1, 2, 3, list(1, 2)))
#' pander(list('FOO', letters[1:3], list(1:5), table(mtcars$gear), list('FOOBAR', list('a', 'b'))))
#' pander(list(a = 1, b = 2, c = table(mtcars$am), x = list(myname = 1, 2), 56))
#' pander(unclass(chisq.test(table(mtcars$am, mtcars$gear))))
#'
#' ## Arrays
#' pander(mtcars)
#' pander(table(mtcars$am))
#' pander(table(mtcars$am, mtcars$gear))
#'
#' ## Tests
#' pander(ks.test(runif(50), runif(50)))
#' pander(chisq.test(table(mtcars$am, mtcars$gear)))
#' pander(t.test(extra ~ group, data = sleep))
#'
#' ## Models
#' ml <- with(lm(mpg ~ hp + wt), data = mtcars)
#' pander(ml)
#' pander(anova(ml))
#' pander(aov(ml))
#' ## Dobson (1990) Page 93: Randomized Controlled Trial (examples from: ?glm)
#' counts <- c(18, 17, 15, 20, 10, 20, 25, 13, 12)
#' outcome <- gl(3, 1, 9)
#' treatment <- gl(3, 3)
#' m <- glm(counts ~ outcome + treatment, family = poisson())
#' pander(m)
#' pander(anova(m))
#' pander(aov(m))
#' ## overwriting labels
#' pander(lm(Sepal.Width ~ Species, data = iris), covariate.labels = c('Versicolor', 'Virginica'))
#'
#' ## Prcomp
#' pander(prcomp(USArrests))
#'
#' ## Others
#' pander(density(runif(10)))
#' pander(density(mtcars$hp))
#'
#' ## default method
#' x <- chisq.test(table(mtcars$am, mtcars$gear))
#' class(x) <- 'I heave never heard of!'
#' pander(x)
pander <- function(x = NULL, ...) {
## save current knitr.auto.asis option
kaao <- panderOptions('knitr.auto.asis')
if (isTRUE(panderOptions('knitr.auto.asis')) &&
isTRUE(getOption('knitr.in.progress')) &&
requireNamespace('knitr', quietly = TRUE)) {
## override knitr.auto.asis option for nested calls
panderOptions('knitr.auto.asis', FALSE)
## grab stdout
stdout <- vector('character')
con <- textConnection('stdout', 'wr', local = TRUE)
sink(con)
sink(con, type = 'message')
## close
on.exit({
## restore knitr.auto.asis option
if (panderOptions('knitr.auto.asis') != kaao) {
panderOptions('knitr.auto.asis', kaao)
}
## revert R output back to normal
sink()
sink(type = 'message')
close(con)
## restore the final line break
if (tail(stdout, 1) == '') {
stdout <- c(stdout, '')
}
return(knitr::asis_output(paste(stdout, collapse = '\n')))
})
}
UseMethod('pander', x)
}
#' Pander and capture output
#'
#' This is a wrapper function around \code{pander} but instead of printing to \code{stdout}, this function returns a character vector of the captured lines.
#' @param ... everything passed to \code{pander}
#' @export
#' @seealso pander
pander_return <- function(...)
capture.output(pander(...))
#' Pander method for a NULL object
#'
#' Prints a NULL object in Pandoc's markdown.
#' @param x a NULL object
#' @param ... ignored parameters
#' @export
pander.NULL <- function(x, ...)
return(invisible(NULL))
#' Helper function to deal with atomic vectors
#' @param x vector
#' @param ... ignored parameters
#' @keywords internal
pander.vector <- function(x, ...) {
if (!is.null(names(x))) {
return(pandoc.table(x))
}
cat(p(x))
}
#' Pander method for logical class
#'
#' Prints a logical object in Pandoc's markdown.
#' @param x a logical object
#' @param ... ignored parameters
#' @export
pander.logical <- function(x, ...)
pander.vector(x, ...)
#' Pander method for numeric class
#'
#' Prints a numeric class in Pandoc's markdown.
#' @param x a numeric object
#' @param ... igroned parameter
#' @export
pander.numeric <- function(x, ...)
pander.vector(x, ...)
#' Pander method for factor class
#'
#' Prints a factor object in Pandoc's markdown.
#' @param x a factor object
#' @param ... igroned parameters
#' @export
pander.factor <- function(x, ...)
pander.vector(x, ...)
#' Pander method for character class
#'
#' Prints a character class in Pandoc's markdown.
#' @param x a character object
#' @param ... igroned parameters
#' @export
pander.character <- function(x, ...) {
if (!is.null(names(x))) {
return(pandoc.table(x))
}
if (length(x) < 2) {
cat(x)
} else {
cat(p(x))
}
}
#' Pander method for image class
#'
#' Prints a image object in Pandoc's markdown.
#' @param x a image object
#' @param caption caption (string) to be shown under the table
#' @param href link that image should be linked with
#' @param ... ignored parameters
#' @export
pander.image <- function(x, caption = attr(x, 'caption'), href = attr(x, 'href'), ...) {
if (is.null(caption) & !is.null(storage$caption)) {
caption <- get.caption()
}
res <- pandoc.image.return(as.character(x), caption)
if (is.null(href)) {
cat(res)
} else {
pandoc.link(href, res)
}
}
#' Pander method for table class
#'
#' Prints a table object in Pandoc's markdown.
#' @param x a table object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.table <- function(x, caption = attr(x, 'caption'), ...) {
if (is.null(caption) & !is.null(storage$caption)) {
caption <- get.caption()
}
pandoc.table(x, caption = caption, ...)
}
#' Pander method for data.table class
#'
#' Prints a data.table object in Pandoc's markdown. Data.tables drop attributes (like row names) when called.
#' @param x a data.table object
#' @param caption caption (string) to be shown under the table
#' @param keys.as.row.names controls whether to use data.table key as row names when calling pandoc.table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.data.table <- function(x, caption = attr(x, 'caption'),
keys.as.row.names = TRUE, ...) {
if (is.null(caption) & !is.null(storage$caption)) {
caption <- get.caption()
}
requireNamespace('data.table', quietly = TRUE)
xx <- data.table::copy(x)
if (keys.as.row.names && data.table::haskey(xx)) {
firstkey <- data.table::key(xx)[1]
row.names.dt <- as.character(xx[[firstkey]])
xx <- xx[, setdiff(colnames(xx), firstkey), with = FALSE, drop = FALSE]
data.table::setattr(xx, 'row.names', row.names.dt)
}
pandoc.table(xx, caption = caption, ...)
}
#' Pander method for data.frame class
#'
#' Prints a data.frame object in Pandoc's markdown.
#' @param x a data.frame object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.data.frame <- function(x, caption = attr(x, 'caption'), ...) {
if (is.null(caption) & !is.null(storage$caption)) {
caption <- get.caption()
}
pandoc.table(x, caption = caption, ...)
}
#' Pander method for matrix class
#'
#' Prints a matrix object in Pandoc's markdown.
#' @param x a matrix object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.matrix <- function(x, caption = attr(x, 'caption'), ...) {
if (is.null(caption) & !is.null(storage$caption)) {
caption <- get.caption()
}
pandoc.table(x, caption = caption, ...)
}
#' Pander method for cast_df class
#'
#' Prints a cast_df object in Pandoc's markdown.
#' @param x a cast_df object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.cast_df <- function(x, caption = attr(x, 'caption'), ...) {
if (is.null(caption) & !is.null(storage$caption)) {
caption <- get.caption()
}
pandoc.table(as.data.frame(x), caption = caption, ...)
}
#' Pander method for summary.lm class
#'
#' Prints a summary.lm object in Pandoc's markdown.
#' @param x an summary.lm object
#' @param caption caption (string) to be shown under the table
#' @param covariate.labels vector to replace covariate lables in the table
#' @param omit vector of variable to omit for priting in resulting table
#' @param summary (defaut:\code{TRUE}) if used for summary.lm or lm
#' @param add.significance.stars if significance stars should be shown for P value
#' @param move.intercept by default, the Intercept is the first coefficient in the table, which can be moved to the bottom of the table
#' @param ... optional parameters passed to special methods and/or raw \code{pandoc.*} functions
#' @return By default this function outputs (see: \code{cat}) the result. If you would want to catch the result instead, then call the function ending in \code{.return}.
#' @export
pander.summary.lm <- function(x, caption = attr(x, 'caption'), covariate.labels,
omit, summary = TRUE, add.significance.stars = FALSE, move.intercept = FALSE, ...) {
if (is.null(caption)) {
if (is.null(storage$caption)) {
caption <- pandoc.formula.return(x$call$formula, text = 'Fitting linear model:')
} else {
caption <- get.caption()
}
}
res <- as.data.frame(x$coefficients)
if (move.intercept && rownames(res)[1] == '(Intercept)' & nrow(res) > 1) {
res <- res[c(2:nrow(res), 1), ]
}
if (!missing(omit)) {
res <- res[!apply(sapply(omit, grepl, row.names(res)), 1, any), ]
}
if (!missing(covariate.labels)) {
row.names(res)[1:length(covariate.labels)] <- covariate.labels
}
if (add.significance.stars) {
res <- cbind(res, ' ' = add.significance.stars(res[, 4]))
}
if (summary) {
pandoc.table(res, ...)
if (inherits(x, 'summary.glm')) {
cat('\n(Dispersion parameter for ', x$family$family, ' family taken to be ',
format(x$dispersion), ')\n\n')
stats <- cbind(paste(format(c('Null', 'Residual'), justify = 'right'), 'deviance:'),
apply(cbind(
format(unlist(x[c('null.deviance', 'deviance')]), digits = panderOptions('digits')),
' on',
format(unlist(x[c('df.null', 'df.residual')])), ' degrees of freedom\n'),
1L, paste, collapse = ' '))
rownames(stats) <- NULL
colnames(stats) <- NULL
pandoc.table(stats, keep.trailing.zeros = TRUE, ...)
} else {
pandoc.table(data.frame(
'Observations' = length(x$residuals),
'Residual Std. Error' = x$sigma,
'$R^2$' = x$r.squared,
'Adjusted $R^2$' = x$adj.r.squared,
check.names = FALSE), keep.trailing.zeros = TRUE, caption = caption, digits = panderOptions('digits'))
}
} else {
pandoc.table(res, caption = caption, ...)
}
}
#' Pander method for summary.glm class
#'
#' Prints a summary.glm object in Pandoc's markdown.
#' @param x an summary.glm object
#' @param caption caption (string) to be shown under the table
#' @param covariate.labels vector to replace covariate lables in the table
#' @param omit vector of variable to omit for priting in resulting table
#' @param summary (defaut:\code{TRUE}) if used for summary.lm or lm
#' @param ... optional parameters passed to special methods and/or raw \code{pandoc.*} functions
#' @return By default this function outputs (see: \code{cat}) the result. If you would want to catch the result instead, then call the function ending in \code{.return}.
#' @export
pander.summary.glm <- function(x, caption = attr(x, 'caption'), covariate.labels, omit, summary = TRUE, ...)
pander.summary.lm(x, caption = caption, summary = summary, covariate.labels = covariate.labels, omit = omit, ...)
#' Pander method for summary.lm class
#'
#' Prints a summary.lm object in Pandoc's markdown.
#' @param x a summary.glm object
#' @param caption caption (string) to be shown under the table
#' @param covariate.labels vector to replace covariate lables in the table
#' @param omit vector of variable to omit for priting in resulting table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.lm <- function(x, caption = attr(x, 'caption'), covariate.labels, omit, ...)
pander.summary.lm(summary(x),
caption = caption,
summary = FALSE,
covariate.labels = covariate.labels,
omit = omit, ...)
#' Pander method for summary.glm class
#'
#' Prints a summary.glm object in Pandoc's markdown.
#' @param x a summary.glm object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.glm <- function(x, caption = attr(x, 'caption'), ...) {
if (is.null(caption)) {
if (is.null(storage$caption)) {
caption <- sprintf('Fitting generalized (%s) linear model: %s',
paste(x$family$family, x$family$link, sep = '/'),
pandoc.formula.return(x$call$formula))
} else {
caption <- get.caption()
}
}
pander.summary.glm(summary(x), caption = caption, summary = FALSE, ...)
}
#' Pander method for summary.aov class
#'
#' Prints a summary.aov object in Pandoc's markdown.
#' @param x a summary.aov object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.summary.aov <- function(x, caption = attr(x, 'caption'), ...) {
res <- unclass(x)[[1]]
if (is.null(caption)) {
if (is.null(storage$caption)) {
caption <- 'Analysis of Variance Model'
} else {
caption <- get.caption()
}
}
pandoc.table(res, caption = caption, ...)
}
#' Pander method for aov class
#'
#' Prints an aov object in Pandoc's markdown.
#' @param x an aov object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.aov <- function(x, caption = attr(x, 'caption'), ...) {
pander(summary(x), caption = caption, ...)
}
#' Pander method for anova class
#'
#' Prints an anova object in Pandoc's markdown.
#' @param x an anova object
#' @param caption caption (string) to be shown under the table
#' @param add.significance.stars if significance stars should be shown for P value
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.anova <- function(x, caption = attr(x, 'caption'), add.significance.stars = FALSE, ...) {
if (is.null(caption)) {
if (is.null(storage$caption)) {
if (!is.null(attr(x, 'heading'))) {
caption <- strsplit(attr(x, 'heading'), '\n')[[1]][1]
}
}
}
if (is.null(caption)) {
caption <- get.caption()
}
if (add.significance.stars) {
x <- cbind(x, ' ' = add.significance.stars(x[, 5]))
}
pandoc.table(x, caption = caption, ...)
if (add.significance.stars) {
cat('Signif. codes: 0 \'\\*\\*\\*\' 0.001 \'\\*\\*\' 0.01 \'\\*\' 0.05 \'.\' 0.1 \' \' 1\n')
}
}
#' Pander method for summary.aovlist class
#'
#' Prints a summary.aovlist object in Pandoc's markdown.
#' @param x a summary.aovlist object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.summary.aovlist <- function(x, caption = attr(x, 'caption'), ...) {
n <- length(x)
if (n == 1) {
pandoc.table(unclass(x[[1]][[1]]), caption, ...)
} else {
z <- x[[1]][[1]]
for (i in 2:n) {
z <- rbind(z, x[[i]][[1]])
}
pandoc.table(z, caption = caption, ...)
}
}
#' Pander method for aovlist class
#'
#' Prints an aovlist object in Pandoc's markdown.
#' @param x an aovlist object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.aovlist <- function(x, caption = attr(x, 'caption'), ...) {
pander(summary(x), caption = caption, ...)
}
#' Pander method for htest class
#'
#' Prints a htest object in Pandoc's markdown.
#' @param x a htest object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.htest <- function(x, caption = attr(x, 'caption'), ...) {
if (is.null(caption)) {
if (is.null(storage$caption)) {
caption <- paste0(x$method, ': `', gsub('( and | by )', '`\\1`', paste(x$data.name, collapse='')), '`')
} else {
caption <- get.caption()
}
}
## we do not know which values are provided
res <- data.frame(placeholder = 'FOO')
## add what we know
if (!is.null(x$statistic)) {
res$'Test statistic' <- as.numeric(x$statistic)
}
if (!is.null(x$parameter)) {
res[names(x$parameter)] <- x$parameter
}
if (!is.null(x$p.value)) {
res$'P value' <- paste(
format(round(x$p.value, panderOptions('round')),
trim = TRUE,
digits = panderOptions('digits'),
decimal.mark = panderOptions('decimal.mark')),
add.significance.stars(x$p.value))
}
if (!is.null(x$alternative)) {
res['Alternative hypothesis'] <- x$alternative
}
if (!is.null(x$estimate)) {
if (!is.null(names(x$estimate))) {
res[names(x$estimate)] <- x$estimate
} else {
res['Estimate'] <- x$estimate
}
}
## drop placeholder
res$placeholder <- NULL
## return
pandoc.table(res, caption = caption, ...)
}
#' Pander method for summary.prcomp class
#'
#' Prints a summary.prcomp object in Pandoc's markdown.
#' @param x a summary.prcomp object
#' @param caption caption (string) to be shown under the table
#' @param summary (default:\code{TRUE}) if extended summary should be printed
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.summary.prcomp <- function(x, caption = attr(x, 'caption'), summary = TRUE, ...) {
if (is.null(caption)) {
if (is.null(storage$caption)) {
caption <- 'Principal Components Analysis'
} else {
caption <- get.caption()
}
}
pandoc.table(x$rotation, caption = caption, ...)
if (summary) {
pandoc.table(x$importance, ...)
}
}
#' Pander method for prcomp class
#'
#' Prints a prcomp object in Pandoc's markdown.
#' @param x a prcomp object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.prcomp <- function(x, caption = attr(x, 'caption'), ...) {
pander(summary(x), caption = caption, summary = FALSE, ...)
}
#' Pander method for density class
#'
#' Prints a density object in Pandoc's markdown.
#' @param x a density object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.density <- function(x, caption = attr(x, 'caption'), ...) {
if (is.null(caption)) {
if (is.null(storage$caption)) {
caption <- sprintf('Kernel density of *%s* (bandwidth: %s)', x$data.name, format(x$bw))
} else {
caption <- get.caption()
}
}
res <- data.frame('Coordinates' = as.numeric(summary(x$x)),
'Density values' = as.numeric(summary(x$y)),
check.names = FALSE)
rownames(res) <- names(summary(1))
pandoc.table(res, caption = caption, ...)
}
#' Pander method for list class
#'
#' Prints a list object in Pandoc's markdown.
#' @param x a list object
#' @param ... ignored parameters
#' @export
pander.list <- function(x, ...) {
## early exit on no values
if (length(x) == 0) {
return(pander(NULL))
}
## match call
mc <- match.call()
if (is.null(mc$indent)) {
indent <- 0
} else {
indent <- eval(mc$indent, parent.frame(1))
}
## replace missing values
w <- which(is.na(x))
if (length(w) > 0) {
x[w] <- panderOptions('missing')
}
## grab elements name (if any)
x.names <- sapply(names(x), function(x) ifelse(x == '', ' *', sprintf(' * **%s**:', x)))
if (length(x.names) == 0) {
x.names <- rep(' *', length(x))
}
## capture pandoc output of list element
res <- paste(unlist(lapply(1:length(x), function(i) {
res.i <- paste(capture.output(pander(x[[i]], indent = 1)), collapse = '\n')
if (grepl('\n', res.i) & !grepl('\n *\\* ', res.i)) {
res.i <- sub('^\n', '\n\n', res.i)
res.i <- pandoc.indent(res.i, 1)
}
paste(x.names[i], res.i)
})), collapse = '\n') #nolint
## indent output
res <- pandoc.indent(res, indent)
## add (comment): end of list (preventing conflicts with forthcoming pandoc blocks)
if (indent == 0) {
res <- paste0(res, '\n\n<!-- end of list -->\n')
}
pandoc.p(res)
}
#' Default Pander method
#'
#' Method to be used, when no exact S3 method for given object is found. Tries to render object as a list
#' @param x an object
#' @param ... optional parameters passed to raw \code{pandoc.list} function
#' @export
pander.default <- function(x, ...) {
warning(sprintf('No pander.method for "%s", reverting to default.', class(x)))
class(x) <- 'list'
pander(x, ...)
}
#' Pander method for evals class
#'
#' Prints a evals object in Pandoc's markdown.
#' @param x a evals object
#' @param ... ignored parameters
#' @export
pander.evals <- function(x, ...) {
o <- pander(x$result)
if (panderOptions('evals.messages')) {
if (!is.null(x$msg$messages)) {
o <- paste0(o, ' **MESSAGE**', pandoc.footnote.return(x$msg$messages))
}
if (!is.null(x$msg$warnings)) {
o <- paste0(o, ' **WARNING**', pandoc.footnote.return(x$msg$warnings))
}
if (!is.null(x$msg$error)) {
o <- paste0(o, ' **ERROR**', pandoc.footnote.return(x$msg$errors))
}
}
cat(o)
}
#' Pander method for rapport class
#'
#' Prints a rapport object in Pandoc's markdown.
#' @param x a rapport object
#' @param ... ignored parameters
#' @export
pander.rapport <- function(x, ...)
print(x)
#' Pander method for POSIXlt class
#'
#' Prints a POSIXlt object in Pandoc's markdown.
#' @param x a POSIXlt object
#' @param ... optional parameters passed to raw \code{pandoc.date} function
#' @export
pander.POSIXlt <- function(x, ...)
pandoc.date(x, ...)
#' Pander method for POSIXct class
#'
#' Prints a POSIXct object in Pandoc's markdown.
#' @param x a POSIXct object
#' @param ... optional parameters passed to raw \code{pandoc.date} function
#' @export
pander.POSIXct <- function(x, ...)
pandoc.date(x, ...)
#' Pander method for Date class
#'
#' Prints a Date object in Pandoc's markdown.
#' @param x a Date object
#' @param ... optional parameters passed to raw \code{pandoc.date} function
#' @export
pander.Date <- function(x, ...)
pandoc.date(x, ...)
#' Pander method for ftable class
#'
#' Prints a ftable object in Pandoc's markdown.
#' @param x a ftable object
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.ftable <- function(x, ...)
pandoc.table(x, ...)
#' Pander method for CrossTable class
#'
#' Prints a CrossTable object in Pandoc's markdown.
#' @param x a CrossTable object
#' @param caption caption (string) to be shown under the table
#' @param digits number of digits of precision
#' @param total.r if to print row totals. Default values is taken from CrossTable object
#' @param total.c if to print column totals. Default values is taken from CrossTable object
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
pander.CrossTable <- function(x, caption = attr(x, 'caption'), digits = panderOptions('digits'),
total.r = x$total.r, total.c = x$total.c, ...) {
if (is.null(caption) & !is.null(storage$caption)) {
caption <- get.caption()
}
nr <- dim(x$t)[1]
nc <- dim(x$t)[2]
if (!is.null(rownames(x$t))) {
nt <- cbind(rownames(x$t), x$t, x$rs)
} else {
nt <- cbind(' ', x$t, x$rs)
}
hdd <- 100
appendlines <- function(nt, xx, hasttl = FALSE, haslbl = FALSE) {
if (!hasttl) {
xx <- cbind(xx, rep('', nr))
}
if (!haslbl) {
xx <- cbind(rep('', nr), xx)
}
n <- dim(nt)[1] / nr
nt <- rbind(nt, xx)
idx <- integer()
k <- 1
l <- nr * n + 1
for (i in 1:nr) {
for (j in 1:n) {
idx <- c(idx, k)
k <- k + 1
}
idx <- c(idx, l)
l <- l + 1
}
nt <- nt[idx, ]
nt
}
if (!is.na(x$expected) && x$expected == TRUE) {
xex <- outer(x$rs, x$cs, '*')
xex <- xex / x$gt
if (is.null(digits)) {
digits <- 1
}
xx <- format(round(xex, digits), ...)
xx <- cbind(rep('Expected N', nr), xx, rep('', nr))
nt <- rbind(nt, xx)
idx <- integer()
for (i in 1:nr) {
idx <- c(idx, i, i + nr)
}
nt <- nt[idx, ]
}
if (x$prop.chisq) {
xx <- (x$CST$expected - x$t) ^ 2 / x$CST$expected
xx <- format(round(xx, digits), trim = TRUE, ...)
xx <- cbind('Chi-square', xx)
nt <- appendlines(nt, xx, haslbl = TRUE)
}
if (!is.na(x$prop.row[1])) {
xx <- cbind(x$prop.row, x$rs / x$gt)
xx <- format(round(xx * hdd, digits), trim = TRUE, ...)
xx <- matrix(paste(xx, '%', sep = ''), nrow = nr,
ncol = nc + 1)
xx <- cbind('Row(%)', xx)
nt <- appendlines(nt, xx, TRUE, haslbl = TRUE)
}
if (!is.na(x$prop.col[1])) {
xx <- format(round(x$prop.col * hdd, digits), trim = TRUE,
...)
xx <- matrix(paste(xx, '%', sep = ''), nrow = nr,
ncol = nc)
xx <- cbind('Column(%)', xx)
nt <- appendlines(nt, xx, haslbl = TRUE)
}
if (!is.na(x$prop.tbl[1])) {
xx <- format(round(x$prop.tbl * hdd, digits), trim = TRUE,
...)
xx <- matrix(paste(xx, '%', sep = ''), nrow = nr,
ncol = nc)
xx <- cbind('Total(%)', xx)
nt <- appendlines(nt, xx, haslbl = TRUE)
}
if (!is.na(x$resid) && x$resid == TRUE && x$expected == TRUE) {
xx <- x$t - xex
xx <- format(round(xx, digits), trim = TRUE, ...)
xx <- cbind('Residual', xx)
nt <- appendlines(nt, xx, haslbl = TRUE)
}
if (!is.na(x$sresid) && x$sresid == TRUE
&& x$expected == TRUE) {
xx <- x$CST$residual
xx <- format(round(xx, digits), trim = TRUE, ...)
xx <- cbind('Std Residual', xx)
nt <- appendlines(nt, xx, haslbl = TRUE)
}
if (!is.na(x$asr[1])) {
xx <- format(round(x$asr, digits), trim = TRUE, ...)
xx <- cbind('Adj Std Resid', xx)
nt <- appendlines(nt, xx, haslbl = TRUE)
}
n <- dim(nt)[1] / nr
idx <- seq(1, dim(nt)[1], n)
nt <- rbind(nt, c(gettext('Total', domain = 'R-descr'), x$cs,
x$gt))
if (!is.na(x$prop.col[1])) {
nt <- rbind(nt, c('', sapply(round(hdd * x$cs / x$gt, digits), function(x) paste(x, '%', sep = '')), ''))
}
len <- dim(nt)[1]
rownames(nt) <- as.character(1:len)
## merging and print
ts <- ifelse(!is.na(x$prop.col[1]), 2, 1)
or <- (nrow(nt) - ts) / nr
nt.nr <- nrow(nt)
nc <- ncol(nt)
if (!is.null(total.r) && !total.r) {
nc <- nc - 1
}
RowData <- x$RowData
ColData <- x$ColData
res <- NULL
for (i in 1:nr) {
res.r <- paste(pandoc.strong.return(nt[1 + or * (i - 1), 1]),
'N',
paste(nt[ (2 + or * (i - 1)) : (i * or), 1], collapse = '\\ \n'),
sep = '\\ \n')
for (j in 2:nc) {
res.r <- cbind(res.r,
paste(' ',
paste(nt[ (1 + or * (i - 1)) : (i * or), j], collapse = '\\ \n'),
sep = '\\ \n'))
}
res <- rbind(res, res.r)
}
res.r <- NULL
if (is.null(total.c) || total.c) {
for (j in 1:nc) {
res.r <- cbind(res.r, paste(nt[ (nt.nr - ts + 1) : nt.nr, j], collapse = '\\ \n'))
}
res <- rbind(res, res.r)
}
cln <- c(ifelse(RowData != '', RowData, ' '), colnames(x$t))
if (is.null(total.r) || total.r) {
cln <- c(cln, 'Total')
}
if (ColData != '') {
cln.t <- paste(c(' ', ColData, rep(' ', nc - 2)), rep('\\\n', nc - 1), sep = '')
cln <- paste(cln.t, cln, sep = '')
}
colnames(res) <- cln
pandoc.table(res, caption = caption, keep.line.breaks = TRUE, ...)
}
#' Pander method for timeseries class
#'
#' Prints a timeseries object in Pandoc's markdown.
#' @param x a timeseries object
#' @param caption caption (string) to be shown under the table
#' @param ... optional parameters passed to raw \code{pandoc.table} function
#' @export
#' @importFrom stats time cycle frequency start end time
pander.ts <- function(x, caption = attr(x, 'caption'), ...) {
if (is.null(caption) & !is.null(storage$caption)) {
caption <- get.caption()
}
if (!is.null(ncol(x))) {
tp.1 <- trunc(time(x))
tp.2 <- trunc(cycle(x))
day.abb <- c('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri',
'Sat')
row.names <- switch(frequency(x), tp.1, 'Arg2', 'Arg3',
paste(tp.1, c('Q1', 'Q2', 'Q3', 'Q4')[tp.2], sep = ' '),
'Arg5', 'Arg6', paste('Wk.', tp.1, ' ', day.abb[tp.2],
sep = ''), 'Arg8', 'Arg9', 'Arg10', 'Arg11',
paste(tp.1, month.abb[tp.2], sep = ' '))