emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Inserting arbitrary lines before tables with Babel


From: Kierin Bell
Subject: Inserting arbitrary lines before tables with Babel
Date: Sat, 29 Oct 2022 17:09:23 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Hello,

When evaluating Babel code blocks with a ':results table' header
argument, what is the best way to automatically insert text _outside_
of the table?

For example, if I have a source block:
#+NAME: get-random-table
#+begin_src R :var length=12 :results table
  runif(length)
#+end_src

... Then 'org-babel-execute-maybe' (C-c C-v e) will output a nicely
formatted Org table:
#+RESULTS: get-random-table
|  0.645291731692851 |
| 0.0425670417025685 |
|  0.220107783330604 |
|  0.413881630403921 |
|  0.817712268792093 |
|  0.972381719155237 |

... But if I want to insert a line, for example containing a 'NAME'
keyword, before and _outside of_ that table, there seems to be no easy
way to do this automatically.

Trying to insert lines using subsequent code blocks (arguments, noweb,
etc.) will either strip Org table formatting or add additional lines
within the table instead of outside of it (depending on the ':results'
arguments). There is the same problem with using a ':post' header
argument, and ':wrap' does not allow arbitrary insertion of individual
lines.

What I have in mind is to be able to do things like:
#+CALL: get-named-table(name="random-table-1", source-block="get-random-table", 
length=7)

#+RESULTS:
#+NAME: random-table-1
| 0.310966729884967 |
| 0.700660327449441 |
| 0.668548531830311 |
| 0.497187710367143 |
| 0.245545281097293 |
| 0.597585438517854 |
| 0.639235997572541 |

The only "solution" I can think of -- using Elisp to reformat list
objects as Org tables -- feels like an overly complicated hack:

#+NAME: get-named-table
#+begin_src emacs-lisp :results raw :var name="default-name" length=12
  (when (and (boundp 'source-block) (stringp source-block))
    (let ((data (ensure-list (read (eval `(org-sbe ,source-block (length 
,length)))))))
          (concat
           (format "#+NAME: %s" name)
           "\n"
           (orgtbl-to-orgtbl
            (or (and (seq-every-p
                      (lambda (e)
                        (or (eq e 'hline) (listp e)))
                      data)
                     data)
                (list data))
            nil))))
#+end_src

Is there a better way? (I'm learning that there usually is with Org.)
The ability to dynamically prepend and append (e.g., #+TBLFM:) lines to
results tables seems like it would be useful.


Thanks,
Kierin



reply via email to

[Prev in Thread] Current Thread [Next in Thread]