-
Notifications
You must be signed in to change notification settings - Fork 0
/
R2DOCX.R
122 lines (101 loc) · 4.06 KB
/
R2DOCX.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
# TODO: Documentation de R2DOCX.R
#
# Author: David GOHEL <[email protected]>
# Date: 07 june 2013
# Version: 0.1
###############################################################################
install.packages("devtools")
devtools::install_github('R2DOC', 'davidgohel')
devtools::install_github('R2DOCX', 'davidgohel')
Sys.setenv(NOAWT=1) #prevents usage of awt - required on Mac
require( R2DOCX )
require(ggplot2)
# The 2 datasets that will be used
data( weights.summary )
data( measured.weights )
# Word document to write
docx.file = "document.docx"
# Word document to use as base document or a template
template.file = file.path( find.package("R2DOCX"), "templates/TEMPLATE_01.docx", fsep = "/" )
# Create a new object
doc = new("Docx"
, title = "My example"
, basefile = template.file
)
# in the base doc, 2 keyword are existing - let's replace them by some values
doc = replaceText( doc, pattern = "AUTHOR", replacement = "John Doe" )
doc = replaceText( doc, pattern = "DATE", replacement = date() )
# in the base doc, style 'TitleDoc' is existing
# we use it to generate a title ("My example") for the doc
doc = addParagraph( doc, value = "My example", stylename = "TitleDoc" )
# add a page break
doc = addPageBreak( doc )
# add a page for TOC (user will be asked to update this TOC
# when document will be opened the first time)
doc = addHeader(doc, "Table of content", 1);
doc = addTOC(doc)
doc = addPageBreak( doc )
# add a texts (normal and bullets)
doc = addHeader(doc, "Dataset presentation", 1);
doc = addParagraph( doc, value = "Dataset 'measured.weights' is set of weights measurements collected on few people before savate training.", stylename = "Normal" )
texts = c( "Column 'gender' is the subject gender."
, "Column 'id' is the unique subject identifier."
, "Column 'cat' is the subject weight category."
, "Column 'competitor' tells wether or not subject practice competition."
, "Column 'day' is the day of measurement."
, "Column 'weight' is the measured weight."
)
doc = addParagraph( doc, value = texts, stylename = "BulletList" )
# simple addTable
doc = addHeader(doc, "Dataset", 1);
doc = addHeader(doc, "Dataset first lines", 2);
doc = addTable( doc
, data = head( measured.weights, n = 10 )
, formats = get.light.formats()
)
doc = addLineBreak( doc )
# simple addTable with row merging on 'day' column
doc = addHeader(doc, "Dataset last lines", 2);
sampledata = tail( measured.weights[ order(measured.weights$day), ], n = 20 )
doc = addTable( doc
, data = sampledata
, span.columns = c("day")
, col.colors = list( "gender" = ifelse( sampledata$gender == "male" , "#00c2ff", "#ffcdd1") )
)
doc = addPageBreak( doc )
# customized table
doc = addHeader(doc, "Dataset summary", 1);
doc = addTable( doc
, data = weights.summary
, header.labels = list("id" = "Subject Identifier", "avg.weight" = "Average Weight"
, "regularity" = "Regularity", "visit.n" = "Number of visits", "last.day" = "Last visit"
) # columns labels to display
, grouped.cols=list( "id" = "id"
, "Summary" = c( "avg.weight", "regularity", "visit.n", "last.day" )
) # grouped headers line to add before headers line
, col.types = list( "id" = "character", "avg.weight" = "double"
, "regularity" = "percent", "visit.n" = "integer"
, "last.day" = "date" ) # reporting types of each columns
, col.fontcolors = list( "regularity" = ifelse( weights.summary$regularity < 0.5 , "gray", "blue") ) # customized font colors for column "regularity"
)
doc = addPageBreak( doc )
doc = addHeader(doc, "Graphics", 1);
doc = addPlot( doc
, fun = boxplot
, formula = measured.weights$weight ~ measured.weights$id
, xlab = "Subjects", ylab = "Weights" , col = "#0088cc"
, legend = "My boxplot"
)
require( ggplot2 )
my.ggplot = qplot(day, weight, data = measured.weights, geom = "line", color = id )
doc = addPlot( doc
, fun = print
, x = my.ggplot
, legend = "ggplot example"
, width = 9, height = 7
)
writeDoc( doc, docx.file )
if( interactive() ) {
out = readline( "Open the docx file (y/n)? " )
if( out == "y" ) browseURL( file.path(getwd(), docx.file ) )
}