~ubuntu-branches/ubuntu/oneiric/ess/oneiric

« back to all changes in this revision

Viewing changes to etc/useR-2006-ESS/R-examples/Sweave-Example.Rnw

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2011-02-03 16:10:05 UTC
  • mfrom: (1.2.19 upstream)
  • Revision ID: james.westby@ubuntu.com-20110203161005-g1bg3cd5mtu15uh3
Tags: 5.13-1
New upstream version released today

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
\documentclass[12pt]{report}
 
2
 
 
3
%%
 
4
%% This is an Sweave file.  To generate a latex file from this, use (in R): 
 
5
%% > library(tools)
 
6
%% > Sweave("HVTNscenarios")
 
7
%% and then in a regular Unix (or MSDOS, or...) shell, 
 
8
%% $ latex HVTNscenarios
 
9
%% to generate the results.
 
10
%%
 
11
 
 
12
%% The following is for creating PDF.
 
13
\newif\ifpdf
 
14
\ifx\pdfoutput\undefined
 
15
  \pdffalse     % not running PDFLaTeX
 
16
\else
 
17
  \pdfoutput=1  % running PDFLaTeX
 
18
  \pdftrue
 
19
\fi
 
20
 
 
21
%\ifpdf
 
22
%  \usepackage{thumbpdf}
 
23
%\fi
 
24
%\usepackage{html,heqn,htmllist}
 
25
%\usepackage[latex2html]{hyperref} 
 
26
\ifpdf
 
27
  \usepackage[pdftex]{graphicx}
 
28
  \usepackage[pdftex]{hyperref} 
 
29
\else
 
30
  \usepackage[dvips]{graphicx}
 
31
  \usepackage[dvips]{hyperref} 
 
32
\fi
 
33
 
 
34
\usepackage{url}
 
35
\usepackage[authoryear,round]{natbib}
 
36
\usepackage{graphicx}
 
37
 
 
38
\addtolength{\oddsidemargin}{-0.5in}
 
39
\addtolength{\textheight}{0.2in}
 
40
\addtolength{\textwidth}{1in}
 
41
 
 
42
\hypersetup{backref,colorlinks=true,pagebackref=true,
 
43
  hyperindex=true}%pdfpagemode=FullScreen,
 
44
 
 
45
\ifpdf
 
46
  \DeclareGraphicsExtensions{.jpg,.pdf,.png,.mps}
 
47
\fi
 
48
 
 
49
\title{Analysis}
 
50
\author{AJ Rossini}
 
51
\date{\today}
 
52
 
 
53
\begin{document}
 
54
 
 
55
\maketitle
 
56
 
 
57
\tableofcontents
 
58
 
 
59
\listoffigures
 
60
 
 
61
\listoftables
 
62
 
 
63
\begin{abstract}
 
64
  An example for using Sweave.
 
65
\end{abstract}
 
66
 
 
67
%% For setting graphics for Sweave
 
68
%\setkeys{Gin}{width=5cm}
 
69
%\setkeys{Gin}{width=0.8\textwidth}
 
70
 
 
71
Possible settings for sweave chunks: 
 
72
\begin{itemize}
 
73
\item eval=TRUE
 
74
\item fig=TRUE, eps=FALSE
 
75
\item results=hide, tex
 
76
\item echo=TRUE
 
77
\item prefix=FALSE
 
78
\item include=FALSE
 
79
\end{itemize} 
 
80
 
 
81
Indexing works.  You can see that from Table~\ref{tab:1} and
 
82
Figure~\ref{fig:control}.
 
83
 
 
84
<<example data 1>>=
 
85
x <- rnorm(10) + 2
 
86
y <- x + rnorm(10)
 
87
my.lm.fit <- lm(y~x)
 
88
 
89
 
 
90
 
 
91
\chapter{Tables}
 
92
\label{CHA:tables}
 
93
 
 
94
<<Check library existence,echo=FALSE,eval=TRUE>>=
 
95
xtableExists <- require(xtable)
 
96
 
97
 
 
98
We have our first table
 
99
 
 
100
<<example output for data 1,results=tex,echo=FALSE>>=
 
101
if (xtableExists) {
 
102
   xtable(my.lm.fit)
 
103
}
 
104
 
105
 
 
106
and the second table, with Caption and \LaTeX{} reference label. 
 
107
 
 
108
<<example output for data 1,results=tex,echo=FALSE>>=
 
109
if (xtableExists) {
 
110
  xtable(my.lm.fit,caption="My First Table Caption",label="tab:1")
 
111
}
 
112
 
113
 
 
114
\clearpage
 
115
 
 
116
We could do the same thing and echo the code, as well:
 
117
 
 
118
We have our first table
 
119
 
 
120
<<example output for data 1,results=tex,echo=TRUE>>=
 
121
if (xtableExists) {
 
122
   xtable(my.lm.fit)
 
123
}
 
124
 
125
 
 
126
\clearpage
 
127
 
 
128
and the second table, with Caption and \LaTeX{} reference label. 
 
129
 
 
130
<<example output for data 1,results=tex,echo=TRUE>>=
 
131
if (xtableExists) {
 
132
  xtable(my.lm.fit,caption="My Second Table Caption",label="tab:2")
 
133
}
 
134
 
135
 
 
136
\chapter{Figures}
 
137
\label{CHA:figures}
 
138
 
 
139
Normally you can just include plots, by specifying that the chunk
 
140
generates a figure.
 
141
 
 
142
<<exampleFigureOne,fig=TRUE,echo=FALSE>>=
 
143
plot(rnorm(10),rnorm(10))
 
144
 
145
 
 
146
\clearpage
 
147
 
 
148
However, if you want more control, such as Labels on figures as well
 
149
as floating placement (i.e. letting \LaTeX{} place it for you
 
150
appropriately), you might:
 
151
 
 
152
\begin{figure}[htbp]
 
153
  \centering
 
154
<<exampleFigureTwo,fig=TRUE,echo=FALSE>>=  
 
155
plot(rnorm(10),rnorm(10))
 
156
 
157
  \caption{Example of more control}
 
158
  \label{fig:control}
 
159
\end{figure}
 
160
 
 
161
\end{document}