动态计算参数值

时间:2012-02-09 16:57:46

标签: r emacs org-mode

当我将参数传递给#+begin_src块时,有没有办法动态计算它们?

具体来说,我想将:height属性设置为依赖于R代码中某些变量的内容,如下面的模型中所示:

#+begin_src R
x <- 5
#+end_src

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

其中getvar()事物及其计算可能是我的一厢情愿。

2 个答案:

答案 0 :(得分:2)

我不知道如何使用org-mode来做到这一点,但这已经是knitr包中的一个功能(替代Sweave),所以如果你不介意Sweave语法,你可以使用方法:

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

有关knitrhttp://yihui.name/knitr/demo/org/

中org-mode的更多信息

答案 1 :(得分:1)

Org-mode现在将标题规范中的括号解释为elisp,因此您可以使用其中的一些elisp执行此操作:

命名为R src块

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

 #+results: default-height
 : 300

从R得到一个emacs变量

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

在源块标题中使用elisp

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

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

适合我:)