1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
7 <meta name="generator" content="emacs 23.2.1; htmlfontify 0.21" />
8 <style type="text/css"><!--
9 body { font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; color: #e6e1dc; background: #232323; font-size: 8pt; text-decoration: none; }
10 span.default { font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; color: #e6e1dc; background: #232323; font-size: 8pt; text-decoration: none; }
11 span.default a { font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; color: #e6e1dc; background: #232323; font-size: 8pt; text-decoration: underline; }
12 span.org-footnote { color: #00ffff; text-decoration: underline; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; background: #232323; font-size: 8pt; }
13 span.org-footnote a { color: #00ffff; text-decoration: underline; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; background: #232323; font-size: 8pt; }
14 span.org-link { color: #00ffff; text-decoration: underline; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; background: #232323; font-size: 8pt; }
15 span.org-link a { color: #00ffff; text-decoration: underline; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; background: #232323; font-size: 8pt; }
16 span.org-code { color: #b3b3b3; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; background: #232323; font-size: 8pt; text-decoration: none; }
17 span.org-code a { color: #b3b3b3; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; background: #232323; font-size: 8pt; text-decoration: underline; }
18 span.italic { font-style: italic; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; color: #e6e1dc; background: #232323; font-size: 8pt; text-decoration: none; }
19 span.italic a { font-style: italic; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; color: #e6e1dc; background: #232323; font-size: 8pt; text-decoration: underline; }
20 span.org-block { color: #b3b3b3; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; background: #232323; font-size: 8pt; text-decoration: none; }
21 span.org-block a { color: #b3b3b3; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; background: #232323; font-size: 8pt; text-decoration: underline; }
22 span.comment { color: #bc9458; font-style: italic; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; background: #232323; font-size: 8pt; text-decoration: none; }
23 span.comment a { color: #bc9458; font-style: italic; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; background: #232323; font-size: 8pt; text-decoration: underline; }
24 span.org-meta-line { color: #bc9458; font-style: italic; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; background: #232323; font-size: 8pt; text-decoration: none; }
25 span.org-meta-line a { color: #bc9458; font-style: italic; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; background: #232323; font-size: 8pt; text-decoration: underline; }
26 span.org-document-info { color: #afeeee; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; background: #232323; font-size: 8pt; text-decoration: none; }
27 span.org-document-info a { color: #afeeee; font-family: DejaVu Sans Mono; font-stretch: normal; font-weight: 500; font-style: normal; background: #232323; font-size: 8pt; text-decoration: underline; }
28 span.org-document-title { color: #bc9458; font-weight: 700; font-family: DejaVu Sans Mono; font-stretch: normal; font-style: normal; background: #232323; font-size: 8pt; text-decoration: none; }
29 span.org-document-title a { color: #bc9458; font-weight: 700; font-family: DejaVu Sans Mono; font-stretch: normal; font-style: normal; background: #232323; font-size: 8pt; text-decoration: underline; }
30 span.org-document-info-keyword { color: #bc9458; font-weight: 700; font-family: DejaVu Sans Mono; font-stretch: normal; font-style: normal; background: #232323; font-size: 8pt; text-decoration: none; }
31 span.org-document-info-keyword a { color: #bc9458; font-weight: 700; font-family: DejaVu Sans Mono; font-stretch: normal; font-style: normal; background: #232323; font-size: 8pt; text-decoration: underline; }
34 <script type="text/javascript"><!--
35 // this function is needed to work around
36 // a bug in IE related to element attributes
37 function hasClass(obj)
40 if (obj.getAttributeNode("class") != null)
42 result = obj.getAttributeNode("class").value;
49 // the flag we'll use to keep track of
50 // whether the current row is odd or even
53 // if arguments are provided to specify the colors
54 // of the even & odd rows, then use the them;
55 // otherwise use the following defaults:
56 var evenColor = arguments[1] ? arguments[1] : "#fff";
57 var oddColor = arguments[2] ? arguments[2] : "#ddd";
59 // obtain a reference to the desired table
60 // if no such table exists, abort
61 var table = document.getElementById(id);
62 if (! table) { return; }
64 // by definition, tables can have more than one tbody
65 // element, so we'll have to get the list of child
67 var tbodies = table.getElementsByTagName("tbody");
69 // and iterate through them...
70 for (var h = 0; h < tbodies.length; h++)
72 // find all the <tr> elements...
73 var trs = tbodies[h].getElementsByTagName("tr");
75 // ... and iterate through them
76 for (var i = 0; i < trs.length; i++)
78 // avoid rows that have a class attribute
79 // or backgroundColor style
80 if (! hasClass(trs[i]) &&
81 ! trs[i].style.backgroundColor)
83 // get all the cells in this row...
84 var tds = trs[i].getElementsByTagName("td");
86 // and iterate through them...
87 for (var j = 0; j < tds.length; j++)
91 // avoid cells that have a class attribute
92 // or backgroundColor style
93 if (! hasClass(mytd) &&
94 ! mytd.style.backgroundColor)
96 mytd.style.backgroundColor =
97 even ? evenColor : oddColor;
101 // flip from odd to even, or vice-versa
107 function toggle_invis( name )
112 { var classname = node.id;
114 { var classbase = classname.substr( 0, name.length );
115 if( classbase == name ) { return NodeFilter.FILTER_ACCEPT; } }
116 return NodeFilter.FILTER_SKIP; } };
117 var walker = document.createTreeWalker( document.body ,
118 NodeFilter.SHOW_ELEMENT ,
121 while( walker.nextNode() )
123 var e = walker.currentNode;
124 if( e.style.display == "none" ) { e.style.display = "inline"; }
125 else { e.style.display = "none"; }
130 <body onload="stripe('index'); return true;">
132 <pre><span class="org-document-info-keyword" id="nil-1">#+TITLE:</span><span class="org-document-title" id="nil-9"> An Org-mode Demo</span>
133 <span class="org-document-info-keyword" id="nil-27">#+AUTHOR:</span><span class="org-document-info" id="nil-36"> Eric Schulte</span>
134 <span class="org-meta-line" id="nil-50">#+OPTIONS: num:nil ^:nil f:nil</span>
135 <span class="org-meta-line" id="nil-81">#+LATEX_HEADER: \usepackage{amscd}</span>
136 <span class="org-meta-line" id="nil-116">#+STARTUP: hideblocks</span>
137 <span class="org-meta-line" id="nil-138">#+BABEL: :session </span><span class="org-meta-line" id="nil-156">*R</span><span class="org-meta-line" id="nil-158">*</span><span class="org-meta-line" id="nil-159"> </span><span class="org-meta-line" id="nil-160">:results silent</span>
139 <span class="comment" id="nil-177"># some minor customization for nicer looking LaTeX output</span>
140 <span class="org-meta-line" id="nil-235">#+begin_LaTeX
141 </span><span class="org-block" id="nil-249"> \hypersetup{
145 \lstset{basicstyle=\ttfamily\bfseries\small}</span><span class="org-meta-line" id="nil-358">
148 <span class="org-meta-line" id="nil-372">#+begin_center
149 </span> Adapted from <span class="italic" id="nil-402">/</span><span onclick="toggle_invis('invisible-403-454');">…</span><span class="italic" id="invisible-403-454-403">[[http://www.stat.umn.edu/~charlie/Sweave/foo.Rnw]</span><span class="italic" id="invisible-403-454-453">[</span><span class="italic" id="nil-454">A</span><span class="italic" id="nil-455">n</span><span class="italic" id="nil-456"> Sweave Dem</span><span class="italic" id="nil-467">o</span><span onclick="toggle_invis('invisible-468-470');">…</span><span class="italic" id="invisible-468-470-468">]</span><span class="italic" id="invisible-468-470-469">]</span><span class="italic" id="nil-470">/</span> by Charles J. Geyer.<span class="org-meta-line" id="nil-492">
152 This is a demo for using Org-babel to produce LaTeX documents with
153 embedded R code. To get started fire up Emacs and create a text file
154 with the <span class="org-code" id="nil-653">=.org=</span> suffix. You should see Org-mode become your major
155 mode -- denoted by <span class="org-code" id="nil-730">=Org=</span> in your status bar.
157 Press <span class="org-code" id="nil-763">=C-c C-e=</span> while viewing this Org-mode buffer and you will see a
158 menu appear with options for export to a variety target formats --
159 herein we'll only consider export to LaTeX.
161 So now we have a more complicated file chain
165 @>\texttt{Sweave}>>
167 @>\texttt{latex}>>
169 @>\texttt{xdvi}>>
170 \text{view of document}
173 and what have we accomplished other than making it twice as annoying
174 as the WYSIWYG crows (having to use both <span class="org-code" id="nil-1273">=Org-mode=</span> and <span class="org-code" id="nil-1288">=latex=</span> to get
175 anything that looks like the document)?
177 Well, we can now include <span class="org-code" id="nil-1369">=R=</span> in our document. Here's a simple example
178 <span class="org-meta-line" id="nil-1415">#+begin_src R :exports both
179 </span><span class="org-block" id="nil-1443"> 2 + 2</span><span class="org-meta-line" id="nil-1450">
181 What I actually typed in <span class="org-code" id="nil-1486">=foo.org=</span> was
182 <span class="org-code" id="nil-1500">: #+begin_src R :exports both
186 This is a "code block" to be processed by Org-babel. When Org-babel
187 hits such a thing, it processes it, runs R to get the results, and
188 stuffs the output in the LaTeX file it is creating. The LaTeX between
189 code chunks is copied verbatim (except for in-line src code, about
190 which see below). Hence to create a <span class="italic" id="nil-1864">/active/</span> document you just write
191 plain old text interspersed with "code blocks" which are plain old R.
193 <span class="org-meta-line" id="nil-1968">#+LaTeX:</span><span class="org-block" id="nil-1976"> \pagebreak</span><span class="org-block" id="nil-1987">[3</span><span class="org-block" id="nil-1989">]</span>
195 Plots get a little more complicated. First we make something to plot
196 (simulated regression data).
197 <span class="org-meta-line" id="nil-2091">#+source: reg</span>
198 <span class="org-meta-line" id="nil-2105">#+begin_src R :results output :exports both
199 </span><span class="org-block" id="nil-2149"> n <- 50
203 y.true <- a.true + b.true </span><span class="org-block" id="nil-2234">* </span><span class="org-block" id="nil-2236">x
205 y <- y.true + s.true </span><span class="org-block" id="nil-2278">* </span><span class="org-block" id="nil-2280">rnorm(n)
207 summary(out1)</span><span class="org-meta-line" id="nil-2324">
209 (for once we won't show the code chunk itself, look at <span class="org-code" id="nil-2390">=foo.org=</span> if
210 you want to see what the actual code chunk was).
212 Figure \ref{fig:one} (p. \pageref{fig:one}) is produced by the following code
213 <span class="org-meta-line" id="nil-2531">#+srcname: fig1plot</span>
214 <span class="org-meta-line" id="nil-2551">#+begin_src R :exports code
215 </span><span class="org-block" id="nil-2579"> plot(x, y)
216 abline(out1)</span><span class="org-meta-line" id="nil-2606">
218 Note that <span class="org-code" id="nil-2627">=x=</span>, <span class="org-code" id="nil-2632">=y=</span>, and <span class="org-code" id="nil-2641">=out1=</span> are remembered from the preceding code
219 chunk. We don't have to regenerate them. All code chunks are part of
220 one R "session".
221 <span class="org-meta-line" id="nil-2775">#+source: fig1</span>
222 <span class="org-meta-line" id="nil-2790">#+begin_src R :exports results :noweb yes :file fig1.pdf
223 </span><span class="org-block" id="nil-2847"> <<fig1plot>></span><span class="org-meta-line" id="nil-2861">
226 <span class="org-meta-line" id="nil-2873">#+attr_latex: width=0.8\textwidth,placement=[p]</span>
227 <span class="org-meta-line" id="nil-2921">#+label: fig:one</span>
228 <span class="org-meta-line" id="nil-2938">#+caption: Scatter Plot with Regression Line</span>
229 <span class="org-meta-line" id="nil-2983">#+results: fig1</span>
230 <span onclick="toggle_invis('invisible-2999-3001');">…</span><span class="org-link" id="invisible-2999-3001-2999">[</span><span class="org-link" id="invisible-2999-3001-3000">[</span><span class="org-link" id="nil-3001">file:fig1.pd</span><span class="org-link" id="nil-3013">f</span><span onclick="toggle_invis('invisible-3014-3016');">…</span><span class="org-link" id="invisible-3014-3016-3014">]</span><span class="org-link" id="invisible-3014-3016-3015">]</span>
232 Now this was a little tricky. We did this with two code chunks,
233 one visible and one invisible. First we did
234 <span class="org-code" id="nil-3128">: #+srcname: fig1plot
235 : #+begin_src R :exports code :file fig1plot.pdf
239 </span>where the <span class="org-code" id="nil-3253">=:exports code=</span> indicates that only the return value (not
240 code) should be exported and the <span class="org-code" id="nil-3344">=#+srcname: fig1plot=</span> gives the code
241 block a name (to be used later). And "later" is almost immediate.
243 <span class="org-code" id="nil-3460">: #+source: fig1
244 : #+begin_src R :exports results :noweb yes :file fig1.pdf
245 : <<fig1plot>>
248 In this code block the <span class="org-code" id="nil-3589">=:file fig1.pdf=</span> header argumentindicates that
249 the block generates a figure. Org-babel automagically makes a PDF
250 file for the figure, and Org-mode handles the export to LaTeX. The
251 <span class="org-code" id="nil-3771">=<<fig1plot>>=</span> is an example of "code block reuse". It means that we
252 reuse the code of the code chunk named <span class="org-code" id="nil-3880">=fig1plot=</span>. The <span class="org-code" id="nil-3897">=:exports
253 results=</span> in the code block means just what it says (we've already seen
254 the code---it was produced by the preceding chunk---and we don't want
255 to see it again, we only want to see the results). It is important
256 that we observe the DRY/SPOT rule (<span class="italic" id="nil-4151">/don't repeat yourself/</span> or <span class="italic" id="nil-4178">/single
257 point of truth/</span>) and only have one bit of code for generating the
258 plot. What the reader sees is guaranteed to be the code that made the
259 plot. If we had used cut-and-paste, just repeating the code, the
260 duplicated code might get out of sync after edits. The rest of this
261 should be recognizable to anyone who has ever done a LaTeX figure.
263 So making a figure is a bit more complicated in some ways, but much simpler
264 than others. Note the following virtues
265 - The figure is guaranteed to be the one described by the text (at
266 least by the R in the text).
267 - No messing around with sizing or rotations. It just works!
269 <span class="org-meta-line" id="nil-4804">#+source: fig2</span>
270 <span class="org-meta-line" id="nil-4819">#+begin_src R :exports results :file fig2.pdf
271 </span><span class="org-block" id="nil-4865"> out3 <- lm(y ~ x + I(x^2) + I(x^3))
273 curve(predict(out3, newdata=data.frame(x=x)), add = TRUE)</span><span class="org-meta-line" id="nil-4975">
276 Note that if you don't care to show the R code to make the figure, it
277 is simpler still. Figure \ref{fig:two} shows another plot. What I
278 actually typed in <span class="org-code" id="nil-5143">=foo.org=</span> was
279 <span class="org-code" id="nil-5157">: #+srcname: fig2
280 : #+begin_src R :exports results :file fig2.pdf
281 : out3 <- lm(y ~ x + I(x^2) + I(x^3))
283 : curve(predict(out3, newdata=data.frame(x=x)), add = TRUE)
286 <span class="org-meta-line" id="nil-5353">#+attr_latex: width=0.8\textwidth,placement=[p]</span>
287 <span class="org-meta-line" id="nil-5401">#+label: fig:two</span>
288 <span class="org-meta-line" id="nil-5418">#+caption: Scatter Plot with Cubic Regression Curve</span>
289 <span class="org-meta-line" id="nil-5470">#+results: fig2</span>
290 <span onclick="toggle_invis('invisible-5486-5488');">…</span><span class="org-link" id="invisible-5486-5488-5486">[</span><span class="org-link" id="invisible-5486-5488-5487">[</span><span class="org-link" id="nil-5488">file:fig2.pd</span><span class="org-link" id="nil-5500">f</span><span onclick="toggle_invis('invisible-5501-5503');">…</span><span class="org-link" id="invisible-5501-5503-5501">]</span><span class="org-link" id="invisible-5501-5503-5502">]</span>
292 <span class="org-meta-line" id="nil-5505">#+LaTeX:</span><span class="org-block" id="nil-5513"> \pagebreak</span>
294 Now we just excluded the code for the plot from the figure (with
295 <span class="org-code" id="nil-5591">=:exports results=</span> so it doesn't show).
297 Also note that every time we re-export Figures \ref{fig:one}
298 and \ref{fig:two} change, the latter conspicuously (because the
299 simulated data are random). Everything just works. This should tell
300 you the main virtue of Org-babel. It's always correct. There is
301 never a problem with stale cut-and-paste.
303 <span class="org-meta-line" id="nil-5936">#+begin_src R :exports none
304 </span><span class="org-block" id="nil-5964"> options(scipen=10)</span><span class="org-meta-line" id="nil-5984">
307 <span class="org-meta-line" id="nil-5996">#+results:</span>
308 <span class="org-code" id="nil-6007">: 0
309 </span>Simple numbers can be plugged into the text with the <span class="org-code" id="nil-6064">=src_R=</span> command,
310 for example, the quadratic and cubic regression coefficients in the
311 preceding regression were \beta_2 = src_R{round(out3$coef<span class="org-footnote" id="nil-6206">[3</span><span class="org-footnote" id="nil-6208">]</span>, 4)} and \beta_3
312 = src_R{round(out3$coef<span class="org-footnote" id="nil-6250">[4</span><span class="org-footnote" id="nil-6252">]</span>, 4)}. Just magic! What I actually typed
313 in <span class="org-code" id="nil-6299">=foo.org=</span> was
314 <span class="org-code" id="nil-6313">: were </span><span class="org-code" id="nil-6320">\beta</span><span class="org-code" id="nil-6325">_2 = src_R{round(out3$coef</span><span class="org-code" id="nil-6351">[3</span><span class="org-code" id="nil-6353">]</span><span class="org-code" id="nil-6354">, 4)}
315 : and </span><span class="org-code" id="nil-6366">\beta</span><span class="org-code" id="nil-6371">_3 = src_R{round(out3$coef</span><span class="org-code" id="nil-6397">[4</span><span class="org-code" id="nil-6399">]</span><span class="org-code" id="nil-6400">, 4)}
316 </span><span class="org-meta-line" id="nil-6406">#+begin_src R :exports none
317 </span><span class="org-block" id="nil-6434"> options(scipen=0)</span><span class="org-meta-line" id="nil-6453">
320 The <span class="org-code" id="nil-6469">=xtable=</span> command is used to make tables. (The following is the
321 Org-babel output of another code block that we don't explicitly show.
322 Look at <span class="org-code" id="nil-6611">=foo.org=</span> for details.)
323 <span class="org-meta-line" id="nil-6635">#+begin_src R :exports both :results output
324 </span><span class="org-block" id="nil-6679"> out2 <- lm(y ~ x + I(x^2))
325 foo <- anova(out1, out2, out3)
326 foo</span><span class="org-meta-line" id="nil-6746">
329 <span class="org-meta-line" id="nil-6758">#+begin_src R :exports both :results output
330 </span><span class="org-block" id="nil-6802"> class(foo)</span><span class="org-meta-line" id="nil-6814">
333 <span class="org-meta-line" id="nil-6826">#+begin_src R :exports both :results output
334 </span><span class="org-block" id="nil-6870"> dim(foo)</span><span class="org-meta-line" id="nil-6880">
337 <span class="org-meta-line" id="nil-6892">#+source: foo-as-matrix</span>
338 <span class="org-meta-line" id="nil-6916">#+begin_src R :exports both :results output
339 </span><span class="org-block" id="nil-6960"> foo <- as.matrix(foo)
340 foo</span><span class="org-meta-line" id="nil-6989">
343 <span class="org-meta-line" id="nil-7001">#+LaTeX:</span><span class="org-block" id="nil-7009"> \pagebreak</span>
345 <span class="org-meta-line" id="nil-7022">#+begin_src R :results output latex :exports results
346 </span><span class="org-block" id="nil-7075"> library(xtable)
347 xtable(foo, caption = "ANOVA Table", label = "tab:one",
348 digits = c(0, 0, 2, 0, 2, 3, 3))</span><span class="org-meta-line" id="nil-7189">
351 <span class="org-meta-line" id="nil-7201">#+results: foo-as-matrix</span>
353 So now we are ready to turn the matrix <span class="org-code" id="nil-7266">=foo=</span> into Table \ref{tab:one}
355 <span class="org-code" id="nil-7315">: #+begin_src R :results output latex :exports results
357 : xtable(foo, caption = "ANOVA Table", label = "tab:one",
358 : digits = c(0, 0, 2, 0, 2, 3, 3))
361 (note the difference between arguments to the <span class="org-code" id="nil-7550">=xtable=</span> function and to
362 the <span class="org-code" id="nil-7579">=xtable=</span> method of the <span class="org-code" id="nil-7602">=print=</span> function)
364 To summarize, Org-babel is terrific, so important that soon we'll not
365 be able to get along without it. It's virtues are
366 - The numbers and graphics you report are actually what they
368 - Your analysis is reproducible. Even years later, when you've
369 completely forgotten what you did, the whole write-up, every single
370 number or pixel in a plot is reproducible.
371 - Your analysis actually works---at least in this particular instance.
372 The code you show actually executes without error.
373 - Toward the end of your work, with the write-up almost done you
374 discover an error. Months of rework to do? No! Just fix the error
375 and rerun <span class="org-code" id="nil-8275">=Sweave=</span> and <span class="org-code" id="nil-8288">=latex=</span>. One single problem like this and
376 you will have all the time invested in <span class="org-code" id="nil-8372">=Sweave=</span> repaid.
377 - This methodology provides dicipline. There's nothing that will make
378 you clean up your code like the prospect of actually revealing it to
381 Whether we're talking about homework, a consulting report, a textbook,
382 or a research paper. If they involve computing and statistics, this