docx: Create Microsoft Word document object representation

Description Usage Arguments Details Value Note References See Also Examples

View source: R/docx.R

Description

Create a docx object

Usage

1
2
3
4
5
6
7
8
docx(title = "untitled", template, empty_template = FALSE,
  list.definition = getOption("ReporteRs-list-definition"))

## S3 method for class 'docx'
dim(x)

## S3 method for class 'docx'
print(x, ...)

Arguments

title

"character" value: title of the document (in the doc properties).

template

"character" value, it represents the filename of the docx file used as a template.

empty_template

wether the content of the template should be clear or not.

list.definition

a list definition to specify how ordered and unordered lists have to be formated. See list.settings. Default to getOption("ReporteRs-list-definition").

x

a docx objet

...

unused

Details

Several methods can used to send R output into an object of class docx.

R outputs (tables, plots, paragraphs and images) can be inserted (and not added at the end) in a document if a bookmark exists in the template file.

Once object has content, user can write the docx into a ".docx" file, see writeDoc.

dim returns page width and height and page margins of a docx object.

print print informations about an object of class docx.

Value

an object of class docx.

Note

Word 2007-2013 (*.docx) file formats are the only supported files.
Document are manipulated in-memory ; a docx's document is not written to the disk unless the writeDoc method has been called on the object.

References

Wikipedia: Office Open XML
http://en.wikipedia.org/wiki/Office_Open_XML

See Also

pptx

Examples

  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
if( check_valid_java_version() ){
# set default font size to 10
options( "ReporteRs-fontsize" = 10 )

# Create a new document
doc <- docx( title = "title" )

# display available styles
styles( doc )

# add title
doc <- addParagraph( doc, "Document title", stylename = "TitleDoc" )

# add a paragraph
doc <- addParagraph( doc , "This document is generated with ReporteRs."
  , stylename="Citationintense")

# add page break
doc <- addPageBreak( doc )

# add a title
doc <- addTitle( doc, "Table of contents", level =  1 )

################ TOC DEMO ################
# add a table of content
doc <- addTOC( doc )

# add page break and then tables of contents for produced plots and tables
doc <- addPageBreak( doc )
doc <- addTitle( doc, "List of graphics", level =  1 )
doc <- addTOC( doc, stylename = "rPlotLegend" )
doc <- addTitle( doc, "List of tables", level =  1 )
doc <- addTOC( doc, stylename = "rTableLegend" )

# add page break
doc <- addPageBreak( doc )

################ TEXT DEMO ################

# add a title
doc <- addTitle( doc, "Text demo", level =  1 )


sometext = c( "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
  , "In sit amet ipsum tellus. Vivamus dignissim arcu sit."
  , "Quisque dictum tristique ligula."
)
# add simple text with 'Normal' style
doc <- addParagraph( doc, value = sometext, stylename="Normal" )
# add simple text with 'BulletList' style
doc <- addParagraph( doc, value = sometext, stylename="BulletList" )

# Add "My tailor is rich" and "Cats and Dogs"
# format some of the pieces of text
pot1 = pot("My tailor", textProperties(color="red",
	shading.color = "#CCCCCC" ) ) + " is " + pot("rich",
		textProperties(font.weight="bold") )
pot2 = pot("Cats", textProperties(color="red" )) + " and " + pot("Dogs",
	textProperties(color="blue" ) )
doc <- addParagraph(doc, set_of_paragraphs( pot1, pot2 ),
	stylename = "Normal" )

doc <- addParagraph(doc, "Silentium tractibus per minimis ne excita
ut temptentur generalibus quam primordiis per clades post delictis
iuge exitium silentium per et.",
  par.properties = parProperties( padding.left = 25,
    padding.right = 25) )


doc <- addParagraph(doc,
  pot("Gallus necem refert singula modum quae est quae quorum leo.",
	format = textItalic( ) ),
  par.properties = parProperties(list.style = "blockquote") )


ordered.list.level1 = parProperties(list.style = "ordered", level = 1 )
ordered.list.level2 = parProperties(list.style = "ordered", level = 2 )

doc <- addParagraph( doc, value = sometext,
	par.properties = ordered.list.level1 )
doc <- addParagraph( doc, value = sometext,
	par.properties = ordered.list.level2 )

################ PLOT DEMO ################
# load ggplot2
if( requireNamespace("ggplot2", quietly = TRUE) ){
  doc <- addTitle( doc, "Plot example", level = 1 )

  # create a ggplot2 plot
  myplot = ggplot2::qplot(Sepal.Length, Petal.Length, data = iris
    , color = Species, size = Petal.Width, alpha = I(0.7) )

  # Add myplot into object doc
  doc <- addPlot( doc = doc, fun = print, x = myplot )

  # Add a legend below the plot
  doc <- addParagraph( doc, value = "my first plot",
	stylename = "rPlotLegend")
}

################ FLEXTABLE DEMO ################

doc <- addTitle( doc, "FlexTable example", level = 1 )

# Create a FlexTable with data.frame mtcars, display rownames
# use different formatting properties for header and body
MyFTable <- FlexTable( data = mtcars, add.rownames = TRUE,
	header.cell.props = cellProperties( background.color = "#00557F" ),
	header.text.props = textProperties( color = "white",
		font.size = 11, font.weight = "bold" ),
	body.text.props = textProperties( font.size = 10 )
)
# zebra stripes - alternate colored backgrounds on table rows
MyFTable <- setZebraStyle( MyFTable, odd = "#E1EEf4", even = "white" )

# applies a border grid on table
MyFTable <- setFlexTableBorders(MyFTable,
	inner.vertical = borderProperties( color="#0070A8", style="solid" ),
	inner.horizontal = borderNone(),
	outer.vertical = borderProperties( color = "#006699",
		style = "solid", width = 2 ),
	outer.horizontal = borderProperties( color = "#006699",
		style = "solid", width = 2 )
)


# add MyFTable into document
doc <- addFlexTable( doc, MyFTable )
doc <- addParagraph( doc, value = "my first table",
	stylename = "rTableLegend")

# write the doc
writeDoc( doc, file = "document_example.docx")
}


# get docx page dimensions ------
doc = docx( title = "title" )
dim( doc )

davidgohel/ReporteRs documentation built on March 23, 2020, 11 p.m.