rprospero
rprospero

Reputation: 961

How does one reference the result of a call block in an emacs org file?

I'm using emacs org mode for reproducible research. I'm having trouble chaining the results of calls to code blocks. As a minimal example of what I'm trying to do:

* Functions
#+name: f1
#+begin_src elisp :var x=7
  (+ x x)
#+end_src

#+name: f2
#+begin_src elisp :var x=7
  (+ x 2)
#+end_src

* Data
#+name: intermediate
#+call: f1(x=5)

#+RESULTS: intermediate
: 10

#+call: f2(x=intermediate)

When I call f2, it fails because x is nil. If I remove the name from the call on f1, then f2 works, but the results don't stay in sync if I change the parameters for f1.

How do I tell org mode that I want to see the results of the function call without needing to manually rename the results every time they are calculated?

Upvotes: 4

Views: 1560

Answers (1)

itsjeyd
itsjeyd

Reputation: 5280

You can achieve what you want by using Noweb reference syntax in your calculations under the Data heading:

* Functions
...

* Data
#+name: intermediate
#+BEGIN_SRC elisp :noweb yes
<<f1(x=5)>>
#+END_SRC

#+BEGIN_SRC elisp :noweb yes
<<f2(x=intermediate)>>
#+END_SRC

From the manual (emphasis mine):

The "noweb" ... Literate Programming system allows named blocks of code to be referenced by using the familiar Noweb syntax:

<<code-block-name>>

When a code block is tangled or evaluated, whether or not "noweb" references are expanded depends upon the value of the :noweb header argument. If :noweb yes, then a Noweb reference is expanded before evaluation. [...]

It is possible to include the results of a code block rather than the body. This is done by appending parentheses to the code block name which may optionally contain arguments to the code block as shown below.

<<code-block-name(optional arguments)>>

With this setup you can change the value of x that you are passing to f1 and re-export/re-evaluate successfully without having to modify anything else.

Upvotes: 4

Related Questions