Using results from one code block in another org-mode
Posted February 12, 2019 at 09:20 AM | categories: elisp, orgmode, emacs | tags:
Updated February 25, 2019 at 02:17 PM
Table of Contents
One really great feature in org-mode is you have many options to pass data between code-blocks. In this post we look at some of these options using emacs-lisp as the language. This runs in a session where you can keep variables in memory between blocks, and use them in subsequent blocks.
Here we set a variable to a value.
(setq some-variable 42)
42
Then later in another block we can use that variable:
(+ some-variable 1)
43
While you are in the session, some-variable
can be used. If you want some mind-bending trouble, the emacs-lisp session is global, and you can access some-variable
even in another buffer! Don't do that. When you close emacs this variable will disappear, and all that is left are the results from above.
There is another way to pass information from one block to another using named src blocks and variables in the block header. This allows you to pass data between blocks by name, and you will see later you can even access the results by name from other files.
1 :var
First, we give our src block a name like this:
#+name: block-1 #+BEGIN_SRC emacs-lisp (current-time-string) #+END_SRC
When we run this, the results will have a name too.
(current-time-string)
Tue Feb 12 08:19:23 2019
Now, we can use the named result as input to a new block using the :var header.
#+BEGIN_SRC emacs-lisp :var input=block-1 (format "We got %S in block-1" input) #+END_SRC
When we run this block, emacs will run block-1 and put the output in to the variable input
which we use inside the code block.
(format "We got %S in block-1" input)
We got "Tue Feb 12 08:20:44 2019" in block-1
Some things to note:
- Every time you run this, block-1 gets rerun.
- The results in this block are not the same as in block-1
- The results in block-1 are not changed when you run the second block.
You may not want to rerun block-1 each time; maybe it is an expensive calculation, or maybe it should not be changed. You can prevent this behavior by using the :cache header.
2 :cache
If you specify :cache yes
then org-mode should store a hash of the code block with the results, and if the code block hasn't changed then it should not run again.
#+name: block-2 #+BEGIN_SRC emacs-lisp :cache yes (current-time-string) #+END_SRC
(current-time-string)
Tue Feb 12 08:06:22 2019
Now, we use block-2 as input to a block, we see the output is the same as the output from block-2.
(format "We got %S in block-2" input)
We got "Tue Feb 12 08:06:22 2019" in block-2
Ok, but what if my results are too large to put in the buffer, or too complex for text? You still have some options.
3 :wrap
Suppose we generate some json in one block, and we want to use it in another block. We still want to see the json in the buffer as an intermediate result. We can wrap the output in a json block like this.
(require 'json) (json-encode `(("date" . ,(current-time-string))))
{"date":"Tue Feb 12 08:30:20 2019"}
Then, we can simply input that output into a new block.
(format "We got %S in json" input)
We got "{\"date\":\"Tue Feb 12 08:30:20 2019\"} " in json
This admittedly still pretty simple, text-based data. It is probably not a good idea to do this with binary data.
Note you can refer to this result even in another org-file:
#+BEGIN_SRC emacs-lisp :var input=./2019-02-12.org:json input #+END_SRC #+RESULTS: : {"date":"Tue Feb 12 08:30:20 2019"}
4 :file
It may be that your data is too large to conveniently put into your org-file, or maybe it is binary data. No problem, just put it into an external file using the :file header. It looks like this:
#+name: block-3 #+BEGIN_SRC emacs-lisp :cache yes :file block-3 (require 'json) (json-encode `(("date" . ,(current-time-string)))) #+END_SRC #+RESULTS[a14d376653bd8c40a0961ca95f21d8837dddec66]: block-3 [[file:block-3]]
Note that you have to provide a file name for this. Sometimes that is nice if you want a human recognizable file to send to someone, but it would also be nice if there was an automatic naming scheme, e.g. based on an sha-1 hash of the src block.
(require 'json) (json-encode `(("date" . ,(current-time-string))))
Now you can use other tools to check out the file. Here we can still use simple shell tools.
cat block-3
The output of block-3 is a file name:
input
/Users/jkitchin/Box Sync/kitchingroup/jkitchin/journal/2019/02/12/block-3
So you can use it in a new block to read the data in, and then do something new with it.
(with-temp-buffer (insert-file-contents input) (format "We got %S in block-3" (json-read-from-string (buffer-string))))
We got ((date . "Tue Feb 12 08:46:55 2019")) in block-3
5 "remote" data
The blocks do not have to be in order. If you want, you can put your blocks in an appendix, and then just have analysis blocks here that use them. That way, you can have short blocks here that are more readable, but longer, more complex blocks elsewhere that do not clutter your document.
(with-temp-buffer (insert-file-contents input) (format "We got %S in the appendix data" (json-read-from-string (buffer-string))))
We got "{\"date\":\"Tue Feb 12 09:11:12 2019\"}" in the appendix data
6 Manually saving data in files
Note you can also manually save data in a file, for example:
(require 'json) (let ((f "block-4.json")) (with-temp-file f (prin1 (json-encode `(("date" . ,(current-time-string)))) (current-buffer))) f)
block-4.json
We put the filename as the last variable which is returned by the block, so that we don't have to manually type it later in the next block. You know, try not to repeat yourself…
This just shows we did write out to our file:
cat block-4.json
And we read the file in here, using the filename from block-4 as an input variable.
(with-temp-buffer (insert-file-contents input) (format "We got %S in block-4" (json-read-from-string (buffer-string))))
We got "{\"date\":\"Tue Feb 12 08:51:25 2019\"}" in block-4
7 An appendix for data
(require 'json) (let ((f "appendix.json")) (with-temp-file f (prin1 (json-encode `(("date" . ,(current-time-string)))) (current-buffer))) f)
appendix.json
8 Caveats
Using org-mode like this is almost always finding the right tradeoffs in what is persistent, and where is it stored. Not all of the intermediate data/calculations are stored; if they are really cheap you can just run the code blocks again. If they are really small, i.e. easy for your to read in a few lines, you can store them in the document. If they are really large, you can store them in a file.
The beauty of having everything in an org-file is you have a single file that is easy to transport. When the files get too large though, it can become impractical, e.g. emacs may slow down if you try to put thousands of lines of xml data into the buffer. Then, you have to make some decisions about what to keep, where to keep it, and in what form to keep it.
For short projects where you only need a single compute session, having everything in memory may be fine. For longer projects, say one that is long enough you will close all the buffers, and possibly restart emacs in between working on it, then you have to make some decisions about what to save from each block so you can continue the work in the next session. Again, you have to decide what to save, where to save, and in what form.
Once you start saving data outside the org-file, it becomes less portable, or more tricky to move the file because you need to also move all the data files to keep it intact. I have explored a concept of making an org-archive in the past, where you get a list of all files linked in the org-file, but this so far has just been worked out for some small proof of concept ideas.
Not all languages are the same in org-mode. They do not all support sessions for example, and they may not all work like the examples here. The scimax iPython modifications do not behave like the examples above. That is probably due to bugs I have inadvertently introduced, and in the future I will try to make it work like emacs-lisp does above.
Overall, org-mode has one of the most flexible and powerful systems for passing and reusing data in documents I have ever seen. It is not perfect, and in such a powerful system there are many unexplored or lightly traveled corners that may have hazards in them. It still seems pretty promising though.
Copyright (C) 2019 by John Kitchin. See the License for information about copying.
Org-mode version = 9.2.1