In emacs org-babel, can I export the results of previous executions instead of re-evaluating on export?

With org-mode 9, you can prevent the execution of a source block during export with :eval no-export.

From the documentation:

never-export or no-export

Org will not evaluate this ‘src’ code block when exporting, yet the user can evaluate this source block interactively.


You want to set your header argument to include :exports both or :exports results.

For example (The differences in time are due to evaluation when creating the headlines with C-c C-c, I left them as such to make sure there was no further evaluation):

* Testing - results
#+name: test
#+begin_src sh :exports results
  uptime
#+end_src

#+RESULTS:
| 15:49:23 up  2:00 | 2 users | load average: 0.00 | 0.0 | 0.0 |
* Testing - both
#+name: test
#+begin_src sh :exports both
  uptime
#+end_src

#+RESULTS:
| 15:50:02 up  2:01 | 2 users | load average: 0.00 | 0.0 | 0.0 |

Produces the following ASCII C-c C-e A

1 Testing - results 
--------------------


  15:49:23 up  2:00   2 users   load average: 0.00   0.0   0.0  

2 Testing - both 
-----------------


  uptime

  15:50:02 up  2:01   2 users   load average: 0.00   0.0   0.0  

Short answer: use :cache yes

Some additional detail:

Org-mode has (perhaps this is new since the question was asked, but there now exists) a :cache option. The gist is that you set :cache yes on the #+BEGIN_SRC line, and then:

  • When you first run the code (even if results were stored previously, it will have to re-run it at least once, the first time you enable :cache), it will cache the results of the output as usual, and also store a SHA1 hash of the state of the source code (and any data inputs, e.g. referenced lists or tables) that generated it.
  • Then, when you do an operation that would otherwise evaluate the code (whether org-babel-execute-src-block (e.g. via C-c C-c), or one of the export commands, it will recalculate the checksum on the source block and any data inputs, and only if they've changed will it run the code again.

If you have code that has input not explicitly listed in the file (e.g. it uses system time or content of a file or something) that might change the results, you'll have to disable caching (or make some other change) to get the code to run again. But if the data is all self-contained within the org file, it should generally cache when it's supposed to, and re-generate when it has a reason to.

Example:

The following is a contrived example where you have a table of scores that people have achieved in some way, and you wish to do a summary of the data.

This code will run when either the table changes or the code changes (or both), but as long as both stay the same, the checksum won't change and therefore the code won't be run again. Obviously not a huge deal in this particular example, but for code like you mention that takes a long time to run, it could be.

Example org-mode snippet with data table, source code, and results:

** Participants:

    #+NAME: mytable
    | Name  | Points |
    |-------+--------|
    | Alice |     30 |
    | Bob   |     20 |
    | Carol |     25 |
    | Dan   |     18 |

** Summary data:

    #+BEGIN_SRC emacs-lisp :cache yes :var data=mytable :exports results
      (let* ((scores (mapcar (lambda (row) (cadr row)) data))
             (sum (apply '+ scores))
             (avg (/ sum (length scores))))
        (list '("Total Score" "Average Score")
              'hline
              (list sum avg)))
    #+END_SRC

    #+RESULTS[a15af1d9cd34a76f4a6f822c36eccbc8bd493195]:
    | Total Score | Average Score |
    |-------------+---------------|
    |          93 |            23 |

side-note:

I've discovered that doing, for example, org-html-export-to-html seems to re-calculate the values for things when needed, but NOT store them in the RESULTS sections. So, something to be aware of... may want to compute these just with C-c C-c, and export only after that's been done. :-/

Tags:

Emacs

Org Mode