15.6 Results of Evaluation
How Org handles results of a code block execution depends on many header arguments working together. The primary determinant, however, is the βresults
β header argument. It accepts four classes of options. Each code block can take only one option per class:
Collectionβ
For how the results should be collected from the code block;
Typeβ
For which type of result the code block will return; affects how Org processes and inserts results in the Org buffer;
Formatβ
For the result; affects how Org processes results;
Handlingβ
For inserting results once they are properly formatted.
Collectionβ
Collection options specify the results. Choose one of the options; they are mutually exclusive.
βvalue
ββ
Default for most Babel libraries1. Functional mode. Org gets the value by wrapping the code in a function definition in the language of the source block. That is why when using β:results value
β, code should execute like a function and return a value. For languages like Python, an explicit return
statement is mandatory when using β:results value
β. Result is the value returned by the last statement in the code block.
When evaluating the code block in a session (see Environment of a Code Block), Org passes the code to an interpreter running as an interactive Emacs inferior process. Org gets the value from the source code interpreterβs last statement output. Org has to use language-specific methods to obtain the value. For example, from the variable _
in Ruby, and the value of .Last.value
in R.
βoutput
ββ
Scripting mode. Org passes the code to an external process running the interpreter. Org returns the contents of the standard output stream as text results.
When using a session, Org passes the code to the interpreter running as an interactive Emacs inferior process. Org concatenates any text output from the interpreter and returns the collection as a result.
Typeβ
Type tells what result types to expect from the execution of the code block. Choose one of the options; they are mutually exclusive. The default behavior is to automatically determine the result type.
βtable
ββ
βvector
ββ
Interpret the results as an Org table. If the result is a single value, create a table with one row and one column. Usage example: β:results value table
β.
In-between each table row or below the table headings, sometimes results have horizontal lines, which are also known as βhlines". The βhlines
β argument with the default βno
β value strips such lines from the input table. For most code, this is desirable, or else those βhline
β symbols raise unbound variable errors. A βyes
β accepts such lines, as demonstrated in the following example.
#+NAME: many-cols
| a | b | c |
|---+---+---|
| d | e | f |
|---+---+---|
| g | h | i |
#+NAME: no-hline
#+BEGIN_SRC python :var tab=many-cols :hlines no
return tab
#+END_SRC
#+RESULTS: no-hline
| a | b | c |
| d | e | f |
| g | h | i |
#+NAME: hlines
#+BEGIN_SRC python :var tab=many-cols :hlines yes
return tab
#+END_SRC
#+RESULTS: hlines
| a | b | c |
|---+---+---|
| d | e | f |
|---+---+---|
| g | h | i |
βlist
ββ
Interpret the results as an Org list. If the result is a single value, create a list of one element.
βscalar
ββ
βverbatim
ββ
Interpret literally and insert as quoted text. Do not create a table. Usage example: β:results value verbatim
β.
βfile
ββ
Interpret as a filename. Save the results of execution of the code block to that file, then insert a link to it. You can control both the filename and the description associated to the link.
Org first tries to generate the filename from the value of the βfile
β header argument and the directory specified using the βoutput-dir
β header arguments. If βoutput-dir
β is not specified, Org assumes it is the current directory.
#+BEGIN_SRC asymptote :results value file :file circle.pdf :output-dir img/
size(2cm);
draw(unitcircle);
#+END_SRC
If βfile
β header argument is missing, Org generates the base name of the output file from the name of the code block, and its extension from the βfile-ext
β header argument. In that case, both the name and the extension are mandatory.
#+name: circle
#+BEGIN_SRC asymptote :results value file :file-ext pdf
size(2cm);
draw(unitcircle);
#+END_SRC
The βfile-desc
β header argument defines the description (see Link Format) for the link. If βfile-desc
β is present but has no value, the βfile
β value is used as the link description. When this argument is not present, the description is omitted.
By default, Org assumes that a table written to a file has TAB-delimited output. You can choose a different separator with the βsep
β header argument.
The βfile-mode
β header argument defines the file permissions. To make it executable, use β:file-mode (identity #o755)
β.
#+BEGIN_SRC shell :results file :file script.sh :file-mode (identity #o755)
echo "#!/bin/bash"
echo "echo Hello World"
#+END_SRC
Formatβ
Format pertains to the type of the result returned by the code block. Choose one of the options; they are mutually exclusive. The default follows from the type specified above.
βcode
ββ
Result enclosed in a code block. Useful for parsing. Usage example: β:results value code
β.
βdrawer
ββ
Result wrapped in a βRESULTS
β drawer. Useful for containing βraw
β or βorg
β results for later scripting and automated processing. Usage example: β:results value drawer
β.
βhtml
ββ
Results enclosed in a βBEGIN_EXPORT html
β block. Usage example: β:results value html
β.
βlatex
ββ
Results enclosed in a βBEGIN_EXPORT latex
β block. Usage example: β:results value latex
β.
βlink
ββ
βgraphics
ββ
When used along with βfile
β type, the result is a link to the file specified in β:file
β header argument. However, unlike plain βfile
β type, nothing is written to the disk. The block is used for its side-effects only, as in the following example:
#+begin_src shell :results file link :file "download.tar.gz"
wget -c "http://example.com/download.tar.gz"
#+end_src
βorg
ββ
Results enclosed in a βBEGIN_SRC org
β block. For comma-escape, either TAB
in the block, or export the file. Usage example: β:results value org
β.
βpp
ββ
Result converted to pretty-print source code. Enclosed in a code block. Languages supported: Emacs Lisp, Python, and Ruby. Usage example: β:results value pp
β.
βraw
ββ
Interpreted as raw Org mode. Inserted directly into the buffer. Aligned if it is a table. Usage example: β:results value raw
β.
The βwrap
β header argument unconditionally marks the results block by appending strings to β#+BEGIN_
β and β#+END_
β. If no string is specified, Org wraps the results in a β#+BEGIN_results
β β¦ β#+END_results
β block. It takes precedent over the βresults
β value listed above. E.g.,
#+BEGIN_SRC emacs-lisp :results html :wrap EXPORT markdown
"<blink>Welcome back to the 90's</blink>"
#+END_SRC
#+RESULTS:
#+BEGIN_EXPORT markdown
<blink>Welcome back to the 90's</blink>
#+END_EXPORT
Handlingβ
Handling options after collecting the results.
βsilent
ββ
Do not insert results in the Org mode buffer, but echo them in the minibuffer. Usage example: β:results output silent
β.
βreplace
ββ
Default. Insert results in the Org buffer. Remove previous results. Usage example: β:results output replace
β.
βappend
ββ
Append results to the Org buffer. Latest results are at the bottom. Does not remove previous results. Usage example: β:results output append
β.
βprepend
ββ
Prepend results to the Org buffer. Latest results are at the top. Does not remove previous results. Usage example: β:results output prepend
β.
Post-processingβ
The βpost
β header argument is for post-processing results from block evaluation. When βpost
β has any value, Org binds the results to *this*
variable for easy passing to βvar
β header argument specifications (see Environment of a Code Block). That makes results available to other code blocks, or even for direct Emacs Lisp code execution.
The following two examples illustrate βpost
β header argument in action. The first one shows how to attach an βATTR_LATEX
β keyword using βpost
β.
#+NAME: attr_wrap
#+BEGIN_SRC sh :var data="" :var width="\\textwidth" :results output
echo "#+ATTR_LATEX: :width $width"
echo "$data"
#+END_SRC
#+HEADER: :file /tmp/it.png
#+BEGIN_SRC dot :post attr_wrap(width="5cm", data=*this*) :results drawer
digraph{
a -> b;
b -> c;
c -> a;
}
#+end_src
#+RESULTS:
:RESULTS:
#+ATTR_LATEX :width 5cm
[[file:/tmp/it.png]]
:END:
The second example shows use of βcolnames
β header argument in βpost
β to pass data between code blocks.
#+NAME: round-tbl
#+BEGIN_SRC emacs-lisp :var tbl="" fmt="%.3f"
(mapcar (lambda (row)
(mapcar (lambda (cell)
(if (numberp cell)
(format fmt cell)
cell))
row))
tbl)
#+end_src
#+BEGIN_SRC R :colnames yes :post round-tbl[:colnames yes](*this*)
set.seed(42)
data.frame(foo=rnorm(1))
#+END_SRC
#+RESULTS:
| foo |
|-------|
| 1.371 |
- Actually, the constructs β
call_<name>()
β and βsrc_<lang>{}
β are not evaluated when they appear in a keyword (see In-buffer Settings).β©