Function addPlot
will capture plots into png files and insert them into the output document with eventually a legend text below graphics.
Its main argument is a function. The plotting is done by this function. If this function requires parameters, add them as parameters to the addPlot
function (the ...
argument).
doc = addPlot( doc , plot , x = rnorm( 10 ), y = rnorm( 10 ), main = "main title" , legend = "graph example", stylename="PlotReference" )
If manipulating lattice
or ggplot2
plot objects, use print
on the object in the function (otherwise lattice
or ggplot2
objects won't be written).
doc = addPlot( doc , print , x = qplot(Sepal.Length, Petal.Length, data = iris , color = Species, size = Petal.Width ) , legend = "graph example", stylename="PlotReference" )
To combine multiple plots, simply create a function to execute all plotting instructions:
doc = addPlot( doc , function( ) { print( qplot(Sepal.Length, Petal.Length , data = iris, color = Species, size = Petal.Width ) ) plot( x = rnorm( 10 ), y = rnorm( 10 ), main = "main title" ) } , legend = "graph example", stylename="PlotReference" )
Simple texts output can be done with function addParagraph
.
doc = addParagraph( doc, "Hello World!", stylename = "Normal" )
Argument stylename
is detailed in the chapter Styles.
The function can also be used to execute text replacement and conditional formatting. If replacements have to be done, keywords that identify text to be replaced must be contains into square brackets [].
Replacement texts has to be formated with textProperties
objects.
# template text x = "[animal] eats [food]." # define formatting properties for replacement text. See chapter "styles" textProp = textProperties( color = "blue" ) replacement.styles = list( animal= textProp, food= textProp ) # define replacement text replacements = list( animal = "donkey", food = "grass" ) doc = addParagraph( doc, value = x, stylename = "Normal" , replacements = replacements , replacement.styles = replacement.styles )
Produces: donkey eats grass.
The function is vectorized
x = c("text 1", "text 2") doc = addParagraph( doc, value = x, stylename = "Normal")
Insert a table of contents with function addTOC
.
doc = new("Docx", title = "My example" ) doc = addTOC(doc) doc = addHeader(doc, "Title 1", 1); doc = addHeader(doc, "Title 1.1", 2); doc = addPageBreak( doc ) doc = addHeader(doc, "Title 1.2", 2); doc = addPageBreak( doc ) doc = addHeader(doc, "Title 1.2.1", 3);
Insert a customized table of contents with argument stylename
. If used, a table of contents will be created containing entries formated with specified styles. An example of usage is a table of contents of the produced graphics:
doc = new("Docx", title = "My example" ) doc <- addTOC(doc, stylename = "PlotReference") doc = addHeader(doc, "Title 1", 1); doc = addHeader(doc, "Title 1.1", 2); doc = addPlot( doc , fun = plot, x = rnorm( 100 ), y = rnorm (100 ) , main = "base plot main title" , legend="graph example 1", stylename = "PlotReference" ) doc = addPageBreak( doc ) doc = addHeader(doc, "Title 1.2", 2); doc = addTable( doc, iris ) doc = addParagraph( doc, value="table 1", stylename = "PlotReference" ) doc = addPageBreak( doc ) doc = addHeader(doc, "Title 1.2.1", 3); doc = addPlot( doc , fun = plot, x = rnorm( 100 ), y = rnorm (100 ) , main = "base plot main title" , legend="graph example 2", stylename = "PlotReference" )
Insert a page break with addPageBreak
.
doc = addPageBreak( doc)
Insert an external image with addImage
.
doc = addImage( doc, "D:/img/image.png" )
The function is vectorized.