emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Kierin Bell <bellkierin@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Inserting arbitrary lines before tables with Babel
Date: Sat, 29 Oct 2022 17:09:23 -0400	[thread overview]
Message-ID: <87wn8ibam4.fsf@gmail.com> (raw)


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	other threads:[~2022-10-29 21:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-29 21:09 Kierin Bell [this message]
2022-10-30  4:43 ` Inserting arbitrary lines before tables with Babel Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wn8ibam4.fsf@gmail.com \
    --to=bellkierin@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).