-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_pandoc.r
1203 lines (1037 loc) · 35.8 KB
/
report_pandoc.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
library(ggplot2)
#library(knitr)
library(pander)
panderOptions("round",4)
panderOptions("table.style","rmarkdown") #"grid"
panderOptions("table.split.table",Inf)
panderOptions("table.alignment.default","left")
panderOptions("evals.messages",F)
#panderOptions("graph.fontsize",10)
evalsOptions("cache",F)
evalsOptions("cache.mode","environment")
#evalsOptions("output",c("result"))
evalsOptions("output",c("all"))
evalsOptions("graph.output","svg") #"png"
evalsOptions("graph.unify",F)
evalsOptions("width",800) #800 #1000
evalsOptions("height",600) #640 #840
evalsOptions("res",75)
evalsOptions("hi.res",T)
evalsOptions("hi.res.width",1200)
evalsOptions("graph.env",F)
evalsOptions("graph.recordplot",F)
evalsOptions("graph.RDS",F)
##using graph.name option causes seemingly unconnected
##errors starting with warnings like:
##`No pander method for "ggplot", reverting to default`
##described in the URL but still not solved apparently
#https://github.com/Rapporter/rapport/issues/98
#evalsOptions("graph.name","plot-%d-%n")
## This is ggplot2 function that changes
## text base size within a current theme for entire session
#theme_set(theme_gray(base_size = 20))
theme_set(theme_bw(base_size = 20))
make.global <- function(var) {
assign(deparse(substitute(var)),var,envir=globalenv())
}
#' patch generated HTML report
#' TODO: reimplement with pure R to work on Windows
.pander_patch_html_report <- function() {
## Linux, Mac etc Unix-like
## The dollar sign in the substitution pattern is a work-around the inability of Mac OS sed to understand the newline eascape sequence.
## It only understands the actual newline after the line contunuation backlash. The dollar sign is passed to the shell, and the lines
## are concatenated to build sed pattern string: https://superuser.com/questions/307165/newlines-in-sed-on-mac-os-x
## On Ubuntu, the shell that understands the dollar sign trick should be `bash`, not `sh`
##
## Using the -i non-empty extension and deleting is the only way that works for both Mac OS, latest Ubuntu and CentOS 6
if(.Platform$OS.type == "unix") {
script = "#!/bin/bash
find . -name '*.html' | xargs sed -i'.sed.bak' -e 's/href=\"http:/href=\"https:/'
find . -name '*.html' | xargs sed -i'.sed.bak' -e 's/src=\"http:/src=\"https:/'
find . -name '*.html' | xargs sed -i'.sed.bak' -e '/stylesheets\\/skeleton.css/s/$/\\'$'\n''<style>\\'$'\n''\\.container \\{ width: 100\\%; \\}\\'$'\n''\\.container \\.twelve\\.columns \\{ width: 80\\%; \\}\\'$'\n''\\.container \\.three\\.columns \\{ width: 20\\%; \\}\\'$'\n''<\\/style>\\'$'\n''/'
rm *.sed.bak
"
script_fn = ".pander_patch_html_report.sh"
writeLines(script,script_fn)
system2("/bin/bash",script_fn)
}
else {
warning("Not running on a Unix-like platform - patching of HTML report is not implemented yet.")
}
}
# print a named list as a string of named function arguments
arg.list.as.str<-function(x,collapse=",") {
paste("[",
paste(capture.output(str(x,no.list=T,comp.str="",give.attr=F,give.head=F)),collapse=collapse),
"]",
sep=""
)
}
are.automatic.rownames <- function(df) {
all(rownames(df) == paste(seq(nrow(df))))
}
tempfile.unix <- function(...) {
x = tempfile(...)
gsub("\\","/",x,fixed=T)
}
html.path <- function(...) {
paste(...,sep="/")
}
#' Write citations for a vector of package names into file in BibTex format
#' TODO can just use bibtex::write.bib
#' TODO after writing (or before if BibTex allows that type), optionally replace
#' Manual type with TechReport that Zotero understands in BibTex (converts to Report).
#' Otherwise Zotero
#' replaces Manual with Book. The replacement parameter should be a list of
#' to:from tuples. In the path BibTex -> Zotero -> RIS -> Endnote Web Page
#' gets converted to Journal Article still. It seems that in certain styles in
#' EndNote (ACS), the only way too show URL is to set type to Web Page. Otherwise
#' it is not clear at all that packages are CRAN packages.
citation.to.file <- function(package,file.name,append=F,...) {
cit = unlist(sapply(package,function(p) toBibtex(citation(p,...))))
write(cit,file.name,append=append)
}
#' Adopted from phyloseq code
#' Computes text size of axis label based on the number of
#' labels.
#' Maybe R strwidth can be used even with ggplot2?
calc.text.size <- function(n, mins=0.5, maxs=4, B=6, D=100){
# empirically selected size-value calculator.
s <- B * exp(-n/D)
# enforce a floor.
s <- ifelse(s > mins, s, mins)
# enforce a max
s <- ifelse(s < maxs, s, maxs)
return(s)
}
mget.stack<-function(x,ifnotfound) {
pars = lapply(rev(sys.parents()),sys.frame)
#print(pars)
#print(sys.parent())
#print(sys.frames())
for(fr in pars) {
y = tryCatch(get(x,envir=fr,inherits=F),error=function(x) NULL)
#print(paste("y=",y,"ifnotfound=",ifnotfound))
if(!is.null(y)) {
#print(paste("y=",y,"ifnotfound=",ifnotfound))
return (y)
}
}
return (ifnotfound)
}
new_section_path_el <- function(num=0,sub=F,has.header=F) {
list(num=num,sub=sub,has.header=has.header)
}
extract.path.nums.section.path <- function(x) {
sapply(x,function(y) y$num)
}
extract.path.nums.report.section <- function(x) {
extract.path.nums.section.path(x$path)
}
extract.path.subs.section.path <- function(x) {
sapply(x,function(y) y$sub)
}
extract.path.subs.report.section <- function(x) {
extract.path.subs.section.path(x$path)
}
get.sub.level.section.path <- function(x) {
sum(extract.path.subs.section.path(x))
}
cut.to.bottom.sub.section.path <- function(x) {
subs = extract.path.subs.section.path(x)
pos = length(subs) - match(T,rev(subs)) + 1
if(is.na(pos)) {
return (list())
}
else {
if(pos>1)
return (x[1:pos-1])
else
return (list())
}
}
get.default.section<-function() {
x = new.env(parent=emptyenv())
x$path = list(new_section_path_el())
return (x)
}
clone.report.section<-function(x) {
return (as.environment(as.list(x, all.names=TRUE)))
}
get.report.section<-function(default=get.default.section()) {
y = mget.stack("report.section",ifnotfound=0)
if(!identical(y,0)) return (y)
return (default)
}
## when x is NULL, this function creates a clone
push.report.section<-function(x=NULL,sub=F,has.header=F) {
if(is.null(x)) {
x = get.report.section(default=NULL)
if(is.null(x)) {
x = get.default.section()
}
else {
x = clone.report.section(x)
}
}
x$path[[length(x$path)+1]] = new_section_path_el(num=0,sub=sub,has.header=has.header)
return (x)
}
pop.report.section<-function(x=NULL) {
if(is.null(x)) {
x = get.report.section(default=NULL)
if(is.null(x)) {
x = get.default.section()
}
}
l = length(x$path)
if(l>1)
x$path = x$path[1:l-1]
return (x)
}
incr.report.section<-function(x=NULL) {
if(is.null(x)) {
x = get.report.section(default=NULL)
if(is.null(x)) {
x = get.default.section()
}
}
last = length(x$path)
if(last>0) {x$path[[last]]$num = x$path[[last]]$num + 1}
else {x$path[[1]]$num = 1}
return (x)
}
incr.report.section.if.zero <- function(x=NULL) {
if(is.null(x)) {
x = get.report.section()
}
last = length(x$path)
if(last>0 && x$path[[last]]$num == 0) {x$path[[last]]$num = x$path[[last]]$num + 1}
return (x)
}
format.section.path<-function(x=NULL) {
if(is.null(x)) {
x = get.report.section()$path
}
num = extract.path.nums.section.path(x)
return (paste("\\(",paste(num,sep="",collapse="."),"\\)",sep=""))
}
format.report.section<-function(x=NULL) {
if(is.null(x)) {
x = get.report.section()
}
format.section.path(x$path)
}
format.section.path.as.file<-function(x=NULL) {
if(is.null(x)) {
x = get.report.section()$path
}
num = extract.path.nums.section.path(x)
return (paste(num,sep="",collapse="."))
}
format.report.section.as.file<-function(x=NULL) {
if(is.null(x)) {
x = get.report.section()
}
format.section.path.as.file(x$path)
}
pandoc.as.printed.return <- function(x,attrs="") {
x = capture.output(print(x))
paste0('\n', repChar('`', 7),
ifelse(attrs == '', '', sprintf('{%s}', attrs)),
'\n', paste(x,
collapse = '\n'),
'\n', repChar('`', 7), '\n')
}
pandoc.special.symb = "\\-\\[\\]`*_{}()#+!~"
pandoc.escape.special <- function(x) {
x = gsub(paste('([',pandoc.special.symb,'])',sep=''),"\\\\\\1",
format(x,digits=panderOptions("digits")),perl=T)
##the only way to have backstick is table cells is to use special symbol
gsub('[|]','|',x)
}
pandoc.link.verbatim.return <- function(url,text=NULL) {
if(is.null(text)) {
text = url
}
t_len = length(text)
u_len = length(url)
if(t_len==1) {
text = rep(text,u_len)
}
else if(u_len==1) {
url = rep(url,t_len)
}
else if(t_len!=u_len) stop("Lengths of url and text arguments are not compatible")
sapply(seq_along(url),function(i) { pandoc.link.return(url[i],pandoc.verbatim.return(text[i]))})
}
pandoc.anchor.return <- function(anchor,text) {
anchor.tag = sprintf('<a name="%s"></a>',anchor)
ret = sprintf("%s%s",
anchor.tag,
pandoc.link.verbatim.return(sprintf("#%s",anchor),
text))
return(ret)
}
str_match_all_group <- function(string,pattern,group=1) {
lapply(stringr::str_match_all(string, pattern),function(x) if(length(x)>0) x[,group+1] else c())
}
pandoc.replace.patt.with.links <- function(string,pattern,url_templ="",collapse=",",default="") {
sapply(str_match_all_group(string,pattern),
function(x) if(!is.null(x)) paste(pandoc.link.verbatim.return(sprintf(url_templ,x),x),collapse = collapse) else default)
}
## make string x a (more or less) valid file name
str.to.file.name <- function(x,max.length=0) {
x = gsub('[^-[:alnum:]._]+','.',x)
## R Studio's own Web server does not like file names with double .. in them.
## For example, if you generate here "something.", then append extension ".html"
## and get "something..html", and try to open it from the Files tab, you will get
## file/something..html not found. Below we zap all double dots, and any tailing dots
## after the max length cut has been applied.
x = gsub('[.]+','.',x)
if(max.length>0) {
x = substring(x,1,max.length)
}
x = gsub('[.]+$','',x)
if(stringr::str_length(x)==0) {
x = "file"
}
x
}
PandocAT <- setRefClass('PandocAT', contains = "Pandoc",
fields = list(
'sections' = 'list',
'incremental.save' = 'logical',
'out.file' = 'character',
'out.formats' = 'character',
'portable.html' = 'logical',
'object.index' = 'list',
'data.dir' = 'character',
'widget.dir' = 'character',
'widget.deps.dir' = 'character',
'graph.dir' = 'character'
)
)
PandocAT$methods(initialize = function(
author = "Anonymous",
title = "Analysis",
out.file = "report",
out.formats = c("html"),
incremental.save = F,
portable.html=T,
...
) {
#.self$author=author
#.self$title=title
.self$out.file=out.file
.self$out.formats=out.formats
.self$incremental.save=incremental.save
.self$portable.html=portable.html
.self$object.index=list(table=1,figure=1)
.self$data.dir = "data"
unlink(.self$data.dir,recursive=T,force=T)
dir.create(.self$data.dir, showWarnings = FALSE, recursive = TRUE)
.self$graph.dir = evalsOptions("graph.dir")
unlink(.self$graph.dir,recursive=T,force=T)
dir.create(.self$graph.dir, showWarnings = FALSE, recursive = TRUE)
.self$widget.dir = "." #normalizePath(graph.dir,winslash = "/")
.self$widget.deps.dir = "widget_deps" #html.path(.self$widget.dir,"widget_deps")
dir.create(.self$widget.deps.dir, showWarnings = FALSE, recursive = TRUE)
callSuper(author=author,title=title,...)
})
## private service method - should be called whenever an element is
## appended to the .self$body
PandocAT$methods(priv.append.section = function() {
incr.report.section.if.zero()
.self$sections[[length(.self$sections)+1]] = get.report.section()$path
})
## private service method - should be called whenever an element
## that needs its own index in the report is
## appended to the .self$body
PandocAT$methods(priv.append.index = function(type) {
val = .self$object.index[[type]]
if(is.null(val)) {
val = .self$object.index[[type]] = 1
}
.self$object.index[[type]] = val + 1
return(val)
})
PandocAT$methods(format.caption = function(caption,section=NULL,type=NULL) {
if(!is.null(caption)) {
if(is.null(type)) {
type = ""
ind = ""
}
else {
ind = .self$priv.append.index(type)
}
if(nzchar(ind)) {
anchor = sprintf('%s.%s',type,ind)
anchor.name = sprintf("%s %s.",Hmisc::capitalize(type),ind)
name = pandoc.anchor.return(anchor,anchor.name)
}
else {
name = ""
}
caption = paste(format.report.section(section),name,caption)
if(substr(caption,nchar(caption),nchar(caption)+1)!=".") {
caption = paste(caption,".",sep="")
}
}
return (caption)
})
PandocAT$methods(add.widget = function(x,new.paragraph=T,
caption=NULL,
show.image.links=T,
width = 800,
height = 800,
data.export = NULL,
data.export.descr = NULL,
show.inline = T,
...) {
require(htmlwidgets)
if(new.paragraph) {
.self$add.p("")
}
name.base=paste(str.to.file.name(caption,20),".html",sep="")
fn = .self$make.file.name(name.base,dir=.self$widget.dir,make.unique=T)
saveWidget(x,fn,selfcontained = F,libdir=.self$widget.deps.dir)
caption.res = sprintf("Click to see HTML widget file in full window: %s",
pandoc.link.verbatim.return(fn))
caption.type = "widget"
if(!is.null(caption)) {
caption = .self$format.caption(caption,type=caption.type)
}
caption = paste(caption,caption.res)
if(!is.null(data.export)) {
caption = sprintf("%s. Dataset is also saved here: %s",
caption,
pandoc.link.verbatim.return(data.export))
if(!is.null(data.export.descr)) {
caption = sprintf("%s, %s",caption,data.export.descr)
}
}
if(!is.null(caption)) {
.self$add.p(caption)
}
if(show.inline) {
if(is.null(width)) {
width = evalsOptions("width")
}
if(is.null(height)) {
height = evalsOptions("height")
}
iframe.tpl = '<iframe style="max-width=100%"
src="fn"
sandbox="allow-same-origin allow-scripts"
width="100%"
height="%s"
scrolling="no"
seamless="seamless"
frameBorder="0"></iframe>'
iframe.tpl = '<iframe src="%s" width="%s" height="%s"> </iframe>'
report$add(sprintf(iframe.tpl,
fn,
width,
height))
}
return(x)
})
PandocAT$methods(add = function(x,new.paragraph=T,
caption=NULL,
show.image.links=T,
caption.type=NULL,
graph.output = pander::evalsOptions("graph.output"),
hi.res = pander::evalsOptions("hi.res"),
...) {
timer <- proc.time()
par_fr <- parent.frame()
if(!identical(.GlobalEnv, par_fr)) {
env <- list2env(as.list(par_fr, all.names=TRUE),parent=parent.frame(2))
}
else {
env = NULL
}
## work around pander bug in v.0.6.0 where hi.res is created as a broken symlink plots/normal.res
## instead of just normal.res
if(graph.output == 'svg') {
hi.res = F
}
res <- pander::evals(deparse(match.call()[[2]]),env=env,
graph.output = graph.output,
hi.res = hi.res,
...)
if(new.paragraph) {
.self$add.p("")
}
is.image = F
caption.res = ""
for (r in res) {
if(any(r$type=="image")) {
if(show.image.links) {
rr = r$result
caption.res = paste(caption.res,
sprintf("Image file: %s.",
pandoc.link.verbatim.return(as.character(rr)))
)
hres.ref = attr(rr,"href")
if(!is.null(hres.ref)) {
caption.res = paste(caption.res,
sprintf("High resolution image file: %s.",
pandoc.link.verbatim.return(hres.ref))
)
}
}
is.image = T
}
}
if(is.null(caption.type)) {
if(is.image) {
caption.type = "figure"
}
}
if(!is.null(caption)) {
caption = .self$format.caption(caption,type=caption.type)
}
if(nzchar(caption.res)) {
caption = paste(caption,caption.res)
}
if(!is.null(caption)) {
.self$add.p(caption)
}
.self$body <- c(.self$body,res)
.self$priv.append.section()
.self$proc.time <- .self$proc.time + as.numeric(proc.time() - timer)[3]
return(res)
})
PandocAT$methods(add.list = function(x,...) {
return(.self$add(as.list(x),...))
})
PandocAT$methods(reset.section = function(...) {
return(NULL)
})
PandocAT$methods(default.section = function(...) {
return(get.default.section(...))
})
PandocAT$methods(get.section = function(...) {
return(get.report.section(...))
})
PandocAT$methods(incr.section = function(...) {
return(incr.report.section(...))
})
PandocAT$methods(push.section = function(...) {
return(push.report.section(...))
})
PandocAT$methods(pop.section = function(...) {
return(pop.report.section(...))
})
PandocAT$methods(add.file = function(x,
caption=NULL,
wrap.caption=T,
skip.if.empty=F,
...) {
if (wrap.caption && !is.null(caption)) {
caption = pandoc.escape.special(caption)
}
caption = .self$format.caption(caption,type="dataset")
if(is.null(x)) {
if(!skip.if.empty) {
if(!is.null(caption)) {
.self$add.p(caption)
}
return(.self$add.p("Empty dataset"))
}
else {
return(.self)
}
}
caption = paste(caption,
"Dataset is saved in a file (click to download)",
pandoc.link.verbatim.return(x)
)
return(.self$add.p(caption))
})
PandocAT$methods(add.table = function(x,
show.row.names=is.matrix(x),
echo=T,
caption=NULL,
wrap.vals=T,
wrap.caption=T,
split.tables=Inf,
style="rmarkdown",
export.to.file=T,
file.name=NULL,
show.first.rows=200,
show.first.cols=200,
skip.if.empty=F,
...) {
if (wrap.caption && !is.null(caption)) {
caption = pandoc.escape.special(caption)
}
caption = .self$format.caption(caption,type="table")
if(is.null(x) || nrow(x)==0) {
if(!skip.if.empty) {
if(!is.null(caption)) {
.self$add.p(caption)
}
return(.self$add.p("Empty dataset"))
}
else {
return(.self)
}
}
if(show.first.rows > 0) {
if(show.first.rows >= nrow(x)) {
show.first.rows = 0
}
}
if(show.first.rows > 0) {
caption = paste(caption,sprintf("Showing only %s first rows.",show.first.rows))
}
if(show.first.cols > 0) {
if(show.first.cols >= ncol(x)) {
show.first.cols = 0
}
}
if(show.first.cols > 0) {
caption = paste(caption,sprintf("Showing only %s first columns.",show.first.cols))
}
if(export.to.file) {
file.name = .self$write.table.file(x,
name.base=paste(str.to.file.name(caption,20),".csv",sep=""),
descr=NULL,
row.names=show.row.names,
row.names.header=T,
file.name=file.name)
caption = paste(caption,
"Full dataset is also saved in a delimited text file (click to download and open e.g. in Excel)",
pandoc.link.return(file.name,pandoc.verbatim.return(file.name))
)
}
if(show.first.rows > 0) {
if(inherits(x,"data.table")) x = x[1:show.first.rows]
else x = x[1:show.first.rows,,drop=F]
}
if(show.first.cols > 0) {
if(inherits(x,"data.table")) x = x[,1:show.first.cols,with=F]
else x = x[,1:show.first.cols,drop=F]
}
## With data.table, I am getting this message:
## `data.table inherits from data.frame (from v1.5) but this data.table does not`
## when calling `rn = rownames()` below. Converting to data.frame here to get rid of it.
if(inherits(x,"data.table")) x = as.data.frame(x)
if(!show.row.names) {
rownames(x) <- NULL
}
if(wrap.vals) {
rn = rownames(x)
if(show.row.names && !are.automatic.rownames(x)) {
rn = pandoc.escape.special(rn)
}
if(is.matrix(x)) {
x = as.data.frame(x)
}
x = sapply(x,pandoc.escape.special,USE.NAMES=F,simplify=T)
if(!is.matrix(x)) {
x = t(as.matrix(x))
}
rownames(x) = rn
colnames(x) = pandoc.escape.special(colnames(x))
}
.self$add.p(caption)
tbl_p <- pandoc.table.return(x,split.tables=split.tables,style=style,caption=NULL,...)
if(echo) {
print(tbl_p)
}
return(.self$add.p(tbl_p))
})
PandocAT$methods(add.vector = function(x,name=NULL,
show.row.names=T,
caption=NULL,
...) {
if(is.null(x) || length(x)==0) {
if(!is.null(caption)) {
.self$add.p(.self$format.caption(caption))
}
return(.self$add.p("Empty dataset"))
}
y = data.frame(x=x)
if(!is.null(name)) {
names(y) <- c(name)
}
if(is.null(names(x))) {
show.row.names = F
}
if(show.row.names) {
row.names(y) <- names(x)
}
return(.self$add.table(y,caption=caption,show.row.names=show.row.names,...))
})
PandocAT$methods(add.p = function(x,rule=F,echo=T,...) {
if(rule) {
.self$priv.append.section()
.self$add.paragraph(pandoc.horizontal.rule.return())
}
if(echo) {
cat(format.report.section(),x,"\n")
}
.self$priv.append.section()
return(.self$add.paragraph(x,...))
})
PandocAT$methods(add.descr = function(x,...) {
.self$add.p(x,...)
})
PandocAT$methods(add.package.citation = function(x,...) {
.self$add.p(capture.output(print(citation(x),style="text")))
})
PandocAT$methods(add.printed = function(x,caption=NULL,echo=T,...) {
if(!is.null(caption)) {
.self$add.p(.self$format.caption(caption))
}
return(.self$add.p(pandoc.as.printed.return(x,...),echo=echo))
})
PandocAT$methods(add.header = function(x,level=NULL,report.section=NULL,section.action="incr",echo=T,sub=F,...) {
if(sub) {
section.action = "push"
}
do.clone = is.null(report.section)
report.section = switch(section.action,
incr=incr.report.section(report.section),
push=incr.report.section(report.section),
keep=get.report.section(report.section))
num = extract.path.nums.report.section(report.section)
if (is.null(level)) {
##headers will shift to the left above level 5 in HTML output
level = min(5,length(num))
}
x = paste(format.report.section(report.section),x)
##newlines currently break header formatting, remove them
x = gsub("\n"," ",x)
.self$add.p(pandoc.header.return(x,level=level,...),echo=echo)
if(section.action=="push") {
rep.sec.push = NULL
if(!do.clone) rep.sec.push = report.section
#w/o argument it clones
report.section = push.report.section(rep.sec.push,sub=sub,has.header=T)
}
if(.self$incremental.save) {
.self$save()
}
return (report.section)
})
PandocAT$methods(make.plot.file.name = function(name.base="",
make.unique=T,
section.path=NULL) {
.self$make.file.name(name.base = name.base,
make.unique = make.unique,
section.path = section.path,
dir = .self$graph.dir)
})
PandocAT$methods(make.file.name = function(name.base="",
make.unique=T,
dir=NULL,
section.path=NULL) {
if(is.null(dir)) {
dir = .self$data.dir
}
if(length(name.base)==0) {
name.base = ""
}
if(name.base=="" && !make.unique) {
stop("Need either non-empty name.base or make.unique=T")
}
fn.start = format.section.path.as.file(section.path)
if(make.unique) {
fn = tempfile.unix(paste(fn.start,"-",sep=""),tmpdir=dir,fileext=name.base)
}
else {
fn = file.path(dir,paste(fn.start,name.base,sep="-"),fsep="/")
}
return(fn)
})
## Save data as a delimited text file
## ... are optional arguments to write.table
PandocAT$methods(write.table.file = function(data,
name.base,
make.unique=T,
descr=NULL,
row.names=F,
row.names.header=T,
file.name=NULL,
...) {
## if we write row.names, Excel shifts header row to the left when loading
if(row.names && row.names.header) {
data = cbind(rownames=rownames(data),data)
row.names=F
}
if(is.null(file.name)) {
fn = .self$make.file.name(name.base,make.unique=make.unique)
}
else {
fn = file.name
}
write.csv(data,
fn,
row.names = row.names,
...)
if (!is.null(descr)) {
.self$add.descr(paste("Wrote",descr,"to file",
pandoc.link.return(fn,fn)))
}
return(fn)
})
PandocAT$methods(save = function(out.file.loc,out.formats.loc,portable.html.loc,sort.by.sections=F) {
if (missing(out.file.loc)) {
out.file.loc = .self$out.file
if(missing(out.file.loc)) {
out.file.loc = "report"
}
}
if (missing(out.formats.loc)) {
out.formats.loc = .self$out.formats
if(missing(out.formats.loc)) {
out.formats.loc = c("html")
}
}
if (missing(portable.html.loc)) {
portable.html.loc = .self$portable.html
if(missing(portable.html.loc)) {
portable.html.loc = T
}
}
fp <- out.file.loc
timer <- proc.time()
fp.all = list()
f_sections = .self$sections
f_body = .self$body
if(sort.by.sections) {
##sort by section lexicographically, using a stable sort
sect_ord = sort.list(
unlist(lapply(.self$sections,format.section.path)),
method="shell")
f_body = f_body[sect_ord]
}
write.el <- function(el,fp) {
el.str = pander_return(el$result)
cat(paste(el.str, collapse = '\n'),
file = fp, append = TRUE)
}
for(i.el in seq_along(f_body)) {
section = f_sections[[i.el]]
el = f_body[[i.el]]
section.par = cut.to.bottom.sub.section.path(section)
#print(paste("Full section:",paste(section,collapse=" ")))
#print(paste("Par section:",paste(section.par,collapse=" ")))
if(length(section.par) > 0) {
sub.path = section.par
}
else {
sub.path = NULL
}
fp.sub = make.file.name(name.base=fp,
make.unique=F,
dir=".",
section.path=sub.path)
fp.sub.md = paste(fp.sub,".Rmd",sep="") #".md"
#print(paste("fp.sub=",fp.sub))
if(is.null(fp.all[[fp.sub.md]])) {
cat(pandoc.title.return(.self$title, .self$author, .self$date), file = fp.sub.md)
}
if(i.el>1) {
sub.level.prev = get.sub.level.section.path(f_sections[[i.el-1]])
sub.level = get.sub.level.section.path(section)
if(sub.level > sub.level.prev) {
cat(pandoc.link.verbatim.return(paste(fp.sub,".html",sep=""),"Subreport"), #".html"
file = fp.sub.md.prev, append = TRUE)
if(section[[length(section)]]$has.header) {
write.el(f_body[[i.el-1]],fp.sub.md)
}
}
}
write.el(el,fp.sub.md)
fp.all[[fp.sub.md]] = 1
fp.sub.md.prev = fp.sub.md
}
for(out.format in out.formats.loc) {
for(fp.sub.md in names(fp.all)) {
## It would be nice to add `options="-s -S"` to support
## Pandoc's subscript and suprscript extensions, but
## this will entirely replace internal default options and
## break TOC etc
cat(sprintf("Pandoc converting markdown file %s to %s format\n",fp.sub.md,out.format))
Pandoc.convert(fp.sub.md,format=out.format,open=F,footer=F,
portable.html=portable.html.loc)
}
}
.pander_patch_html_report()
})
tmp.save <- function(report,out.file.loc,out.formats.loc,portable.html.loc,sort.by.sections=F) {
.self = report$.self
if (missing(out.file.loc)) {
out.file.loc = .self$out.file
if(missing(out.file.loc)) {
out.file.loc = "report"
}
}
if (missing(out.formats.loc)) {
out.formats.loc = .self$out.formats
if(missing(out.formats.loc)) {
out.formats.loc = c("html")
}