foo.org.html 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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">
  5. <head>
  6. <title>foo.org</title>
  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; }
  32. --></style>
  33. <script type="text/javascript"><!--
  34. // this function is needed to work around
  35. // a bug in IE related to element attributes
  36. function hasClass(obj)
  37. {
  38. var result = false;
  39. if (obj.getAttributeNode("class") != null)
  40. {
  41. result = obj.getAttributeNode("class").value;
  42. }
  43. return result;
  44. }
  45. function stripe(id)
  46. {
  47. // the flag we'll use to keep track of
  48. // whether the current row is odd or even
  49. var even = false;
  50. // if arguments are provided to specify the colors
  51. // of the even & odd rows, then use the them;
  52. // otherwise use the following defaults:
  53. var evenColor = arguments[1] ? arguments[1] : "#fff";
  54. var oddColor = arguments[2] ? arguments[2] : "#ddd";
  55. // obtain a reference to the desired table
  56. // if no such table exists, abort
  57. var table = document.getElementById(id);
  58. if (! table) { return; }
  59. // by definition, tables can have more than one tbody
  60. // element, so we'll have to get the list of child
  61. // &lt;tbody&gt;s
  62. var tbodies = table.getElementsByTagName("tbody");
  63. // and iterate through them...
  64. for (var h = 0; h < tbodies.length; h++)
  65. {
  66. // find all the &lt;tr&gt; elements...
  67. var trs = tbodies[h].getElementsByTagName("tr");
  68. // ... and iterate through them
  69. for (var i = 0; i < trs.length; i++)
  70. {
  71. // avoid rows that have a class attribute
  72. // or backgroundColor style
  73. if (! hasClass(trs[i]) &&
  74. ! trs[i].style.backgroundColor)
  75. {
  76. // get all the cells in this row...
  77. var tds = trs[i].getElementsByTagName("td");
  78. // and iterate through them...
  79. for (var j = 0; j < tds.length; j++)
  80. {
  81. var mytd = tds[j];
  82. // avoid cells that have a class attribute
  83. // or backgroundColor style
  84. if (! hasClass(mytd) &&
  85. ! mytd.style.backgroundColor)
  86. {
  87. mytd.style.backgroundColor =
  88. even ? evenColor : oddColor;
  89. }
  90. }
  91. }
  92. // flip from odd to even, or vice-versa
  93. even = ! even;
  94. }
  95. }
  96. }
  97. function toggle_invis( name )
  98. {
  99. var filter =
  100. { acceptNode:
  101. function( node )
  102. { var classname = node.id;
  103. if( classname )
  104. { var classbase = classname.substr( 0, name.length );
  105. if( classbase == name ) { return NodeFilter.FILTER_ACCEPT; } }
  106. return NodeFilter.FILTER_SKIP; } };
  107. var walker = document.createTreeWalker( document.body ,
  108. NodeFilter.SHOW_ELEMENT ,
  109. filter ,
  110. false );
  111. while( walker.nextNode() )
  112. {
  113. var e = walker.currentNode;
  114. if( e.style.display == "none" ) { e.style.display = "inline"; }
  115. else { e.style.display = "none"; }
  116. }
  117. }
  118. --> </script>
  119. </head>
  120. <body onload="stripe('index'); return true;">
  121. <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>
  122. <span class="org-document-info-keyword" id="nil-27">#+AUTHOR:</span><span class="org-document-info" id="nil-36"> Eric Schulte</span>
  123. <span class="org-meta-line" id="nil-50">#+OPTIONS: num:nil ^:nil f:nil</span>
  124. <span class="org-meta-line" id="nil-81">#+LATEX_HEADER: \usepackage{amscd}</span>
  125. <span class="org-meta-line" id="nil-116">#+STARTUP: hideblocks</span>
  126. <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>
  127. <span class="comment" id="nil-177"># some minor customization for nicer looking LaTeX output</span>
  128. <span class="org-meta-line" id="nil-235">#+begin_LaTeX
  129. </span><span class="org-block" id="nil-249"> \hypersetup{
  130. linkcolor=blue,
  131. pdfborder={0 0 0 0}
  132. }
  133. \lstset{basicstyle=\ttfamily\bfseries\small}</span><span class="org-meta-line" id="nil-358">
  134. #+end_LaTeX</span>
  135. <span class="org-meta-line" id="nil-372">#+begin_center
  136. </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">
  137. #+end_center</span>
  138. This is a demo for using Org-babel to produce LaTeX documents with
  139. embedded R code. To get started fire up Emacs and create a text file
  140. with the <span class="org-code" id="nil-653">=.org=</span> suffix. You should see Org-mode become your major
  141. mode -- denoted by <span class="org-code" id="nil-730">=Org=</span> in your status bar.
  142. Press <span class="org-code" id="nil-763">=C-c C-e=</span> while viewing this Org-mode buffer and you will see a
  143. menu appear with options for export to a variety target formats --
  144. herein we'll only consider export to LaTeX.
  145. So now we have a more complicated file chain
  146. $$
  147. \begin{CD}
  148. \texttt{foo.org}
  149. @&gt;\texttt{Sweave}&gt;&gt;
  150. \texttt{foo.tex}
  151. @&gt;\texttt{latex}&gt;&gt;
  152. \texttt{foo.dvi}
  153. @&gt;\texttt{xdvi}&gt;&gt;
  154. \text{view of document}
  155. \end{CD}
  156. $$
  157. and what have we accomplished other than making it twice as annoying
  158. 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
  159. anything that looks like the document)?
  160. Well, we can now include <span class="org-code" id="nil-1369">=R=</span> in our document. Here's a simple example
  161. <span class="org-meta-line" id="nil-1415">#+begin_src R :exports both
  162. </span><span class="org-block" id="nil-1443"> 2 + 2</span><span class="org-meta-line" id="nil-1450">
  163. #+end_src</span>
  164. What I actually typed in <span class="org-code" id="nil-1486">=foo.org=</span> was
  165. <span class="org-code" id="nil-1500">: #+begin_src R :exports both
  166. : 2 + 2
  167. : #+end_src
  168. </span>
  169. This is a &quot;code block&quot; to be processed by Org-babel. When Org-babel
  170. hits such a thing, it processes it, runs R to get the results, and
  171. stuffs the output in the LaTeX file it is creating. The LaTeX between
  172. code chunks is copied verbatim (except for in-line src code, about
  173. which see below). Hence to create a <span class="italic" id="nil-1864">/active/</span> document you just write
  174. plain old text interspersed with &quot;code blocks&quot; which are plain old R.
  175. <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>
  176. Plots get a little more complicated. First we make something to plot
  177. (simulated regression data).
  178. <span class="org-meta-line" id="nil-2091">#+source: reg</span>
  179. <span class="org-meta-line" id="nil-2105">#+begin_src R :results output :exports both
  180. </span><span class="org-block" id="nil-2149"> n &lt;- 50
  181. x &lt;- seq(1, n)
  182. a.true &lt;- 3
  183. b.true &lt;- 1.5
  184. y.true &lt;- a.true + b.true </span><span class="org-block" id="nil-2234">* </span><span class="org-block" id="nil-2236">x
  185. s.true &lt;- 17.3
  186. y &lt;- y.true + s.true </span><span class="org-block" id="nil-2278">* </span><span class="org-block" id="nil-2280">rnorm(n)
  187. out1 &lt;- lm(y ~ x)
  188. summary(out1)</span><span class="org-meta-line" id="nil-2324">
  189. #+end_src</span>
  190. (for once we won't show the code chunk itself, look at <span class="org-code" id="nil-2390">=foo.org=</span> if
  191. you want to see what the actual code chunk was).
  192. Figure \ref{fig:one} (p. \pageref{fig:one}) is produced by the following code
  193. <span class="org-meta-line" id="nil-2531">#+srcname: fig1plot</span>
  194. <span class="org-meta-line" id="nil-2551">#+begin_src R :exports code
  195. </span><span class="org-block" id="nil-2579"> plot(x, y)
  196. abline(out1)</span><span class="org-meta-line" id="nil-2606">
  197. #+end_src</span>
  198. 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
  199. chunk. We don't have to regenerate them. All code chunks are part of
  200. one R &quot;session&quot;.
  201. <span class="org-meta-line" id="nil-2775">#+source: fig1</span>
  202. <span class="org-meta-line" id="nil-2790">#+begin_src R :exports results :noweb yes :file fig1.pdf
  203. </span><span class="org-block" id="nil-2847"> &lt;&lt;fig1plot&gt;&gt;</span><span class="org-meta-line" id="nil-2861">
  204. #+end_src</span>
  205. <span class="org-meta-line" id="nil-2873">#+attr_latex: width=0.8\textwidth,placement=[p]</span>
  206. <span class="org-meta-line" id="nil-2921">#+label: fig:one</span>
  207. <span class="org-meta-line" id="nil-2938">#+caption: Scatter Plot with Regression Line</span>
  208. <span class="org-meta-line" id="nil-2983">#+results: fig1</span>
  209. <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>
  210. Now this was a little tricky. We did this with two code chunks,
  211. one visible and one invisible. First we did
  212. <span class="org-code" id="nil-3128">: #+srcname: fig1plot
  213. : #+begin_src R :exports code :file fig1plot.pdf
  214. : plot(x, y)
  215. : abline(out1)
  216. : #+end_src
  217. </span>where the <span class="org-code" id="nil-3253">=:exports code=</span> indicates that only the return value (not
  218. code) should be exported and the <span class="org-code" id="nil-3344">=#+srcname: fig1plot=</span> gives the code
  219. block a name (to be used later). And &quot;later&quot; is almost immediate.
  220. Next we did
  221. <span class="org-code" id="nil-3460">: #+source: fig1
  222. : #+begin_src R :exports results :noweb yes :file fig1.pdf
  223. : &lt;&lt;fig1plot&gt;&gt;
  224. : #+end_src
  225. </span>
  226. In this code block the <span class="org-code" id="nil-3589">=:file fig1.pdf=</span> header argumentindicates that
  227. the block generates a figure. Org-babel automagically makes a PDF
  228. file for the figure, and Org-mode handles the export to LaTeX. The
  229. <span class="org-code" id="nil-3771">=&lt;&lt;fig1plot&gt;&gt;=</span> is an example of &quot;code block reuse&quot;. It means that we
  230. 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
  231. results=</span> in the code block means just what it says (we've already seen
  232. the code---it was produced by the preceding chunk---and we don't want
  233. to see it again, we only want to see the results). It is important
  234. 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
  235. point of truth/</span>) and only have one bit of code for generating the
  236. plot. What the reader sees is guaranteed to be the code that made the
  237. plot. If we had used cut-and-paste, just repeating the code, the
  238. duplicated code might get out of sync after edits. The rest of this
  239. should be recognizable to anyone who has ever done a LaTeX figure.
  240. So making a figure is a bit more complicated in some ways, but much simpler
  241. than others. Note the following virtues
  242. - The figure is guaranteed to be the one described by the text (at
  243. least by the R in the text).
  244. - No messing around with sizing or rotations. It just works!
  245. <span class="org-meta-line" id="nil-4804">#+source: fig2</span>
  246. <span class="org-meta-line" id="nil-4819">#+begin_src R :exports results :file fig2.pdf
  247. </span><span class="org-block" id="nil-4865"> out3 &lt;- lm(y ~ x + I(x^2) + I(x^3))
  248. plot(x, y)
  249. curve(predict(out3, newdata=data.frame(x=x)), add = TRUE)</span><span class="org-meta-line" id="nil-4975">
  250. #+end_src</span>
  251. Note that if you don't care to show the R code to make the figure, it
  252. is simpler still. Figure \ref{fig:two} shows another plot. What I
  253. actually typed in <span class="org-code" id="nil-5143">=foo.org=</span> was
  254. <span class="org-code" id="nil-5157">: #+srcname: fig2
  255. : #+begin_src R :exports results :file fig2.pdf
  256. : out3 &lt;- lm(y ~ x + I(x^2) + I(x^3))
  257. : plot(x, y)
  258. : curve(predict(out3, newdata=data.frame(x=x)), add = TRUE)
  259. : #+end_src
  260. </span>
  261. <span class="org-meta-line" id="nil-5353">#+attr_latex: width=0.8\textwidth,placement=[p]</span>
  262. <span class="org-meta-line" id="nil-5401">#+label: fig:two</span>
  263. <span class="org-meta-line" id="nil-5418">#+caption: Scatter Plot with Cubic Regression Curve</span>
  264. <span class="org-meta-line" id="nil-5470">#+results: fig2</span>
  265. <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>
  266. <span class="org-meta-line" id="nil-5505">#+LaTeX:</span><span class="org-block" id="nil-5513"> \pagebreak</span>
  267. Now we just excluded the code for the plot from the figure (with
  268. <span class="org-code" id="nil-5591">=:exports results=</span> so it doesn't show).
  269. Also note that every time we re-export Figures \ref{fig:one}
  270. and \ref{fig:two} change, the latter conspicuously (because the
  271. simulated data are random). Everything just works. This should tell
  272. you the main virtue of Org-babel. It's always correct. There is
  273. never a problem with stale cut-and-paste.
  274. <span class="org-meta-line" id="nil-5936">#+begin_src R :exports none
  275. </span><span class="org-block" id="nil-5964"> options(scipen=10)</span><span class="org-meta-line" id="nil-5984">
  276. #+end_src</span>
  277. <span class="org-meta-line" id="nil-5996">#+results:</span>
  278. <span class="org-code" id="nil-6007">: 0
  279. </span>Simple numbers can be plugged into the text with the <span class="org-code" id="nil-6064">=src_R=</span> command,
  280. for example, the quadratic and cubic regression coefficients in the
  281. 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
  282. = 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
  283. in <span class="org-code" id="nil-6299">=foo.org=</span> was
  284. <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)}
  285. : 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)}
  286. </span><span class="org-meta-line" id="nil-6406">#+begin_src R :exports none
  287. </span><span class="org-block" id="nil-6434"> options(scipen=0)</span><span class="org-meta-line" id="nil-6453">
  288. #+end_src</span>
  289. The <span class="org-code" id="nil-6469">=xtable=</span> command is used to make tables. (The following is the
  290. Org-babel output of another code block that we don't explicitly show.
  291. Look at <span class="org-code" id="nil-6611">=foo.org=</span> for details.)
  292. <span class="org-meta-line" id="nil-6635">#+begin_src R :exports both :results output
  293. </span><span class="org-block" id="nil-6679"> out2 &lt;- lm(y ~ x + I(x^2))
  294. foo &lt;- anova(out1, out2, out3)
  295. foo</span><span class="org-meta-line" id="nil-6746">
  296. #+end_src</span>
  297. <span class="org-meta-line" id="nil-6758">#+begin_src R :exports both :results output
  298. </span><span class="org-block" id="nil-6802"> class(foo)</span><span class="org-meta-line" id="nil-6814">
  299. #+end_src</span>
  300. <span class="org-meta-line" id="nil-6826">#+begin_src R :exports both :results output
  301. </span><span class="org-block" id="nil-6870"> dim(foo)</span><span class="org-meta-line" id="nil-6880">
  302. #+end_src</span>
  303. <span class="org-meta-line" id="nil-6892">#+source: foo-as-matrix</span>
  304. <span class="org-meta-line" id="nil-6916">#+begin_src R :exports both :results output
  305. </span><span class="org-block" id="nil-6960"> foo &lt;- as.matrix(foo)
  306. foo</span><span class="org-meta-line" id="nil-6989">
  307. #+end_src</span>
  308. <span class="org-meta-line" id="nil-7001">#+LaTeX:</span><span class="org-block" id="nil-7009"> \pagebreak</span>
  309. <span class="org-meta-line" id="nil-7022">#+begin_src R :results output latex :exports results
  310. </span><span class="org-block" id="nil-7075"> library(xtable)
  311. xtable(foo, caption = &quot;ANOVA Table&quot;, label = &quot;tab:one&quot;,
  312. digits = c(0, 0, 2, 0, 2, 3, 3))</span><span class="org-meta-line" id="nil-7189">
  313. #+end_src</span>
  314. <span class="org-meta-line" id="nil-7201">#+results: foo-as-matrix</span>
  315. So now we are ready to turn the matrix <span class="org-code" id="nil-7266">=foo=</span> into Table \ref{tab:one}
  316. using the R chunk
  317. <span class="org-code" id="nil-7315">: #+begin_src R :results output latex :exports results
  318. : library(xtable)
  319. : xtable(foo, caption = &quot;ANOVA Table&quot;, label = &quot;tab:one&quot;,
  320. : digits = c(0, 0, 2, 0, 2, 3, 3))
  321. : #+end_src
  322. </span>
  323. (note the difference between arguments to the <span class="org-code" id="nil-7550">=xtable=</span> function and to
  324. the <span class="org-code" id="nil-7579">=xtable=</span> method of the <span class="org-code" id="nil-7602">=print=</span> function)
  325. To summarize, Org-babel is terrific, so important that soon we'll not
  326. be able to get along without it. Its virtues are
  327. - The numbers and graphics you report are actually what they
  328. are claimed to be.
  329. - Your analysis is reproducible. Even years later, when you've
  330. completely forgotten what you did, the whole write-up, every single
  331. number or pixel in a plot is reproducible.
  332. - Your analysis actually works---at least in this particular instance.
  333. The code you show actually executes without error.
  334. - Toward the end of your work, with the write-up almost done you
  335. discover an error. Months of rework to do? No! Just fix the error
  336. 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
  337. you will have all the time invested in <span class="org-code" id="nil-8372">=Sweave=</span> repaid.
  338. - This methodology provides dicipline. There's nothing that will make
  339. you clean up your code like the prospect of actually revealing it to
  340. the world.
  341. Whether we're talking about homework, a consulting report, a textbook,
  342. or a research paper. If they involve computing and statistics, this
  343. is the way to do it.
  344. </pre>
  345. </body>
  346. </html>