org: Results of evaluation
14.9 Results of evaluation
==========================
The way in which results are handled depends on whether a session is
invoked, as well as on whether ‘:results value’ or ‘:results output’ is
used. The following table shows the table possibilities. For a full
listing of the possible results header arguments see results.
Non-session Session
‘:results value’ value of last value of last expression
expression
‘:results output’ contents of STDOUT concatenation of interpreter
output
Note: With ‘:results value’, the result in both ‘:session’ and
non-session is returned to Org mode as a table (a one- or
two-dimensional vector of strings or numbers) when appropriate.
14.9.1 Non-session
------------------
14.9.1.1 ‘:results value’
.........................
This is the default. Internally, the value is obtained by wrapping the
code in a function definition in the external language, and evaluating
that function. Therefore, code should be written as if it were the body
of such a function. In particular, note that Python does not
automatically return a value from a function unless a ‘return’ statement
is present, and so a ‘return’ statement will usually be required in
Python.
This is the only one of the four evaluation contexts in which the
code is automatically wrapped in a function definition.
14.9.1.2 ‘:results output’
..........................
The code is passed to the interpreter as an external process, and the
contents of the standard output stream are returned as text. (In
certain languages this also contains the error output stream; this is an
area for future work.)
14.9.2 Session
--------------
14.9.2.1 ‘:results value’
.........................
The code is passed to an interpreter running as an interactive Emacs
inferior process. Only languages which provide tools for interactive
evaluation of code have session support, so some language (e.g., C and
ditaa) do not support the ‘:session’ header argument, and in other
languages (e.g., Python and Haskell) which have limitations on the code
which may be entered into interactive sessions, those limitations apply
to the code in code blocks using the ‘:session’ header argument as well.
Unless the ‘:results output’ option is supplied (see below) the
result returned is the result of the last evaluation performed by the
interpreter. (This is obtained in a language-specific manner: the value
of the variable ‘_’ in Python and Ruby, and the value of ‘.Last.value’
in R).
14.9.2.2 ‘:results output’
..........................
The code is passed to the interpreter running as an interactive Emacs
inferior process. The result returned is the concatenation of the
sequence of (text) output from the interactive interpreter. Notice that
this is not necessarily the same as what would be sent to ‘STDOUT’ if
the same code were passed to a non-interactive interpreter running as an
external process. For example, compare the following two blocks:
#+BEGIN_SRC python :results output
print "hello"
2
print "bye"
#+END_SRC
#+RESULTS:
: hello
: bye
In non-session mode, the “2” is not printed and does not appear.
#+BEGIN_SRC python :results output :session
print "hello"
2
print "bye"
#+END_SRC
#+RESULTS:
: hello
: 2
: bye
But in ‘:session’ mode, the interactive interpreter receives input
“2” and prints out its value, “2”. (Indeed, the other print statements
are unnecessary here).