Short answer: use
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.
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:
| 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")
(list sum avg)))
| Total Score | Average Score |
| 93 | 23 |
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. :-/