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)
6 #+AUTHOR: Dan Davison, Eric Schulte
7 #+EMAIL: davison at stats dot ox dot ac dot uk
13 <p>executable source code blocks in org-mode</p>
17 <img src="images/tower-of-babel.png" alt="images/tower-of-babel.png" />
20 <a href="http://www.flickr.com/photos/23379658@N05/" title=""><b>Martijn Streefkerk</b></a>
27 Org-babel provides the following modifications to [[http://orgmode.org/manual/Literal-examples.html][the existing
28 support]] for blocks of source code examples in the org-mode core.
29 1. source code execution
30 2. arguments to source code blocks
31 3. exportation of source code blocks to files (literate programming)
34 Grab the latest code from the git repo at [[http://github.com/eschulte/org-babel/tree/master][github/org-babel]]
36 git clone git://github.com/eschulte/org-babel.git
39 And add the following lines to your .emacs, replacing the path as
40 appropriate. A good place to check that things are up and running
41 would the examples in [[* Basic org-babel functionality][Basic org-babel functionality]].
42 #+begin_src emacs-lisp
43 (add-to-list 'load-path "/path/to/org-babel/lisp")
44 (require 'org-babel-init)
47 * Basic org-babel functionality
48 *** Source code execution
49 For interpreted languages such as shell, python, R, etc, org-babel
50 allows source blocks to be executed: the code is passed to the
51 interpreter and you have control over what is done with the
52 results of excecution. E.g. place point anywhere in the following
53 block and use C-c C-c to run the code:
55 #+begin_src python :results output
67 #+begin_src R :results value
77 *** What happens to the results?
78 Org-babel provides two fundamentally different modes for capturing
79 the results of code evaluation, specified by the :results header
82 This means that the 'result' of code evaluation is defined to be
83 the *value* of the last statement in the block. Thus with this
84 setting, one can view the code block as a function with a return
85 value. And not only can one view it that way, but you can
86 actually use the return value of one source block as input for
87 another (see later). This setting is the default.
89 With this setting, org-babel captures all the text output of the
90 code block and places it in the org buffer. One can think of this
91 as a 'scripting' mode: the code block contains a series of
92 commands, and you get the output of all the commands. Unlike in
93 the 'functional' mode specified by =:results value=, the code
94 block has no return value. (This mode will be familiar to Sweave
96 **** Additional :results settings
98 *** Arguments to source code blocks
99 In addition to evaluation of code blocks, org-babel allows them to
100 be parameterised (i.e. have arguments). Thus source code blocks
101 now have the status of *functions*.
103 * A meta-programming language for org-mode
104 * Spreadsheet plugins for org-mode in any language
106 What about those source code blocks which are so useful you want to
107 have them available in every org-mode buffer?
109 The [[file:library-of-babel.org][Library of Babel]] is an extensible collection of ready-made and
110 easily-shortcut-callable source-code blocks for handling common
111 tasks. Org-babel comes pre-populated with the source-code blocks
112 located in the [[file:library-of-babel.org][library-of-babel.org]] file. It is possible to add
113 source-code blocks from any org-mode file to the library by calling
115 #+srcname: add-file-to-lob
116 #+begin_src emacs-lisp
117 (org-babel-lob-ingest "path/to/file.org")
120 * Reproducible research
121 - output vs. value mode
122 - file & graphical output
124 * Literate programming
126 - org-babel-load-file
127 * Reference / Documentation
129 *** Source Code block syntax
131 The basic syntax of source-code blocks is as follows:
134 : #+begin_src language header-arguments
138 - name :: This name is associated with the source-code block. This is
139 similar to the =#+TBLNAME= lines which can be used to name tables
140 in org-mode files. By referencing the srcname of a source-code
141 block it is possible to evaluate the block for other places,
142 files, or from inside tables.
143 - language :: The language of the code in the source-code block, valid
144 values must be members of `org-babel-interpreters'.
145 - header-arguments :: Header arguments control many facets of the
146 input to, evaluation of, and output of source-code blocks. See
147 the [[* Header Arguments][Header Arguments]] section for a complete review of available
149 - body :: The actual source code which will be evaluated. This can be
150 edited with `org-edit-special'.
152 **** Header Arguments
154 - results :: results arguments specify what should be done with the
155 output of source-code blocks
156 - The following options are mutually exclusive, and specify how the
157 results should be collected from the source-code block
160 - The following options are mutually exclusive and specify what type
161 of results the code block will return
162 - vector :: specifies that the results should be interpreted as a
163 multidimensional vector (even if the vector is
164 trivial), and will be inserted into the org-mode file
166 - scalar :: specifies that the results should be interpreted as a
167 scalar value, and will be inserted into the org-mode
169 - file :: specifies that the results should be interpreted as the
170 path to a file, and will be inserted into the org-mode
172 - The following options specify how the results should be inserted
173 into the org-mode file
174 - replace :: the current results replace any previously inserted
175 results from the code block
176 - silent :: rather than being inserted into the org-mode file the
177 results are echoed into the message bar
178 - exports :: exports arguments specify what should be included in html
179 or latex exports of the org-mode file
180 - code :: the body of code is included into the exported file
181 - results :: the results of evaluating the code is included in the
183 - both :: both the code and results are included in the exported
185 - none :: nothing is included in the exported file
186 - tangle :: tangle arguments specify whether or not the source-code
187 block should be included in tangled extraction of
189 - yes :: the source-code block is exported to a source-code file
190 named after the basename (name w/o extension) of the
192 - no :: (default) the source-code block is not exported to a
194 - other :: any other string passed to the =tangle= header argument
195 is interpreted as a file basename to which the block will