1

My 1st question on Stack (>﹏<)!

When I write my homework in org-mode, I want to use the literate programming with org-babel.

So I use python as the major language to calculate the results I need in the org file. I use inline call(#+call: or call_<function-name>) with different arguments to realize different functions.

However, I find that #+call: adds an extra newline between in the results block. To avoid that , I use call_<function-name> as an inline call.

But my functions take time to execute, so I want to cache the results of function calls to avoid delays. But with the call, call_<function-name>[:cache yes], the results are not cached.

The following is the original function:

#+NAME: Daoqiex00
#+begin_src python :var R0=10 N=19 :exports none :results value :cache yes
import numpy as np
return np.cosh(1/(N-1)*np.arccosh(R0))
#+end_src

And the inline call is:

We will find \(x_{0}=\) call_Daoqiex00(R0=10,N=19)[:results value raw :cache yes].

How can I make this inline call cache the results?

(This has been tested on emacs 24.5, using org 8.2.10)

2

Caching is not possible for inline functions as per the remarks the in the org-babel manual (see excerpt):

In order for caching to work (i.e., no evaluation when triggered either interactively or during export), the results of the code block must be present in the Org mode file: you must first evaluate it manually, leaving the results (with the hash tag) saved within the Org mode file.

One workaround is to make another src block with your function that stores the result. Then use an inline function to call this src block, which will be faster because there is no re-calculation, just re-reading the result. For example:

#+name: cachedFunction
#+BEGIN_SRC python :cache yes
x = 18
return x
#+END_SRC

#+name: uncachedFunction
#+BEGIN_SRC python :var x=cachedFunction
return int(x)
#+END_SRC

Now any calls to call_uncachedFunction() will get the cached value from cachedFunction.

This is the block I use for just those org files that need caching:

#+begin_src emacs-lisp :cache yes
   (setq org-babel-default-header-args '((:session . "none")
                                         (:results . "replace")
                                         (:exports . "code")
                                         (:cache)))
#+end_src

Lastly, when caching works, you will see the hash tag with the results line, below the src block, as follows:

#+RESULTS[84d9...]:
  • So How to make the results of code block stored to a new src block? Could you show me a piece of example code or a reference link? Thanks! – husky May 3 '16 at 8:13
  • I've added a simple example to illustrate the point. – Emacs User May 3 '16 at 17:23

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.