6

I have an org-mode document with a number of code blocks. Some of these take a long time to run, so I don't want to re-evaluate them each time I do an export.

I know I can prevent the re-evaluation by setting org-export-babel-evaluate to nil, but when I do this it won't print out the results of previous evaluations either, even if I have the code block "results" option set to "output".

Yes, I know I can remove the "#+results:" line from before the results block by hand, but this gets annoying whenever I need to reevaluate by hand, for example when I make a code change.

So, how can I get it to export the old results rather than re-evaluating each time?

3

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  
  • I tried setting :exports results on some emacs-lisp code, and it still evaluated it on export... I'm not sure what exactly the difference is, so just noting that there may be more to this. (Also: this affects what output happens, which may or may not be desirable.) – lindes Feb 24 '18 at 7:16
  • 2
    I don't understand why this answer is accepted. Method described in the answer does not solve the issue. OTOH @lindes's answer seems to solve it. – muhuk Mar 17 at 0:45
  • Because at the time (7 years ago), Org-Mode 9 was not out (Based on git tags latest release would have been 7.8.03). At the time the export options were not what they are today (or what they were in the other answers). With the changes that have been made to Org over the years the accepted answer should change but that depends on the asker. – Jonathan Leech-Pepin Mar 20 at 13:10
1

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.

1

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. :-/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.