typo in ob-worg.org
[org-mode/org-tableheadings.git] / org-babel-worg.org
blob41f2ca845bfcbe3e8c4b16dd830bdeef3942fb44
1 #+OPTIONS:    H:3 num:nil toc:2 \n:nil @:t ::t |:t ^:t -:t f:t *:t TeX:t LaTeX:t skip:nil d:(HIDE) tags:not-in-toc
2 #+STARTUP:    align fold nodlcheck hidestars oddeven lognotestate
3 #+SEQ_TODO:   TODO(t) INPROGRESS(i) WAITING(w@) | DONE(d) CANCELED(c@)
4 #+TAGS:       Write(w) Update(u) Fix(f) Check(c) 
5 #+TITLE:      Org-babel
6 #+AUTHOR:     Dan Davison, Eric Schulte
7 #+EMAIL:      davison at stats dot ox dot ac dot uk
8 #+LANGUAGE:   en
9 #+CATEGORY:   worg
11 #+begin_html 
12   <div id="subtitle">
13     <p>executable source code blocks in org-mode</p>
14   </div>
15   <div id="logo">
16     <p>
17       <img src="images/tower-of-babel.png"  alt="images/tower-of-babel.png"
18            title="And the Lord said, Behold, the people is one, and they have all one language; and this they begin to do; and now nothing will be restrained from them, which they have imagined to do. Genesis 11:1-9"/>
19       <div id="attr">
20         from
21         <a href="http://www.flickr.com/photos/23379658@N05/" title=""><b>Martijn Streefkerk</b></a>
22       </div>
23     </p>
24   </div>
25 #+end_html
27 * Introduction
28   :PROPERTIES:
29   :CUSTOM_ID: introduction
30   :END:
31   Org-babel provides the following modifications to [[http://orgmode.org/manual/Literal-examples.html][the existing
32   support]] for blocks of source code examples in the org-mode core.
33   1. source code execution
34   2. arguments to source code blocks
35   3. exportation of source code blocks to files (literate programming)
37 * Getting started
38   :PROPERTIES:
39   :CUSTOM_ID: getting-started
40   :END:
41   Grab the latest code from the git repo at [[http://github.com/eschulte/org-babel/tree/master][github/org-babel]]
42 #+begin_src sh
43 git clone git://github.com/eschulte/org-babel.git
44 #+end_src
46   And add the following lines to your .emacs, replacing the path as
47   appropriate. A good place to check that things are up and running
48   would the examples in [[* Basic org-babel functionality][Basic org-babel functionality]].
49 #+begin_src emacs-lisp
50   (add-to-list 'load-path "/path/to/org-babel/lisp")
51   (require 'org-babel-init)
52 #+end_src
53   
54 * Basic org-babel functionality
55   :PROPERTIES:
56   :CUSTOM_ID: basic-functionality
57   :END:
58 *** Source code execution
59     :PROPERTIES:
60     :CUSTOM_ID: source-code-execution
61     :END:
62     For interpreted languages such as shell, python, R, etc, org-babel
63     allows source blocks to be executed: the code is passed to the
64     interpreter and you have control over what is done with the
65     results of excecution. E.g. place point anywhere in the following
66     block and use C-c C-c to run the code:
68 [[http://www.ruby-lang.org/][Ruby]] source code
69 #+begin_src ruby
70 "This file was last evaluated on #{Date.today}"
71 #+end_src
73 Results of Ruby evaluation
74 #+resname:
75 : This file was last evaluated on 2009-08-09
77 [[http://www.r-project.org/][R]] source code
78 #+begin_src R :results value
79 x = 4
80 date()
81 c(5, 10)
82 #+end_src
84 Results of R evaluation
85 #+resname:
86 |  5 |
87 | 10 |
89 *** What happens to the results?
90     :PROPERTIES:
91     :CUSTOM_ID: results
92     :END:
93     Org-babel provides two fundamentally different modes for capturing
94     the results of code evaluation, specified by the :results header
95     argument:
96 **** :results value
97      This means that the 'result' of code evaluation is defined to be
98      the *value* of the last statement in the block. Thus with this
99      setting, one can view the code block as a function with a return
100      value. And not only can one view it that way, but you can
101      actually use the return value of one source block as input for
102      another (see later). This setting is the default.
103 **** :results output
104      With this setting, org-babel captures all the text output of the
105      code block and places it in the org buffer. One can think of this
106      as a 'scripting' mode: the code block contains a series of
107      commands, and you get the output of all the commands. Unlike in
108      the 'functional' mode specified by =:results value=, the code
109      block has no return value. (This mode will be familiar to Sweave
110      users).
111 **** Additional :results settings
112      
113 *** Arguments to source code blocks
114     :PROPERTIES:
115     :CUSTOM_ID: arguments-to-source-code-blocks
116     :END:
117     In addition to evaluation of code blocks, org-babel allows them to
118     be parameterised (i.e. have arguments). Thus source code blocks
119     now have the status of *functions*.
121 Inputs for fibonacci-seq
123 #+tblname: fibonacci-inputs
124 | 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 |
125 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
127 in the Org-mode buffer this looks like
128 : #+tblname: fibonacci-inputs
129 : | 1 | 2 | 3 | 4 |  5 |  6 |  7 |  8 |  9 | 10 |
130 : | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
132 [[http://www.gnu.org/software/emacs/manual/elisp.html][Emacs Lisp]] source code
133 #+srcname: fibonacci-seq
134 #+begin_src emacs-lisp :var fib-inputs=fibonacci-inputs
135   (defun fibonacci (n)
136     (if (or (= n 0) (= n 1))
137         n
138       (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
139   
140   (mapcar (lambda (row)
141             (mapcar #'fibonacci row)) fib-inputs)
142 #+end_src
144 in the Org-mode buffer this looks like
145 : #+srcname: fibonacci-seq
146 : #+begin_src emacs-lisp :var fib-inputs=fibonacci-inputs
147 :   (defun fibonacci (n)
148 :     (if (or (= n 0) (= n 1))
149 :         n
150 :       (+ (fibonacci (- n 1)) (fibonacci (- n 2)))))
151 :   
152 :   (mapcar (lambda (row)
153 :             (mapcar #'fibonacci row)) fib-inputs)
154 : #+end_src
156 Results of Emacs Lisp code evaluation
157 #+resname:
158 | 1 | 1 | 2 |  3 |  5 |   8 |  13 |  21 |   34 |   55 |
159 | 1 | 3 | 8 | 21 | 55 | 144 | 377 | 987 | 2584 | 6765 |
161 * A meta-programming language for org-mode
162   :PROPERTIES:
163   :CUSTOM_ID: meta-programming-language
164   :END:
166 Since information can pass freely between source-code blocks and
167 org-mode tables you can mix and match languages using each language
168 for those tasks to which it is suited.  This makes Org-mode files with
169 Org-babel into a kind of meta-functional programming language in which
170 functions from many languages can work together.
172 As an example, lets take some system diagnostics in the shell, and
173 then graph them with R.
175 1. Shell source code
176 #+srcname: directories
177    #+begin_src bash :results replace
178    cd ~ && du -sc * |grep -v total
179    #+end_src
180 2. Results of the shell source code (on my system, grab this org-mode
181    files and try running it on your own)
182 #+resname: directories
183 |       72 | "Desktop"   |
184 | 12156104 | "Documents" |
185 |  3482440 | "Downloads" |
186 |  2901720 | "Library"   |
187 |    57344 | "Movies"    |
188 | 16548024 | "Music"     |
189 |      120 | "News"      |
190 |  7649472 | "Pictures"  |
191 |        0 | "Public"    |
192 |   152224 | "Sites"     |
193 |        8 | "System"    |
194 |       56 | "bin"       |
195 |  3821872 | "mail"      |
196 | 10605392 | "src"       |
197 |     1264 | "tools"     |
198 3. R source code (which calls the previous shell source code)
199 #+srcname: directory-pie
200    #+begin_src R :var dirs = directories :session R-pie-example
201    pie(dirs[,1], labels = dirs[,2])
202    #+end_src
203 4. Results of R code [[file:images/dirs.png]]
205 * Spreadsheet plugins for org-mode in any language
206   :PROPERTIES:
207   :CUSTOM_ID: spreadsheet
208   :END:
210 *NOTE*: Maybe in-addition-to/in-stead-of this example we should do a
211 more traditional "spreadsheet" example with R [Eric]
213 Not only can Org-babel pass entire tables of data to source code
214 blocks (see [[arguments-to-source-code-blocks]]), Org-babel can also be
215 used to call source code blocks from *within* tables using the
216 Org-mode's [[http://orgmode.org/manual/The-spreadsheet.html#The-spreadsheet][existing spreadsheet functionality]].
218 In fact the functional test suite for Org-babel is implemented as a
219 large Org-mode table.  To run the entire test suite you simple
220 evaluate the table =C-u C-c C-c=, and all of the tests are run
221 updating the table with pass/fail statistics.
223 Here's a sample of our test suite.
225 #+TBLNAME: org-babel-tests
226 | functionality    | block        | arg |    expected |     results | pass |
227 |------------------+--------------+-----+-------------+-------------+------|
228 | basic evaluation |              |     |             |             | pass |
229 |------------------+--------------+-----+-------------+-------------+------|
230 | emacs lisp       | basic-elisp  |   2 |           4 |           4 | pass |
231 | shell            | basic-shell  |     |           6 |           6 | pass |
232 | ruby             | basic-ruby   |     |   org-babel |   org-babel | pass |
233 | python           | basic-python |     | hello world | hello world | pass |
234 | R                | basic-R      |     |          13 |          13 | pass |
235 #+TBLFM: $5='(if (= (length $3) 1) (progn (message (format "running %S" '(sbe $2 (n $3)))) (sbe $2 (n $3))) (sbe $2))::$6='(if (string= $4 $5) "pass" (format "expected %S but was %S" $4 $5))
236 #+TBLFM: $5=""::$6=""
238 *** code blocks for tests
240 #+srcname: basic-elisp
241 #+begin_src emacs-lisp :var n=7
242 (* 2 n)
243 #+end_src
245 #+srcname: basic-shell
246 #+begin_src sh :results silent
247 expr 1 + 5
248 #+end_src
250 #+srcname: date-simple
251 #+begin_src sh :results silent
252 date
253 #+end_src
255 #+srcname: basic-ruby
256 #+begin_src ruby :results silent
257 "org-babel"
258 #+end_src
260 #+srcname: basic-python
261 #+begin_src python :results silent
262 'hello world'
263 #+end_src
265 #+srcname: basic-R
266 #+begin_src R :results silent
267 b <- 9
268 b + 4
269 #+end_src
271 * Library of Babel
272   :PROPERTIES:
273   :CUSTOM_ID: library-of-babel
274   :END:
275   What about those source code blocks which are so useful you want to
276   have them available in every org-mode buffer?
278   The [[file:library-of-babel.org][Library of Babel]] is an extensible collection of ready-made and
279   easily-shortcut-callable source-code blocks for handling common
280   tasks.  Org-babel comes pre-populated with the source-code blocks
281   located in the [[file:library-of-babel.org][library-of-babel.org]] file. It is possible to add
282   source-code blocks from any org-mode file to the library by calling
284 #+srcname: add-file-to-lob
285 #+begin_src emacs-lisp 
286 (org-babel-lob-ingest "path/to/file.org")
287 #+end_src
289 * Reproducible Research
290   :PROPERTIES:
291   :CUSTOM_ID: reproducable-research
292   :END:
293 #+begin_quote 
294 An article about computational science in a scientific publication is
295 not the scholarship itself, it is merely advertising of the
296 scholarship. The actual scholarship is the complete software
297 development environment and the complete set of instructions which
298 generated the figures. -- D. Donoho
299 #+end_quote
301 [[http://reproducibleresearch.net/index.php/Main_Page][Reproducible Research]] (RR) is the practice of distributing along with
302 an article of research all data, code, and tools required to reproduce
303 the results discussed in the paper.  As such the paper becomes not
304 only a document describing the research but a complete laboratory
305 reproducing the research.
307 Org-mode already has exceptional support for [[http://orgmode.org/manual/Exporting.html#Exporting][exporting to html and
308 LaTeX]].  Org-babel makes Org-mode a tool for RR by *activating* the
309 data and source code embedded into Org-mode documents making the
310 entire document executable.  This makes it not only possible, but
311 natural to distribute research in a format that encourages readers to
312 recreate your results, and perform their own analysis.
314 Existing RR tools like [[http://en.wikipedia.org/wiki/Sweave][Sweave]] provide for the embedding of [[http://www.r-project.org/][R]] code into
315 LaTeX documents.  While this is very useful, such documents often
316 still require a large degree of "glue code" in the form of external
317 shell scripts, python scripts, and Makefiles.  To our knowledge
318 Org-babel is the only RR tool which allows multiple languages and data
319 to coexist and cooperate inside of a single document.
321 * Literate programming
322   :PROPERTIES:
323   :CUSTOM_ID: literate-programming
324   :END:
325   - org-babel-tangle
326   - org-babel-load-file
327 * Reference / Documentation
328   :PROPERTIES:
329   :CUSTOM_ID: reference-and-documentation
330   :END:
332 *** Source Code block syntax
334 The basic syntax of source-code blocks is as follows:
336 : #+srcname: name
337 : #+begin_src language header-arguments
338 : body
339 : #+end_src
341 - name :: This name is associated with the source-code block.  This is
342      similar to the =#+TBLNAME= lines which can be used to name tables
343      in org-mode files.  By referencing the srcname of a source-code
344      block it is possible to evaluate the block for other places,
345      files, or from inside tables.
346 - language :: The language of the code in the source-code block, valid
347      values must be members of `org-babel-interpreters'.
348 - header-arguments :: Header arguments control many facets of the
349      input to, evaluation of, and output of source-code blocks.  See
350      the [[* Header Arguments][Header Arguments]] section for a complete review of available
351      header arguments.
352 - body :: The actual source code which will be evaluated.  This can be
353           edited with `org-edit-special'.
355 **** Header Arguments
357 - results :: results arguments specify what should be done with the
358              output of source-code blocks
359   - The following options are mutually exclusive, and specify how the
360     results should be collected from the source-code block
361     - value ::
362     - output :: 
363   - The following options are mutually exclusive and specify what type
364     of results the code block will return
365     - vector :: specifies that the results should be interpreted as a
366                 multidimensional vector (even if the vector is
367                 trivial), and will be inserted into the org-mode file
368                 as a table
369     - scalar :: specifies that the results should be interpreted as a
370                 scalar value, and will be inserted into the org-mode
371                 file as quoted text
372     - file :: specifies that the results should be interpreted as the
373               path to a file, and will be inserted into the org-mode
374               file as a link
375   - The following options specify how the results should be inserted
376     into the org-mode file
377     - replace :: the current results replace any previously inserted
378                  results from the code block
379     - silent :: rather than being inserted into the org-mode file the
380                 results are echoed into the message bar
381 - exports :: exports arguments specify what should be included in html
382              or latex exports of the org-mode file
383   - code :: the body of code is included into the exported file
384   - results :: the results of evaluating the code is included in the
385                exported file
386   - both :: both the code and results are included in the exported
387             file
388   - none :: nothing is included in the exported file
389 - tangle :: tangle arguments specify whether or not the source-code
390             block should be included in tangled extraction of
391             source-code files
392   - yes :: the source-code block is exported to a source-code file
393            named after the basename (name w/o extension) of the
394            org-mode file
395   - no :: (default) the source-code block is not exported to a
396           source-code file
397   - other :: any other string passed to the =tangle= header argument
398              is interpreted as a file basename to which the block will
399              be exported
402