Geoscience Reference
In-Depth Information
programming language (e.g. PASCAL or C++), the code could be scripts in a language designed
specifically for data processing and analysis, such as R (R Development Core Team, 2011) or SAS
software (SAS Institute Inc., 2011). Also, the WEAVE part of the process could incorporate output
from the data processing into the human-readable part of the document.
Examples of this approach are the NOWEB system (Ramsey, 1994) and the Sweave package
(Leisch, 2002). The former incorporates code into LATEX documents using two very simple exten-
sions to the markup language. The latter is an extended implementation of this system using R as the
language for the embedded code. Typically, Sweave formatted files have the suffix Rnw . An Rnw
file typically produces a pdf file of the final document via a two-stage process:
1. The Sweave program reads in the Rnw file and outputs a standard tex file, with the
evaluated embedded R code replaced by its output, formatted as LATEX.
2. The intermediate tex file is processed by the standard LATEX software to produce the
inal pdf file.
Provided a third party has access to the Sweave and LATEX software, the final pdf file can
always be reproduced from the Rnw file - but the Rnw file also contains information about how the
computational results provided in the final paper were obtained.
17.3.1 e xaMPle of the u Se of S weave
This document was created using Sweave - a snippet of the original is shown in Appendix 17A -
and the simple example of analysis of variance (ANOVA) in Table 17.1 was created with some
incorporated code.
In this case, the code is echoed in the document, and the result is incorporated in the final docu-
ment. A number of other options are possible - for example, instead of creating formatted output,
Sweave can take LATEX markup generated by the R code and include this directly in the docu-
ment. The xtable package is useful here as it allows R matrices, arrays and results of ANOVA and
regression analyses to be output in this format, where it can then be directly interpreted by LATEX
to create typeset and formatted tables in the final output. Also, the code itself need not be echoed
in the final document - a flag in the Sweave syntax allows this to be switched on or off. So long as
the Rnw document is retained, information about the data analysis is preserved. An example of this
kind was incorporated in this document, giving the output in Table 17.2.
TABLE 17.1
R ANOVA: Example of Incorporated Code
> ## From: Annette Dobson (1990)
> ## “An Introduction to Generalized Linear Models”.
> ## Page 9: Plant Weight Data.
> ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
> trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
> Group <- gl(2,10,20, labels = c(“Ctl”,”Trt”))
> Weight <- c(ctl, trt)
> lm.Dobson.p9 <- lm(Weight ~ Group)
> anova(lm.Dobson.p9)
Analysis of Variance Table
Response: Weight
Df Sum Sq Mean Sq F value Pr(>F)
Group 1 0.6882 0.68820 1.4191 0.249
Residuals 18 8.7292 0.48496
 
Search WWH ::




Custom Search