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.
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/
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With