8

It's possible to programmatically update an org table inside a babel source code block like the following:

#+name: mytable
| 0 | 0 | 0 |
| 0 | 0 | 0 |

#+begin_src emacs-lisp :var table=mytable
  (append table '((1 2 3)))
#+end_src

#+RESULTS:
| 0 | 0 | 0 |
| 0 | 0 | 0 |
| 1 | 2 | 3 |

I would like to update the original named table and discard the RESULTS output. In the case of this code it would keep appending a row to the table each time the source block is evaluated.

I've seen org samples that name the babel source the same as the source/target table (that presumably will do this) (https://eschulte.github.io/org-scraps/scraps/2011-08-19-replacing-a-table.html), but I get an org-get-property-block: Variable binding depth exceeds max-specpdl-size error whenever the names collide.

2 Answers 2

12

You can achieve that by naming your results table. Then, the code block will update the same table it used as input.

#+begin_src emacs-lisp :var table=mytable
  (append table '((1 2 3)))
#+end_src

#+name: mytable
#+RESULTS:
| 0 | 0 | 0 |
| 0 | 0 | 0 |
| 1 | 2 | 3 |

To have the greater flexibility that the OP had in mind: it isn't necessary to have the results immediately follow the source block if the source block and RESULTS label also share a name. Shown here:

#+name: mytable
#+RESULTS: mytable-calc
| 0 | 0 | 0 |
| 0 | 0 | 0 |
| 1 | 2 | 3 |

#+name: mytable-calc
#+begin_src emacs-lisp :var table=mytable
  (append table '((1 2 3)))
#+end_src
1

Having taken a look through the source, I'm not sure why that example would be expected to work. Perhaps org-babel has simply changed in the last four years.

Your test file works exactly as expected. Org-babel sees the table reference, calls into org-table to copy the table values from the buffer into a list structure, then passes that in as the argument. Your call to append copies the list, tacks on a new row and returns it. Org-babel formats the returned list structure as an org table.

What you'll probably have to do is pass in a string containing the name of the table (var="mytable"), find that named item (org-babel-named-data-regexp-for-name, I think?), narrow to it, double-check that it's a table and not something else (org-at-table-p), append the new row to it and widen again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.