This is a continuation of my previous post, Running Python and R inside Emacs. That post shows how to execute independent code blocks in Emacs org-mode. This post illustrates calling one code block from another, each written in a different language.
The example below computes sin2(x) + cos2(x) by computing the sine function in R, the cosine function in Python, and summing their squares in Perl. As you’d hope, it returns 1. (Actually, it returns 0.99999999999985 on my machine.)
To execute the code, go to the #+call
line and type C-c C-c.
#+name: sin_r(x=1) #+begin_src R sin(x) #+end_src #+name: cos_p(x=0) #+begin_src python import math return math.cos(x) #+end_src #+name: sum_sq(a = 0, b = 0) #+begin_src perl $a*$a + $b*$b; #+end_src #+call: sum_sq(sin_r(1), cos_p(1))
Apparently each function argument has to have a default value. If that’s documented, I missed it. I gave the sine and cosine functions default values that would cause the call to sum_sq
to return more than 1 if the defaults were used.
In what way does this post illustrate calling one code block from another, whatever language they’re written in? It seems to me to show code blocks in each of three languages followed by an expression in its own little language which calls each of those in turn. There doesn’t seem to be any call from one code block to another. Have I missed something?
In older versions of org-babel the header ‘#+name’ was ‘#+srcname’. If the above example doesn’t work, check the value of org-babel-src-name-regexp.
Hi John,
Documentation for the requirement that :var header arguments have default values is here: http://orgmode.org/manual/var.html#var
The first paragraph ends with this sentence:
In every case, variables require a default value when they are declared.
hth,
Tom