forked from tomwenseleers/export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table2office.R
312 lines (289 loc) · 11.5 KB
/
table2office.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
#' Export statistical output to a table in Microsoft Office / LibreOffice format
#'
#' Export currently showing R stats object or stats object obj to a Microsoft
#' Office / LibreOffice table
#'
#' @importFrom utils methods
#' @import stats
#' @importFrom xtable xtable
#' @importFrom broom tidy
#' @import officer
#' @import flextable
#' @aliases table2office table2doc table2ppt
#' @param x given R stats object to export; if set to \code{NULL} the output of the
#' previous R command will be exported.
#' @param file name of output file. The .pptx or .docx extension is added automatically.
#' @param type desired output type - \code{"PPT"} for PowerPoint and \code{"DOC"} for Word.
#' @param append logical value - if \code{TRUE} and \code{type="PPT"} or \code{"DOC"} it will
#' append the table to the given file, where file can also be a given corporate. If
#' \code{append=FALSE} any existing file will be overwritten.
#' @param digits number of digits after the comma (for all numeric columns
#' except p-values or degrees of freedom)
#' @param digitspvals number of digits after the comma (for p-values only). The
#' default is equal to \code{digits}.
#' @param trim.pval a threshold below which the p-values are trimmed as
#' "< \code{trim.pval}".
#' @param width desired width of table in inches. If the given width exceeds the page or slide
#' width, the table width becomes the page/slide width.
#' @param height desired height of table in inches. If the given height exceeds the page or slide
#' height, the table height becomes the page/slide height.
#' @param offx x offset in inches to specify horizontal location of table (only for \code{type=="PPT"}).
#' @param offy y offset in inches to specify vertical location of table (only for \code{type=="PPT"}).
#' @param font desired font to use for output table; defaults to \code{"Arial"} on Windows
#' systems and to \code{"Helvetica"} on other systems.
#' @param pointsize desired font point size.
#' @param add.rownames logical specifying whether or not to add row names.
#' @param \dots Further arguments to be passed to \code{table2office}.
#' @return \code{\link[flextable]{flextable}} object
#' @details Columns corresponding to degrees of freedom (with header "Df" or "df")
#' are always given as integers. Objects that can be exported with \code{\link{table2office}} are
#' all those supported by \code{\link[xtable]{xtable}} and \code{\link[broom]{tidy}}. The function will
#' first use \code{\link[xtable]{xtable}} to format the data. If the data class is not supported by
#' \code{\link[xtable]{xtable}} the function will then use \code{\link[broom]{tidy}}.
#' The data classes suported by \code{\link[xtable]{xtable}} are:
#' \itemize{
#' \item \code{anova}
#' \item \code{aov}
#' \item \code{aovlist}
#' \item \code{data.frame}
#' \item \code{glm}
#' \item \code{gmsar}
#' \item \code{lagImpact}
#' \item \code{lm}
#' \item \code{matrix}
#' \item \code{prcomp}
#' \item \code{sarlm}
#' \item \code{sarlm.pred}
#' \item \code{spautolm}
#' \item \code{sphet}
#' \item \code{splm}
#' \item \code{stsls}
#' \item \code{summary.aov}
#' \item \code{summary.aovlist}
#' \item \code{summary.glm}
#' \item \code{summary.gmsar}
#' \item \code{summary.lm}
#' \item \code{summary.prcomp}
#' \item \code{summary.sarlm}
#' \item \code{summary.spautolm}
#' \item \code{summary.sphet}
#' \item \code{summary.splm}
#' \item \code{summary.stsls}
#' \item \code{table}
#' \item \code{ts}
#' \item \code{zoo}
#' }
#' The data classes suported by \code{\link[broom]{tidy}} are:
#' \itemize{
#' \item \code{aareg}
#' \item \code{acf}
#' \item \code{Arima}
#' \item \code{betareg}
#' \item \code{biglm}
#' \item \code{binDesign}
#' \item \code{binWidth}
#' \item \code{brmsfit}
#' \item \code{btergm}
#' \item \code{cch}
#' \item \code{character}
#' \item \code{cld}
#' \item \code{coeftest}
#' \item \code{confint.glht}
#' \item \code{cv.glmnet}
#' \item \code{default}
#' \item \code{density}
#' \item \code{dgCMatrix}
#' \item \code{dgTMatrix}
#' \item \code{dist}
#' \item \code{emmGrid}
#' \item \code{ergm}
#' \item \code{felm}
#' \item \code{fitdistr}
#' \item \code{ftable}
#' \item \code{gam}
#' \item \code{Gam}
#' \item \code{gamlss}
#' \item \code{geeglm}
#' \item \code{glht}
#' \item \code{glmnet}
#' \item \code{glmRob}
#' \item \code{gmm}
#' \item \code{htest}
#' \item \code{ivreg}
#' \item \code{kappa}
#' \item \code{kde}
#' \item \code{kmeans}
#' \item \code{Line}
#' \item \code{Lines}
#' \item \code{list}
#' \item \code{lme}
#' \item \code{lmodel2}
#' \item \code{lmRob}
#' \item \code{logical}
#' \item \code{lsmobj}
#' \item \code{manova}
#' \item \code{map}
#' \item \code{Mclust}
#' \item \code{merMod}
#' \item \code{mle2}
#' \item \code{muhaz}
#' \item \code{multinom}
#' \item \code{nlrq}
#' \item \code{nls}
#' \item \code{NULL}
#' \item \code{numeric}
#' \item \code{orcutt}
#' \item \code{pairwise.htest}
#' \item \code{plm}
#' \item \code{poLCA}
#' \item \code{Polygon}
#' \item \code{Polygons}
#' \item \code{power.htest}
#' \item \code{pyears}
#' \item \code{rcorr}
#' \item \code{ref.grid}
#' \item \code{ridgelm}
#' \item \code{rjags}
#' \item \code{roc}
#' \item \code{rowwise_df}
#' \item \code{rq}
#' \item \code{rqs}
#' \item \code{sparseMatrix}
#' \item \code{SpatialLinesDataFrame}
#' \item \code{SpatialPolygons}
#' \item \code{SpatialPolygonsDataFrame}
#' \item \code{spec}
#' \item \code{speedlm}
#' \item \code{stanfit}
#' \item \code{stanreg}
#' \item \code{summary.glht}
#' \item \code{summaryDefault}
#' \item \code{survdiff}
#' \item \code{survexp}
#' \item \code{survfit}
#' \item \code{survreg}
#' \item \code{tbl_df}
#' \item \code{TukeyHSD}
#' }
#' @author Tom Wenseleers, Christophe Vanderaa
#' @example examples/table2office.R
#' @seealso \code{\link{table2tex}}, \code{\link{table2html}}, \code{\link{table2spreadsheet}}
#' @export
#'
table2office = function(x = NULL, file = "Rtable", type = c("PPT","DOC"), append = FALSE, digits = 2,
digitspvals = NULL, trim.pval = 1E-16, width = NULL, height = NULL, offx = 1, offy = 1,
font = ifelse(Sys.info()["sysname"]=="Windows","Arial","Helvetica")[[1]], pointsize = 12,
add.rownames = FALSE) {
if(is.null(digitspvals)) digitspvals <- digits
obj=x
if (is.null(obj)) {
outp = .Last.value # capture previously shown output or use passed object
} else {
outp = obj
}
if (is.null(outp)) stop("no R stats object available to export")
supobjects = unique(c(as.character(gsub("xtable.", "", methods(xtable))),
as.character(gsub("tidy.", "", methods(tidy))),
"xtabs"))
if (length(intersect(class(outp), supobjects)) == 0) stop(paste0(class(outp), " is currently not supported by table2office"))
type=toupper(type)
type=match.arg(type,c("PPT","DOC"))
ext <- if(type=="PPT"){
".pptx"
} else if(type == "DOC"){
".docx"
}
file = sub("^(.*)[.].*", "\\1", file) # remove extension if given
file = paste0(file, ext) # add extension
if(type == "PPT"){
if (append & file.exists(file)) {
doc = read_pptx(path = file)
} else {
doc = read_pptx()
}
doc <- add_slide(doc, layout = "Blank", master = "Office Theme")
pagesize <- get.slide.size(doc)
} else if(type == "DOC"){
if (append & file.exists(file)) {
doc = read_docx(path = file)
doc = body_add_break(doc, pos = "after")
} else {
doc = read_docx()
}
pagesize <- (doc$sect_dim$page - doc$sect_dim$margins[c(3,2)])/1440 # 1440 is a factor to convert to inches
}
# deal with specific classes of objects
if (inherits(outp, "summary.merMod")) {
outp <- data.frame(coef(summary(outp)), check.names = F)
} else if(inherits(outp, "Matrix")) {
outp <- as.data.frame(as.matrix(x))
} else if (inherits(outp, c("xtabs", "ftable"))) {
outp <- ftable(outp)
}
# Depending on the data class, call xtable or tidy
if (length(intersect(class(outp), as.character(gsub("xtable.", "", methods(xtable))))) >= 1) {
tab <- xtable2(x=outp, ndigits = digits, ndigitspvals = digitspvals, trim.pval = trim.pval)
} else if (length(intersect(class(outp), as.character(gsub("tidy.", "", methods(tidy))))) >= 1) {
tab <- tidy2(x=outp, ndigits = digits, ndigitspvals = digitspvals, trim.pval = trim.pval)
} else { # should not occur
tab <- data.frame2(x=outp, ndigits = digits, ndigitspvals = digitspvals, trim.pval = trim.pval)
}
nc <- ncol(tab)
nr <- nrow(tab)
tblaspectr = nc / nr * 2 # guess table aspect ratio
pageaspectr = pagesize["width"]/pagesize["height"]
if (pageaspectr > tblaspectr) {
xf = tblaspectr/pageaspectr
yf = 1
} else {
xf = 1
yf = pageaspectr/tblaspectr
}
w = pagesize["width"] * xf
h = pagesize["height"] * yf
# if width and height is given override other scaling params
if (!is.null(width)) w = width
if (!is.null(height)) h = height
# Avoid bug in flextable: when one of the colnames = x, flextable returns an empty table
x.col <- which(colnames(tab) == "x")
if(length(x.col)>0) colnames(tab)[x.col]<- "x "
# Issues
# - Deal with ftable
# - use margins ?
cell.height <- min(h, pagesize["height"] - offy)/(nr+1)
cell.width <- min(w, pagesize["width"] - offx)/(nc+1)
if(inherits(tab,"xtable")){
tab <- xtable_to_flextable(tab, include.rownames = add.rownames, rowname_col = ".")
tab <- width(tab, width=cell.width)
tab <- height(tab, height=cell.height)
} else {
if(add.rownames) x <- cbind(" " = rownames(x), x)
tab <- flextable(tab, cheight = cell.height, cwidth = cell.width)
}
# Format the digits
col.pval <- grep("\\QPr(\\E|\\Qp-value\\E|\\Qp value\\E|\\Qpadj\\E|^p$|^padj$|p[.]value", tab$col_keys, value = TRUE)
col.df <- grep("^df$", tab$col_keys, value = TRUE, ignore.case = TRUE)
col.other <- tab$col_keys[! tab$col_keys %in% c(col.pval, col.df)]
tab <- colformat_num(x = tab, col_keys = col.other, digits = digits)
tab <- colformat_int(x = tab, col_keys = col.df)
tab <- colformat_num(x = tab, col_keys = col.pval, digits = )
tab <- bold(tab, part = "header") # bold header
tab <- fontsize(tab, part = "all", size = pointsize)
tab <- font(tab, part = "all", fontname = font)
if(type=="PPT"){
doc <- ph_with(doc, value = tab , location = ph_location(left = offx, top = offy))
} else if(type == "DOC"){
doc <- body_add_flextable(doc, value = tab)
}
print(doc, target = file)
message(paste0("Exported table as ",file))
return(tab)
}
#' @describeIn table2office
#' Export statistical output to a table in a Microsoft Office PowerPoint/ LibreOffice Impress presentation
#' @export
table2ppt = function(...) table2office(type = "PPT", ...)
#' @describeIn table2office
#' Export statistical output to a table in a Microsoft Office Word/ LibreOffice Writer document
#' @export
table2doc = function(...) table2office(type = "DOC", ...)