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

522

CHAPTER 22 Creating dynamic reports

22.3 Creating dynamic reports with R and LaTeX

LaTeX is a document-preparation system for high-quality typesetting that’s freely available for Windows, Mac, and Linux platforms. LaTeX allows you to create beautiful, complex, multipart documents, and it can convert from one type of document (such as an article) to another type of document (such as a report) by changing just a few lines of code. It’s extraordinarily powerful software and, as such, has a significant learning curve.

If you’re unfamiliar with LaTeX, you may want to read “The Not So Short Introduction to LaTeX 2e” by Tobias Oetiker et al. (http://mng.bz/45vP) or LaTeX Tutorials: A Primer by the Indian TEX Users Group (http://mng.bz/2c0O) before continuing. The language is definitely worth learning, but it will take some time and patience to master. Once you’re familiar with LaTeX, creating a dynamic report is a straightforward process.

The knitr package allows you to embed R code within the LaTeX document using techniques that are analogous to the ones used previously for creating web pages. If you installed rmarkdown or are using RStudio, you already have knitr. If not, install it now (install.packages("knitr")). Additionally, you’ll need a LaTeX compiler; see section 22.2 for details.

In this section, you’ll create a report describing patients’ reactions to various drugs, using data from the multcomp package. If you didn’t install it in chapter 9, be sure to run install.packages("multcomp") before continuing.

To generate a report using R and LaTeX, you first create a text file (typically with the filename extension .Rnw) containing the report text, LaTeX markup code, and R code chunks. An example is given in listing 22.2. Each R code chunk starts with the delimiter <<options>>= and ends with the delimiter @. The code chunk options are listed in table 22.3. Inline R code is included using the \Sexpr{R code} syntax. When the R code is evaluated, the number or string is inserted at that point in the text.

The file is then processed by the knit() function:

library(knitr)

knit("drugs.Rnw")

During this step, the R code chunks are processed and, depending on the options, replaced with LaTeX-formatted R code and output. By default, knit("drugs.Rnw") inputs the file drugs.Rnw and outputs the file drugs.tex. The .tex file is then run through a LaTeX compiler, creating a PDF, PostScript, or DVI file.

As a simpler alternative, you can use the knit2pdf() helper function in the knitr package:

library(knitr)

knit2pdf("drugs.Rnw")

The function generates the .tex file and converts it to a finished PDF document named drugs.pdf. The resulting PDF document is displayed in figure 22.3.

Creating dynamic reports with R and LaTeX

523

Listing 22.2 drugs.Rnw: a sample LaTeX template with embedded R code

\documentclass[11pt]{article} \title{Sample Report} \author{Robert I. Kabacoff, Ph.D.} \usepackage{float}

\usepackage[top=.5in, bottom=.5in, left=1in, right=1in]{geometry} \begin{document}

\maketitle

<<echo=FALSE, results='hide', message=FALSE>>= library(multcomp)

library(xtable) df <- cholesterol

@

\section{Results}

Cholesterol reduction was assessed in a study that randomized \Sexpr{nrow(df)} patients

to one of \Sexpr{length(unique(df$trt))} treatments. Summary statistics are provided in

Table \ref{table:descriptives}.

<<echo=FALSE, results='asis'>>=

descTable

<- data.frame("Treatment" = sort(unique(df$trt)),

"N"

= as.vector(table(df$trt)),

"Mean"

=

tapply(df$response,

list(df$trt), mean, na.rm=TRUE),

"SD"

=

tapply(df$response,

list(df$trt), sd, na.rm=TRUE)

)

print(xtable(descTable, caption = "Descriptive statistics for each treatment group", label = "table:descriptives"), caption.placement = "top", include.rownames = FALSE)

@

The analysis of variance is provided in Table \ref{table:anova}.

<<echo=FALSE, results='asis'>>=

fit <- aov(response ~ trt, data=df) print(xtable(fit, caption = "Analysis of variance",

label = "table:anova"), caption.placement = "top")

@

\noindent and group differences are plotted in Figure \ref{figure:tukey}.

\begin{figure}[H]\label{figure:tukey}

\begin{center}

<<echo=FALSE, fig.width=4, fig.height=3>>= par(mar=c(3,3,1,3))

boxplot(response ~ trt, data=df, col="lightgrey", xlab="Treatment", ylab="Response")

@

\caption{Distribution of response times by treatment.} \end{center}

\end{figure}

\end{document}

524

CHAPTER 22 Creating dynamic reports

Sample Report

Robert I. Kabacoff, Ph.D.

March 23, 2015

1 Results

Cholesterol reduction was assessed in a study that randomized 50 patients to one of 5 treatments. Summary statistics are provided in Table 1.

Table 1: Descriptive statistics for each treatment group

Treatment

N

Mean

SD

1time

10

5.78

2.88

2times

10

9.22

3.48

4times

10

12.37

2.92

drugD

10

15.36

3.45

drugE

10

20.95

3.35

The analysis of variance is provided in Table 2.

Table 2: Analysis of variance

 

Df

Sum Sq

Mean Sq

F value

Pr( > F)

trt

4

1351.37

337.84

32.43

0.0000

Residuals

45

468.75

10.42

 

 

and group differences are plotted in Figure 1.

5 10 15 20 25

1time

4times

drugE

Figure 1: Distribution of response times by treatment.

1

Figure 22.3 The text file drugs.Rnw is processed through the knit2pdf() function, resulting in a typeset PDF document (drugs.pdf).

Creating dynamic reports with R and Open Document

525

The knitr package is documented at http://yihui.name/knitr and in Yihui Xie’s book Dynamic Documents with R and knitr (Chapman and Hall/CRC, 2013). To learn more about LaTeX, check out the tutorials mentioned earlier and visit www.latex-project.org.

22.4 Creating dynamic reports with R and Open Document

Although LaTeX is powerful, it requires significant study to use effectively and creates documents in formats (PDF, DVI, PS) that can’t be edited. You can also output R results to a word-processing document. Two of the most popular formats are Microsoft Word (.docx) and Open Document (.odf).

Open Document Format for Office Applications (ODF) is an open source, XML- based file format that’s compatible with many software suites. Two popular and freely available office suites are OpenOffice (www.openoffice.org) and LibreOffice (www.libreoffice.org). Both are available for Windows, Mac OS X, and Linux environments, and either can be used in this section.

The odfWeave package provides a mechanism for embedding R code and output in an Open Document file. In this section, you’ll create a report exploring salary differences between male and female professors.

After installing OpenOffice (or LibreOffice) and the odfWeave package, create a document named salaryTemplate.odt (see figure 22.4). The document contains formatted text and R code chunks. The text is formatted using the OpenOffice (or LibreOffice) GUI. The code chunks are delimited as follows:

<<options>>= R statements

@

Code chunk options are given in table 22.3. Inline R code results (numbers or strings) are included using \Sexpr{R code}.

Table 22.3 R code chunk options in odfWeave

Option

Action

 

 

echo

Includes R code in the output file (TRUE/FALSE)

results

Outputs results as is (verbatim), as XML code (xml), or suppress output (hide)

fig

Code chunk produces graphical output (TRUE/FALSE)

 

 

Once the document is saved, process it using odfWeave() in the odfWeave package:

library(odfWeave)

infile <- "salaryTemplate.odt" outfile <- "salaryReport.odf" odfWeave(infile, outfile)

This takes the salaryTemplate.odt file displayed in figure 22.4 and produces the salaryReport.odf file displayed in figure 22.5.

526

CHAPTER 22 Creating dynamic reports

Figure 22.4 OpenOffice Writer file

(salaryTemplate.odt) with embedded R code chunks. After the file is processed by the odfWeave() function, the report in figure 22.7 (salaryReport.odf) is produced.

Figure 22.5 Final report in ODF format (salaryReport.odf)

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