Question

I am having trouble calling my babel code blocks from tables using sbe. Here are some minimal examples that don't work for me:

|   |    in: | in:    | out:     | out:     | out:      |
| ! | number | string | elispStr | elispNum | pythonStr |
|---+--------+--------+----------+----------+-----------|
| # |      5 | fd     | #ERROR   | #ERROR   | #ERROR    |
| # |      7 | fadsf  | #ERROR   | #ERROR   | #ERROR    |
#+TBLFM: $4='(sbe elispStrBlock (foo $string))::$5='(sbe elispNumBlock (a $number));N::$6='(sbe pythonStrBlock (foo $string))

#+name elispNumBlock
#+header: :var a=4
#+begin_src emacs-lisp :results value
(message (number-to-string a))
#+end_src

#+RESULTS:
: 4

#+name elispStrBlock
#+header: :var a="testing"
#+begin_src emacs-lisp :results value 
(message a)
#+end_src

#+RESULTS:
: testing

#+name pythonStrBlock
#+header: :var foo="testing"
#+begin_src python :results output
print foo
#+end_src

#+RESULTS:
: testing

The code blocks work when I evaluate them with C-c C-c with the point in the block (hence the #+RESULTS output shown above).

I have looked at the mailing lists (for example) and have seen that some people can output babel errors to an *Org Babel Error Output* buffer, I guess this would help debugging but I couldn't see where to turn this on (toggle-debug-on-error didn't work)

I am using org-version: 8.2.3c. I have also tried the above in a fresh emacs with the -q option to ignore my config on startup. I have made sure that python is enabled for evaluation:

(org-babel-do-load-languages 'org-babel-load-languages '((emacs-lisp . t) (python . t)))
Was it helpful?

Solution

Debugging revealed: a colon is missing in

#+name elispNumBlock

It must be:

#+name: elispNumBlock

The following advice delivers error messages of sbe to the message buffer:

(defadvice sbe (around get-err-msg activate)
  "Issue messages at errors."
  (condition-case err
      (progn
    ad-do-it)
    ((error debug)
     (message "Error in sbe: %S" err)
     (signal (car err) (cdr err)))))

Without it error messages are just translated into the table cell content #ERROR. Not so informative.

As I already commented: The N output format does not really work as expected. The resulting strings are substituted into the command call. So you need additional quoting for strings.

For me, the following is working:

|   |    in: | in:     | out:     |     out: | out:      |
| ! | number | string  | elispStr | elispNum | pythonStr |
|---+--------+---------+----------+----------+-----------|
| # |      5 | "fd"    | fd       |        5 | fd        |
| # |      7 | "fadsf" | fadsf    |        7 | fadsf     |
#+TBLFM: $4='(sbe elispStrBlock (foo $string))::$5='(sbe elispNumBlock (a $number))::$6='(sbe pythonStrBlock (foo $string))

#+name: elispNumBlock
#+header: :var a=4
#+begin_src emacs-lisp :results value
(message (number-to-string a))
#+end_src

#+RESULTS:
: 4

#+name: elispStrBlock
#+header: :var foo="testing"
#+begin_src emacs-lisp :results value 
(message foo)
#+end_src


#+name: pythonStrBlock
#+header: :var foo="testing"
#+begin_src python :results output
print foo
#+end_src

EDIT: About the cell references "$2". The following works for me:

| ! | arg | res |
|---+-----+-----|
| # |   1 |   2 |
| # |   2 |   3 |
#+TBLFM: $3='(sbe myColFm (el $2))

#+name: myColFm
#+header: :var el=1
#+begin_src emacs-lisp
  (1+ el)
#+end_src

EDIT: With the additional advice

(defadvice sbe (before escape-args activate)
  "Apply prin1 to argument values."
  (mapc '(lambda (var) (setcdr var (list (prin1-to-string (cadr var))))) variables))

the command sbe works as one would expect:

| ! | arg | res |   |
|---+-----+-----+---|
| # |   1 |   3 | 5 |
| # |   2 |   4 | 6 |
#+TBLFM: $3='(sbe myStr (el $2))::$4='(sbe myColFm (el $2));N

#+name: myStr
#+header: :var el="1"
#+begin_src emacs-lisp
  (+ 2 (string-to-number el))
#+end_src

#+name: myColFm
#+header: :var el=1
#+begin_src emacs-lisp
  (+ 4 el)
#+end_src
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top