4

I would like to have the output of src_R blocks in an org-mode table:

| Variable | Value    |
|----------+----------|
| x        | src_R{x} |

However, when I export to PDF (via LaTeX) I get the literal src_R{x} rather than the value of the x variable in the underlying R session. I can use the same src_R{x} in text and it works as expected.

Is there a way to support inline source code in tables?

(I have seen this question with a similar title: Code blocks inside tables for org-mode, but the topic is different.)

Stefano
  • 251
  • 1
  • 9
  • 1
    A workaround is to use an elisp formula that evaluates the expression. I don't have R installed, but the following works for elisp: `#+TBLFM: @2$2='(org-babel-execute:emacs-lisp "(+ 2 2)" nil)` – Juancho Jul 07 '17 at 18:42

1 Answers1

5

Thanks to the prompt by Juancho (see comments), I have found the answer here: http://orgmode.org/worg/org-contrib/babel/intro.html#spreadsheet. I first define a named source block to perform my R computation:

#+NAME: my-code
#+BEGIN_SRC R :results output
  message(10)
#+END_SRC

(Imagine the output is simply the number 10). Then I insert it into the table like this:

| Variable | Value |
|----------+-------|
| Name     |       |
#+TBLFM: @2$2='(org-sbe my-code)

Comments:

  • It seems that org-babel-execute is no longer there, the docs use org-sbe, which works with my 9.0.x org-mode version.
  • I have wrapped the code output in message() to avoid extra output from R. I have tried various header arguments to the R code (e.g., :results value raw) but I get either extra parentheses, presumably from lisp, or errors.
  • With org-sbe you can also pass arguments to the code, and even the output of other code blocks. This is explained in the docs referenced above.
Stefano
  • 251
  • 1
  • 9