]> git.lyx.org Git - lyx.git/blob - lib/examples/sweave.lyx
Acknowledgments.lyx: fix a typo
[lyx.git] / lib / examples / sweave.lyx
1 #LyX 2.0 created this file. For more info see http://www.lyx.org/
2 \lyxformat 413
3 \begin_document
4 \begin_header
5 \textclass article
6 \use_default_options true
7 \begin_modules
8 sweave
9 \end_modules
10 \maintain_unincluded_children false
11 \language english
12 \language_package default
13 \inputencoding auto
14 \fontencoding global
15 \font_roman default
16 \font_sans default
17 \font_typewriter default
18 \font_default_family default
19 \use_non_tex_fonts false
20 \font_sc false
21 \font_osf false
22 \font_sf_scale 100
23 \font_tt_scale 100
24
25 \graphics default
26 \default_output_format default
27 \output_sync 0
28 \bibtex_command default
29 \index_command default
30 \paperfontsize default
31 \spacing single
32 \use_hyperref false
33 \papersize default
34 \use_geometry false
35 \use_amsmath 1
36 \use_esint 1
37 \use_mhchem 1
38 \use_mathdots 1
39 \cite_engine basic
40 \use_bibtopic false
41 \use_indices false
42 \paperorientation portrait
43 \suppress_date false
44 \use_refstyle 0
45 \index Index
46 \shortcut idx
47 \color #008000
48 \end_index
49 \secnumdepth 3
50 \tocdepth 3
51 \paragraph_separation skip
52 \defskip medskip
53 \quotes_language english
54 \papercolumns 1
55 \papersides 1
56 \paperpagestyle default
57 \tracking_changes false
58 \output_changes false
59 \html_math_output 0
60 \html_css_as_file 0
61 \html_be_strict false
62 \end_header
63
64 \begin_body
65
66 \begin_layout Title
67 A Test File
68 \end_layout
69
70 \begin_layout Author
71 Friedrich Leisch
72 \begin_inset Foot
73 status collapsed
74
75 \begin_layout Plain Layout
76 LyX 
77 \begin_inset Quotes eld
78 \end_inset
79
80 port
81 \begin_inset Quotes erd
82 \end_inset
83
84  by Gregor Gorjanc
85 \end_layout
86
87 \end_inset
88
89
90 \end_layout
91
92 \begin_layout Standard
93 A simple example that will run in any S engine: The integers from 1 to 10
94  are
95 \end_layout
96
97 \begin_layout Chunk
98 <<print=TRUE>>=
99 \end_layout
100
101 \begin_layout Chunk
102 1:10 
103 \end_layout
104
105 \begin_layout Chunk
106 <<results=hide>>=
107 \end_layout
108
109 \begin_layout Chunk
110 print(1:20)
111 \end_layout
112
113 \begin_layout Chunk
114 @ % the above is just to ensure that 2 code chunks can follow each other
115 \end_layout
116
117 \begin_layout Standard
118 We can also emulate a simple calculator: 
119 \end_layout
120
121 \begin_layout Chunk
122
123 \end_layout
124
125 \begin_layout Chunk
126 <<echo=TRUE,print=TRUE>>=
127 \end_layout
128
129 \begin_layout Chunk
130 1 + 1 
131 \end_layout
132
133 \begin_layout Chunk
134 1 + pi
135 \end_layout
136
137 \begin_layout Chunk
138 sin(pi/2)
139 \end_layout
140
141 \begin_layout Chunk
142 @
143 \end_layout
144
145 \begin_layout Standard
146 Now we look at Gaussian data:
147 \end_layout
148
149 \begin_layout Chunk
150
151 \end_layout
152
153 \begin_layout Chunk
154 <<>>=
155 \end_layout
156
157 \begin_layout Chunk
158 library(stats)
159 \end_layout
160
161 \begin_layout Chunk
162 x <- rnorm(20)
163 \end_layout
164
165 \begin_layout Chunk
166 print(x)
167 \end_layout
168
169 \begin_layout Chunk
170 print(t1 <- t.test(x))
171 \end_layout
172
173 \begin_layout Chunk
174 @
175 \end_layout
176
177 \begin_layout Standard
178 Note that we can easily integrate some numbers into standard text: The third
179  element of vector 
180 \family typewriter
181 x
182 \family default
183  is 
184 \begin_inset Flex S/R expression
185 status collapsed
186
187 \begin_layout Plain Layout
188
189 x[3]
190 \end_layout
191
192 \end_inset
193
194 , the 
195 \begin_inset Formula $p$
196 \end_inset
197
198 -value of the test is 
199 \begin_inset Flex S/R expression
200 status collapsed
201
202 \begin_layout Plain Layout
203
204 format.pval(t1$p.value)
205 \end_layout
206
207 \end_inset
208
209
210 \end_layout
211
212 \begin_layout Standard
213
214 \family roman
215 \series medium
216 \shape up
217 \size normal
218 \emph off
219 \bar no
220 \noun off
221 \color none
222 Now we look at a summary of the famous iris dataset, and we want to see
223  the commands in the code chunks:
224 \end_layout
225
226 \begin_layout Standard
227 \begin_inset Flex Sweave Options
228 status open
229
230 \begin_layout Plain Layout
231
232 echo=true,keep.source=true
233 \end_layout
234
235 \end_inset
236
237
238 \end_layout
239
240 \begin_layout Chunk
241 <<engine=R>>=
242 \end_layout
243
244 \begin_layout Chunk
245 # A comment
246 \end_layout
247
248 \begin_layout Chunk
249 data(iris)
250 \end_layout
251
252 \begin_layout Chunk
253 summary(iris)
254 \end_layout
255
256 \begin_layout Chunk
257 @ % def
258 \end_layout
259
260 \begin_layout Standard
261 \begin_inset Float figure
262 placement tbph
263 wide false
264 sideways false
265 status collapsed
266
267 \begin_layout Chunk
268
269 \end_layout
270
271 \begin_layout Chunk
272
273 \end_layout
274
275 \begin_layout Chunk
276 \align center
277
278 <<fig=TRUE>>=
279 \begin_inset Newline newline
280 \end_inset
281
282 library(graphics)
283 \begin_inset Newline newline
284 \end_inset
285
286 pairs(iris)
287 \begin_inset Newline newline
288 \end_inset
289
290 @
291 \end_layout
292
293 \begin_layout Plain Layout
294 \begin_inset Caption
295
296 \begin_layout Plain Layout
297 Pairs plot of the iris data.
298 \end_layout
299
300 \end_inset
301
302
303 \end_layout
304
305 \end_inset
306
307
308 \end_layout
309
310 \begin_layout Standard
311 \begin_inset Float figure
312 placement tbph
313 wide false
314 sideways false
315 status collapsed
316
317 \begin_layout Chunk
318 \align center
319
320 <<fig=true>>=
321 \begin_inset Newline newline
322 \end_inset
323
324 boxplot(Sepal.Length~Species,data=iris)
325 \begin_inset Newline newline
326 \end_inset
327
328 @
329 \end_layout
330
331 \begin_layout Plain Layout
332 \begin_inset Caption
333
334 \begin_layout Plain Layout
335 Boxplot of sepal length grouped by species.
336 \end_layout
337
338 \end_inset
339
340
341 \end_layout
342
343 \end_inset
344
345
346 \end_layout
347
348 \begin_layout Chunk
349 <<engine=S4>>=
350 \end_layout
351
352 \begin_layout Chunk
353 function.that.comes.only.with.Splus(x)
354 \end_layout
355
356 \begin_layout Chunk
357 @
358 \end_layout
359
360 \begin_layout Standard
361 \begin_inset Float figure
362 wide false
363 sideways false
364 status open
365
366 \begin_layout Chunk
367 \align center
368
369 <<fig=true>>=
370 \begin_inset Newline newline
371 \end_inset
372
373 boxplot(Sepal.Length~Species,data=iris)
374 \begin_inset Newline newline
375 \end_inset
376
377 @
378 \end_layout
379
380 \begin_layout Plain Layout
381 \begin_inset Caption
382
383 \begin_layout Plain Layout
384 Boxplot of sepal length grouped by species
385 \end_layout
386
387 \end_inset
388
389
390 \end_layout
391
392 \end_inset
393
394
395 \end_layout
396
397 \end_body
398 \end_document