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).β©