Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing argument values dynamically

Tags:

emacs

r

org-mode

When I'm passing arguments to a #+begin_src block, is there a way to compute them dynamically?

Specifically, I want to set the :height attribute to something that depends on some variables in my R code, like in the following mockup:

#+begin_src R
x <- 5
#+end_src

#+begin_src R :results graphics :file foo.svg :height (3*getvar('x'))
...draw picture here
#+end_src

where that getvar() thing, and computations therewith, is maybe my wishful thinking.

like image 890
Ken Williams Avatar asked Feb 09 '12 16:02

Ken Williams


2 Answers

I do not know how to use org-mode to do that, but this is already a feature in the knitr package (an alternative to Sweave), so if you do not mind the Sweave syntax, you can use:

<<>>=
x <- 5
<<foo, dev='svg', fig.height=3*x>>=
# draw plots here
@

More on org-mode in knitr: http://yihui.name/knitr/demo/org/

like image 59
Yihui Xie Avatar answered Nov 19 '22 21:11

Yihui Xie


Org-mode now interprets brackets in the header specs as elisp so you can do this with some elisp in between:

Named R src block

 #+name: default-height
 #+begin_src R
   x <- 300
 #+end_src

 #+results: default-height
 : 300

Make the result from R an emacs variable

#+begin_src emacs-lisp :var incoming = default-height :results silent
  (setq dh incoming)
#+end_src

Use of elisp in source block header

#+begin_src R :results graphics :file test.png :height (* dh 3)
  plot(rnorm(100))
#+end_src

#+results:
[[file:test.png]]

Works for me :)

like image 1
tony day Avatar answered Nov 19 '22 20:11

tony day