Debian Codegraph @

R/R5.R

raw
1 #' Reporting with Pandoc
2 #'
3 #' This \code{R5} reference class can hold bunch of elements (text or R objects) from which it tries to create a Pandoc's markdown text file. Exporting the report to several formats (like: PDF, docx, odt etc. - see Pandoc's documentation) is also possible, see examples below.
4 #' @param ... this is an R5 object without any direct params but it should be documented, right?
5 #' @export Pandoc
6 #' @examples \dontrun{
7 #' ## Initialize a new Pandoc object
8 #' myReport <- Pandoc$new()
9 #'
10 #' ## Add author, title and date of document
11 #' myReport$author <- 'Anonymous'
12 #' myReport$title <- 'Demo'
13 #'
14 #' ## Or it could be done while initializing
15 #' myReport <- Pandoc$new('Anonymous', 'Demo')
16 #'
17 #' ## Add some free text
18 #' myReport$add.paragraph('Hello there, this is a really short tutorial!')
19 #'
20 #' ## Add maybe a header for later stuff
21 #' myReport$add.paragraph('# Showing some raw R objects below')
22 #'
23 #' ## Adding a short matrix
24 #' myReport$add(matrix(5,5,5))
25 #'
26 #' ## Or a table with even # TODO: caption
27 #' myReport$add.paragraph('Hello table:')
28 #' myReport$add(table(mtcars$am, mtcars$gear))
29 #'
30 #' ## Or a "large" data frame which barely fits on a page
31 #' myReport$add(mtcars)
32 #'
33 #' ## And a simple linear model with Anova tables
34 #' ml <- with(lm(mpg ~ hp + wt), data = mtcars)
35 #' myReport$add(ml)
36 #' myReport$add(anova(ml))
37 #' myReport$add(aov(ml))
38 #'
39 #' ## And do some principal component analysis at last
40 #' myReport$add(prcomp(USArrests))
41 #'
42 #' ## Sorry, I did not show how Pandoc deals with plots:
43 #' myReport$add(plot(1:10)) # TODO: caption
44 #'
45 #' ## Want to see the report? Just print it:
46 #' myReport
47 #'
48 #' ## Exporting to PDF (default)
49 #' myReport$export()
50 #'
51 #' ## Or to docx in tempdir:
52 #' myReport$format <- 'docx'
53 #' myReport$export(tempfile())
54 #'
55 #' ## You do not want to see the generated report after generation?
56 #' myReport$export(open = FALSE)
57 #' }
58 #' @importFrom methods setRefClass new getGeneric
59 Pandoc <- setRefClass('Pandoc', fields = list('author' = 'character',
60 'title' = 'character',
61 'date' = 'character',
62 'body' = 'list',
63 'format' = 'character',
64 'proc.time' = 'numeric'))
65
66 Pandoc$methods(initialize = function(author = 'Anonymous',
67 title = base::sprintf('%s\'s report', author),
68 date = base::date(),
69 format = 'pdf', ...) {
70
71 .self$author <- author
72 .self$title <- title
73 .self$date <- date
74 .self$format <- format
75 .self$proc.time <- 0
76 callSuper(...)
77
78 })
79
80 Pandoc$methods(add = function(x) {
81
82 timer <- proc.time()
83 res <- evals(deparse(match.call()[[2]]),
84 env = parent.frame(),
85 graph.name = evalsOptions('graph.name'),
86 graph.dir = evalsOptions('graph.dir'),
87 graph.output = evalsOptions('graph.output'),
88 width = evalsOptions('width'),
89 height = evalsOptions('height'))
90 .self$body <- c(.self$body, res)
91 .self$proc.time <- .self$proc.time + as.numeric(proc.time() - timer)[3]
92
93 })
94
95 Pandoc$methods(add.paragraph = function(x) .self$body <- c(.self$body, list(list(result = pandoc.p.return(x)))))
96
97 Pandoc$methods(show = function(x) {
98
99 ## starting timer for proc.time
100 timer <- proc.time()
101
102 ## show header
103 cat(pandoc.header(.self$title, style = 'setext'))
104 cat(' written by *', .self$author, '* at *', .self$date, '*\n', sep = '')
105 cat('\n', sprintf(' This report holds %s block(s).', length(.self$body)), '\n')
106
107 ## show body (if not empty)
108 if (length(.self$body) > 0) {
109
110 cat(pandoc.horizontal.rule())
111
112 lapply(.self$body, function(x) {
113 for (m in c('messages', 'warnings', 'errors')) {
114 if (!is.null(x$msg[[m]])) {
115 cat('\n**', toupper(sub('s$', '', m)), ':** ', x$msg[[m]], sep = '')
116 }
117 }
118 pander(x$result)
119 })
120
121 ## show proc.time
122 cat(pandoc.horizontal.rule())
123 cat('\nProc. time: ', .self$proc.time + as.numeric(proc.time() - timer)[3], 'seconds. \n\n')
124
125 } else {
126 cat('\n)')
127 }
128
129 })
130
131 Pandoc$methods(export = function(f, ...) {
132
133 if (missing(f)) {
134 f <- tempfile('pander-', getwd())
135 }
136 fp <- sprintf('%s.md', f)
137 timer <- proc.time()
138
139 ## create pandoc file
140 cat(pandoc.title.return(.self$title, .self$author, .self$date), file = fp)
141 lapply(.self$body,
142 function(x) cat(paste(capture.output(pander(x$result)), collapse = '\n'), file = fp, append = TRUE))
143
144 ## convert
145 fe <- Pandoc.convert(fp, format = .self$format, proc.time = as.numeric(proc.time() - timer)[3], ...)
146
147 ## return
148 cat('\nExported to *', f, '.[md|', format, ']* under ',
149 .self$proc.time + as.numeric(proc.time() - timer)[3], ' seconds.\n\n', sep = '')
150
151 return(invisible(fe))
152
153 })