Skip to content
This repository was archived by the owner on Mar 23, 2020. It is now read-only.

Commit 2da5a3a

Browse files
committed
add examples
1 parent 40b7875 commit 2da5a3a

30 files changed

+388
-3
lines changed

examples/FlexTableExample.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#####################################################################
22

3+
# set default font size to 10
4+
options( "ReporteRs-fontsize" = 10 )
5+
36
# Create a FlexTable with data.frame mtcars, display rownames
47
# use different formatting properties for header and body cells
58
MyFTable = FlexTable( data = mtcars, add.rownames = TRUE

examples/addBasePlot_vg.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Add a base plot
2+
# set vector.graphic to FALSE if Word version
3+
# used to read the file is <= 2007
4+
doc = addPlot( doc, fun = plot
5+
, x = rnorm( 100 ), y = rnorm (100 )
6+
, main = "base plot main title"
7+
, vector.graphic = TRUE
8+
, width = 5, height = 7
9+
, par.properties = parProperties(text.align = "left")
10+
)

examples/addFlexTable.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

2+
# add MyFTable into document
23
doc = addFlexTable( doc, MyFTable )

examples/addLMPlot.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
###########################################################
2+
# Create lm.D9, a lm object
3+
ctl = c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
4+
trt = c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
5+
group = gl(2, 10, 20, labels = c("Ctl","Trt"))
6+
weight = c(ctl, trt)
7+
lm.D9 = lm(weight ~ group)
8+
9+
# add the 6 plots into the document
10+
doc = addPlot( doc, plot, x = lm.D9, width = 6, height = 7 )

examples/addPage.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

2+
# add a page with title "page example"
23
doc = addPage( doc, title = "page example" )

examples/addParagraph_bullets_docx.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
# define some texts
3+
texts = c( "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
4+
, "In sit amet ipsum tellus. Vivamus dignissim arcu sit amet faucibus auctor."
5+
, "Quisque dictum tristique ligula."
6+
)
7+
8+
# add texts with stylename BulletList
9+
doc = addParagraph( doc, value = texts, stylename="BulletList" )

examples/addParagraph_hello_docx.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
# Add "Hello World" into the document doc
3+
doc = addParagraph(doc, "Hello Word", stylename = "Normal" )
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
# Add "Hello World" into the document doc
3+
doc = addParagraph(doc, "Hello Word" )

examples/addParagraph_sop_docx.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
# Add my.pars into the document doc
3+
doc = addParagraph(doc, my.pars, stylename = "Normal" )
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
# Add my.pars into the document doc
3+
doc = addParagraph(doc, my.pars )

examples/addTitle1Level1.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
doc = addTitle( doc, "Title example", level = 1 )
2+
doc = addTitle( doc, "Title example 1", level = 1 )

examples/addTitle1NoLevel.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
doc = addTitle( doc, "Title example" )
2+
doc = addTitle( doc, "Title example 1" )

examples/addTitle2Level1.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
doc = addTitle( doc, "Title example 2", level = 1 )

examples/addTitle2NoLevel.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
doc = addTitle( doc, "Title example 2" )

examples/addTitle3Level1.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
doc = addTitle( doc, "Title example 3", level = 1 )

examples/addTitle3NoLevel.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
doc = addTitle( doc, "Title example 3" )

examples/addggplot.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# load ggplot2
2+
require( ggplot2 )
3+
4+
# create a ggplot2 plot
5+
myplot = qplot(Sepal.Length, Petal.Length, data = iris
6+
, color = Species, size = Petal.Width, alpha = I(0.7) )
7+
8+
# Add myplot into object doc
9+
# myplot is assigned to argument 'x' because function 'print' on ggplot
10+
# objects is expecting argument 'x'.
11+
doc = addPlot( doc = doc, fun = print, x = myplot )

examples/agg.mtcars.FlexTable.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#####################################################################
22

3+
# set default font size to 10
4+
options( "ReporteRs-fontsize" = 10 )
5+
36
# a summary of mtcars
47
dataset = aggregate( mtcars[, c("disp", "mpg", "wt")]
58
, by = mtcars[, c("cyl", "gear", "carb")]
69
, FUN = mean )
710
dataset = dataset[ order(dataset$cyl, dataset$gear, dataset$carb), ]
811

9-
options( "ReporteRs-fontsize" = 9 )
1012

1113
# set cell padding defaut to 2
1214
baseCellProp = cellProperties( padding = 2 )

examples/docx.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
2+
# set default font size to 10
3+
options( "ReporteRs-fontsize" = 10 )
4+
15
doc = docx( )

examples/docx_example.R

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
require( ggplot2 )
2+
3+
# Word document to write
4+
docx.file = "document_example.docx"
5+
6+
# set default font size to 10
7+
options( "ReporteRs-fontsize" = 10 )
8+
9+
# Create a new document
10+
doc = docx( title = "title" )
11+
12+
# display available styles
13+
styles( doc )
14+
15+
# add title
16+
doc = addParagraph( doc, "Document title", stylename = "TitleDoc" )
17+
18+
# add a paragraph
19+
doc = addParagraph( doc , "This document is generated with ReporteRs."
20+
, stylename="Citationintense")
21+
22+
# add page break
23+
doc = addPageBreak( doc )
24+
25+
# add a title
26+
doc = addTitle( doc, "Table of contents", level = 1 )
27+
28+
################ TOC DEMO ################
29+
# add a table of content
30+
doc = addTOC( doc )
31+
32+
# add page break and then tables of contents for produced plots and tables
33+
doc = addPageBreak( doc )
34+
doc = addTitle( doc, "List of graphics", level = 1 )
35+
doc = addTOC( doc, stylename = "rPlotLegend" )
36+
doc = addTitle( doc, "List of tables", level = 1 )
37+
doc = addTOC( doc, stylename = "rTableLegend" )
38+
39+
# add page break
40+
doc = addPageBreak( doc )
41+
42+
################ TEXT DEMO ################
43+
44+
# add a title
45+
doc = addTitle( doc, "Texts demo", level = 1 )
46+
texts = c( "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
47+
, "In sit amet ipsum tellus. Vivamus dignissim arcu sit amet faucibus auctor."
48+
, "Quisque dictum tristique ligula."
49+
)
50+
51+
# add simple text
52+
doc = addParagraph( doc, value = texts, stylename="BulletList" )
53+
54+
# Add "My tailor is rich" and "Cats and Dogs"
55+
# format some of the pieces of text
56+
pot1 = pot("My tailor"
57+
, textProperties(color="red", font.size = 12 ) ) + " is " + pot("rich"
58+
, textProperties(font.weight="bold") )
59+
pot2 = pot("Cats"
60+
, textProperties(color="red", font.size = 12)
61+
) + " and " + pot("Dogs"
62+
, textProperties(color="blue", font.size = 12) )
63+
doc = addParagraph(doc, set_of_paragraphs( pot1, pot2 ), stylename="Normal" )
64+
65+
################ PLOT DEMO ################
66+
67+
myplot = qplot(Sepal.Length, Petal.Length
68+
, data = iris, color = Species
69+
, size = Petal.Width, alpha = I(0.7)
70+
)
71+
# Add titles and then 'myplot'
72+
doc = addTitle( doc, "Plot examples", level = 1 )
73+
doc = addPlot( doc, function( ) print( myplot ) )
74+
# Add a legend below the plot
75+
doc = addParagraph( doc, value = "my first plot", stylename = "rPlotLegend")
76+
77+
################ TABLE DEMO ################
78+
79+
# Add a table
80+
doc = addTitle( doc, "Table example", level = 1 )
81+
# add iris sample
82+
doc = addTable( doc, data = iris[25:33, ] )
83+
# Add a legend below the table
84+
doc = addParagraph( doc, value = "my first table", stylename = "rTableLegend")
85+
86+
87+
################ FLEXTABLE DEMO ################
88+
89+
doc = addTitle( doc, "FlexTable example", level = 1 )
90+
91+
# Create a FlexTable with data.frame mtcars, display rownames
92+
# use different formatting properties for header and body cells
93+
MyFTable = FlexTable( data = mtcars, add.rownames = TRUE
94+
, body.cell.props = cellProperties( border.color = "#EDBD3E")
95+
, header.cell.props = cellProperties( background.color = "#5B7778" )
96+
)
97+
# zebra stripes - alternate colored backgrounds on table rows
98+
MyFTable = setZebraStyle( MyFTable, odd = "#D1E6E7", even = "#93A8A9" )
99+
100+
# applies a border grid on table
101+
MyFTable = setFlexTableBorders(MyFTable
102+
, inner.vertical = borderProperties( color="#EDBD3E", style="dotted" )
103+
, inner.horizontal = borderProperties( color = "#EDBD3E", style = "none" )
104+
, outer.vertical = borderProperties( color = "#EDBD3E", style = "solid" )
105+
, outer.horizontal = borderProperties( color = "#EDBD3E", style = "solid" )
106+
)
107+
108+
# add MyFTable into document
109+
doc = addFlexTable( doc, MyFTable )
110+
111+
# write the doc
112+
writeDoc( doc, file = docx.file)

examples/html.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11

2+
# set default font size to 10
3+
options( "ReporteRs-fontsize" = 10 )
4+
25
doc = html()

examples/html_example.R

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
require( ggplot2 )
2+
3+
# Word document to write
4+
html_directory = "html_example"
5+
6+
# set default font size to 10
7+
options( "ReporteRs-fontsize" = 10 )
8+
9+
# Create a new document
10+
doc = html( title = "document title" )
11+
12+
# add a page
13+
doc = addPage( doc, title = "HTML demo" )
14+
15+
16+
################ TEXT DEMO ################
17+
18+
# add a title
19+
doc = addTitle( doc, "Text demo", level = 1 )
20+
texts = c( "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
21+
, "In sit amet ipsum tellus. Vivamus dignissim arcu sit amet faucibus auctor."
22+
, "Quisque dictum tristique ligula."
23+
)
24+
25+
# add simple text
26+
doc = addParagraph( doc, value = texts )
27+
28+
# Add "My tailor is rich" and "Cats and Dogs"
29+
# format some of the pieces of text
30+
pot1 = pot("My tailor"
31+
, textProperties(color="red" ) ) + " is " + pot("rich"
32+
, textProperties(font.weight="bold") )
33+
pot2 = pot("Cats"
34+
, textProperties(color="red" )
35+
) + " and " + pot("Dogs"
36+
, textProperties(color="blue" ) )
37+
doc = addParagraph(doc, set_of_paragraphs( pot1, pot2 ) )
38+
39+
################ PLOT DEMO ################
40+
doc = addTitle( doc, "Plot demo", level = 1 )
41+
42+
myplot = qplot(Sepal.Length, Petal.Length
43+
, data = iris, color = Species
44+
, size = Petal.Width, alpha = I(0.7)
45+
)
46+
# Add titles and then 'myplot'
47+
doc = addPlot( doc, function( ) print( myplot ) )
48+
49+
################ TABLE DEMO ################
50+
doc = addTitle( doc, "Table example", level = 1 )
51+
52+
# add iris sample
53+
doc = addTable( doc, data = iris[25:33, ] )
54+
55+
################ FLEXTABLE DEMO ################
56+
doc = addTitle( doc, "FlexTable example", level = 1 )
57+
58+
59+
# Create a FlexTable with data.frame mtcars, display rownames
60+
# use different formatting properties for header and body cells
61+
MyFTable = FlexTable( data = mtcars, add.rownames = TRUE
62+
, body.cell.props = cellProperties( border.color = "#EDBD3E")
63+
, header.cell.props = cellProperties( background.color = "#5B7778" )
64+
)
65+
# zebra stripes - alternate colored backgrounds on table rows
66+
MyFTable = setZebraStyle( MyFTable, odd = "#D1E6E7", even = "#93A8A9" )
67+
MyFTable = setFlexTableWidths( MyFTable, widths = c(2,rep(.7,11)))
68+
69+
# applies a border grid on table
70+
MyFTable = setFlexTableBorders(MyFTable
71+
, inner.vertical = borderProperties( color="#EDBD3E", style="dotted" )
72+
, inner.horizontal = borderProperties( color = "#EDBD3E", style = "none" )
73+
, outer.vertical = borderProperties( color = "#EDBD3E", style = "solid" )
74+
, outer.horizontal = borderProperties( color = "#EDBD3E", style = "solid" )
75+
)
76+
77+
# add MyFTable into document
78+
doc = addFlexTable( doc, MyFTable )
79+
80+
# Write the object
81+
writeDoc( doc, directory = html_directory )
82+

examples/pot1_example.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# "My tailor is rich" with formatting on some words
2+
pot1 = pot("My tailor", textProperties(color = "red" )
3+
) + " is " + pot("rich", textProperties( font.weight = "bold" ) )

examples/pot2_example.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# "Cats and dogs" with formatting on some words
2+
pot2 = pot("Cats", textProperties(color = "red" )
3+
) + " and " + pot("dogs", textProperties( color = "blue" ) )

examples/pptx.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
2+
# set default font size to 24
3+
options( "ReporteRs-fontsize" = 24 )
4+
15
doc = pptx( title = "title" )

0 commit comments

Comments
 (0)