forked from kabacoff/RiA2
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Ch22 Creating dynamic Reports.R
66 lines (53 loc) · 2.44 KB
/
Ch22 Creating dynamic Reports.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
#--------------------------------------------------------------------------#
# R in Action (2nd ed): Chapter 22 #
# Creating dynamic reports #
# requires packages knitr, odfWeave, r2wd, car (for the dataset) #
# install.packages(c("knitr", "odfWeave", "r2wd", "car", "rmarkdown"), #
# depend=TRUE) #
# Windows users will need #
# MikTeX (http://miktex.org/) #
# Mac users will need #
# MacTeX (http://www.tug.org/mactex/) #
# Linux users should just need the knitr package #
#--------------------------------------------------------------------------#
# download http://www.statmethods.net/RiA/DynamicReports.zip
# and unzip the contents in the R current working directory
# Creating web page with rmarkdown
library(rmarkdown)
render("women.Rmd", "html_document")
## Creating publication ready documents with R and LaTeX
library(knitr)
knit2pdf("drugs.Rnw")
## Creating dynamic reports with R and OpenOffice
# you will need to have OpenOffice (http://www.openoffice.org) installed
library(odfWeave)
infile <- "salaryTemplate.odt"
outfile <- "salaryReport.odt"
odfWeave(infile, outfile)
## Creating dynamic reports with R and Microsoft Word
# you will need to have Microsoft Word installed
# Listing 22.3 - R script for inserting results in salary.docx
require(R2wd)
require(car)
df <- Salaries
n <- nrow(df)
fit <- lm(salary ~ rank*sex, data=df)
aovTable <- Anova(fit, type=3)
aovTable <- round(as.data.frame(aovTable), 3)
aovTable[is.na(aovTable)] <- ""
wdGet("salaryTemplate2.docx", method="RDCOMClient")
wdGoToBookmark("n")
wdWrite(n)
wdGoToBookmark("aovTable")
wdTable(aovTable, caption="Two-way Analysis of Variance",
caption.pos="above", pointsize=12, autoformat=4)
wdGoToBookmark("effectsPlot")
myplot <- function(){
require(effects)
par(mar=c(2,2,2,2))
plot(allEffects(fit), main="")
}
wdPlot(plotfun=myplot, caption="Mean Effects Plot",
height=4, width=5, method="metafile")
wdSave("SalaryReport2.docx")
wdQuit()