0

This paper inspired me to check out Emac's org-mode a bit and currently I try to assess what's more suitable for writing my documents: knitr/Sweave (I'm mainly using R to do my programming) or org-mode.

What I really like about knitr is the option to externalize the actual source (watch out: the declaration of labels/names in the R script seems to have changed from ## ---- label ------- to ## @knitr label; see ?read_chunk) and "link" them to the actual literate programming/reproducible research document (as opposed to actually writing the code in that very document):

"Import" with

<<import-external, cache=FALSE>>=
read_chunk('foo-bar.R') # has label/name 'foo-bar'
@

and "re-use" by referencing the respective labels with

<<foo-bar>>=
@

Question

Is this also possible in org-mode or am I bound to putting the actual code into the .org document?

I found this, but I did not find any specific notion of linking/importing external source code files and be able to execute them by having the linked code inside

#+BEGIN_SRC R
<linked code>
#+END_SRC

Background

I do see that this approach might contrast the general paradigm of literate programing to some extend. But I like to work in a somewhat "atomic" style and thus it feels more natural to me to keep the files separated at first and then mash everything together dynamically.

Rappster
  • 12,762
  • 7
  • 71
  • 120

1 Answers1

1

Would named code blocks help?

 #+NAME: foo-bar
 #+BEGIN_SRC R
   source(foo-bar.R)
 #+END_SRC

And then evaluate (i.e. load) the code when you actually need it:

 #+CALL: foo-bar()

See manual for more details.

Alex Vorobiev
  • 4,349
  • 21
  • 29
  • "Duh" to myself! Of course, that should work! Thanks a lot man! – Rappster Aug 13 '13 at 13:22
  • Just to make sure, though: that way the code in `foo-bar.R` is expected to be a `function` (as opposed to other types of "loose" expressions), right? Because seems that in `#+CALL:` you actually calling a function. Or is the `()` part just the org-mode way of passing/"overwriting" arbitrary code elements to named code blocks? – Rappster Aug 13 '13 at 13:27
  • The name in the `#+CALL:` is what you used in `#+NAME:`, it has nothing to do with what is actually in your R code (it can be anything). The `()` part is the way to pass parameters to the named code block where you could have defined header parameters (e.g. `:var n=2`), then you can do `#+CALL: foo-bar(n=4)` (see that section from the Org mode manual). – Alex Vorobiev Aug 13 '13 at 13:55
  • Cool, thanks a lot for clarifying that. Then I think that should perfectly do! – Rappster Aug 13 '13 at 16:41