Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
R in Action, Second Edition.pdf
Скачиваний:
540
Добавлен:
26.03.2016
Размер:
20.33 Mб
Скачать

Creating dynamic reports with R and Microsoft Word

527

By default, odfWeave renders data frames, matrices, and vectors in an attractive table format. The odfTable() function can be used to format tables with a higher degree of precision and control. The function produces XML code, so be sure to specify result=xml in code chunks employing this function. Unfortunately, the xtable() function doesn’t work with odfWeave.

Once you have your report in ODF format, you can continue to edit it, tighten up the formatting, and save the results to the ODT, HTML, DOC, or DOCX file format. To learn more, read the odfWeave manual and vignette. Additional information, including a tutorial on document formatting with odfWeave, can be found in the Examples folder installed in the odfWeave directory in your R library. (The function

.libPaths() will display your library location.)

22.5 Creating dynamic reports with R and Microsoft Word

For good or ill, Microsoft Word is the standard for report writing in corporate settings. You’ve already seen how to create a Word document from a Markdown file using rmarkdown (section 22.2). In this section, we’ll look at a method for creating dynamic reports by embedding R code directly into Word documents using the R2wd package. The methods in the section will only work on Windows platforms (sorry, Mac and Linux users).

To follow the examples in this section, you’ll need to install the R2wd package (install.packages("R2wd")). R2wd also requires the RDCOMClient package, available from the Omega Project for Statistical Computing.

At the time of this writing, RDCOMClient must be installed from source. First be sure Rtools is installed (http://cran.r-project.org/bin/windows/Rtools). Next, download the source file (RDCOMClient_0.93-0.tar.gz) from www.omegahat.org/RDCOMClient. Note that the version numbers are likely to change over time. Finally, install the package using

install.packages(RDCOMClient_0.93-0.tar.gz, repos = NULL, type = "source")

The R2wd package provides functions that allow you to create a blank Word document; insert sections and titles; insert text, tables, and graphs; add formatting; and save the results. Although the package is versatile, building and formatting Word documents programmatically can be time-consuming.

The easiest way to create a dynamic report in Word using the R2wd package is a two-step process:

1Create a Word document that contains bookmarks indicating where you want your R results to be placed.

2Create an R script that inserts the results in the Word document at the bookmarked locations and saves the finished document.

Let’s try this approach.

Open a new Word document, and call it salaryTemplate2.docx. Add the text and bookmarks displayed in figure 22.6. (In reality, the bookmarks in figure 22.6 aren’t

528

CHAPTER 22 Creating dynamic reports

Sample Report

Introduction

A two-way analysis of variance was employed to investigate the relationship between gender, academic rank, and annual salary in dollars. Data were collected from n professors in 2008. The ANOVA table is given in Table 1.

aovTable

The interaction between gender and rank is plotted in Figure 1.

effectsPlot

Figure 22.6 A Microsoft Word document named salaryTemplate2.docx containing text and bookmarks. The file is processed by the script salary.R (listing 22.3), results are inserted at the bookmark locations, and the document is saved as salaryReport2.docx (figure 22.7). Note that the bookmarks (in bold shaded text) aren’t actually visible on the page; the image has been annotated so that you can see where to place them.

visible. I’ve annotated the image, adding the bookmark names with a bold colored background, so that you can see where each should go.)

To insert a bookmark, place the cursor where you want the bookmark, choose Insert > Bookmark, give the bookmark a name, and click Add. The bookmarks in this example are named n, aovTable, and effectsPlot.

TIP Selecting Options > Advanced > Show Bookmarks in Microsoft Word will help you see where bookmarks are placed.

Next, create the R script given in listing 22.3. When the script is executed, it produces the necessary analyses, inserts them into the Word document, and saves the final document to disk. The script uses the functions listed in table 22.4.

Table 22.4 R2wd functions

Function

Use

 

 

wdGet()

Returns a handle to a Word document. If Word isn’t running, it’s started, a

 

blank document is opened, and a handle is returned.

wdGoToBookmark()

Places the cursor at a bookmark.

wdWrite()

Writes text at the cursor.

wdTable()

Writes a data frame or an array as a Word table at the current cursor location.

wdPlot()

Creates an R plot, and pastes it into Word at the current cursor location.

wdSave()

Saves the Word document. If no file name is given, Word prompts the user for

 

one.

wdQuit()

Closes Word, and removes the handle.

 

 

Creating dynamic reports with R and Microsoft Word

529

Listing 22.3 salary.R: 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)

b Opens the document

c Inserts text

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") f Saves and quits wdQuit()

d Inserts a table

e Inserts a plot

First, the salary2Template.docx file is opened. If Word isn’t running, it’s automatically launched b. Next, the data analyses are performed. The cursor then is moved to the bookmark named n, and the sample size is inserted c.

Next, the cursor is moved to the bookmark named aovTable and the ANOVA results are inserted as a Word table d. Because R2wd doesn’t support the xtable() function, it’s important that the table be an R data frame, a matrix, or an array. Options can control the text and location of the table caption, font size, and table formatting. Try autoformat = 1, 2, 3, and so on, to see the various formats available. Currently there is no way to suppress the caption.

Two of the statements in the ANOVA code require additional explanation. The aovTable object is a data frame containing the two-way ANOVA results. The round() function is used to limit the number of decimal places printed in the table. The statement

aovTable[is.na(aovTable)] <- ""

is a trick that replaces NAs with blanks. This is necessary because there are no values in the F and Pr(>F) columns for the Residuals row, and you don’t want NA to print in these cells of the table.

530

CHAPTER 22 Creating dynamic reports

The cursor next moves to the bookmark named effectsPlot. The wdPlot() function requires that the user specify a plotting function. Here, the myplot() function produces an effects plot via the allEffects() function in the effects package e.

The wdPlot() function supports method="bitmap" and method="metafile". Use metafile whenever possible—it looks better in a Word document. Unfortunately, the metafile option doesn’t support transparency, so you have to use the bitmap option when transparency is present. You’re most likely to encounter transparency when using the ggplot2 package to produce graphs.

When the code in salary.R is executed, it runs the R code, inserts the results into salaryTemplate2.docx, and saves the finished Word document as salaryReport2.docx f. Then the Microsoft Word application quits. The resulting document is displayed in figure 22.7.

Sample Report

Introduction

A two-way analysis of variance was employed to investigate the relationship between gender, academic rank, and annual salary in dollars. Data were collected from 397 professors in 2008. The ANOVA table is given in Table 1.

Table 1 Two-way Analysis of Variance

 

Sum Sq

Df

F value

Pr(>F)

(Intercept)

67009671400

1

119.538

0

rank

15266607695

2

13.617

0

sex

97803720

1

0.174

0.676

rank:sex

43603063

2

0.039

0.962

Residuals

219184457146

391

 

 

 

 

 

 

 

The interaction between gender and rank is plotted in Figure 1.

 

 

 

AsstProf

AssocProf

Prof

 

 

sex : Female

 

sex : Male

 

 

130000

 

 

 

 

 

120000

 

 

 

 

 

110000

 

 

 

 

salary

100000

 

 

 

 

90000

 

 

 

 

 

 

 

 

 

 

80000

 

 

 

 

 

70000

 

 

 

 

 

AsstProf

AssocProf

Prof

 

 

 

 

 

rank

 

 

Figure 1 Mean Effects Plot

Figure 22.7 Final report in DOCX format (salaryReport2.docx)

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]