emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Inserting arbitrary lines before tables with Babel
@ 2022-10-29 21:09 Kierin Bell
  2022-10-30  4:43 ` Ihor Radchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Kierin Bell @ 2022-10-29 21:09 UTC (permalink / raw)
  To: emacs-orgmode


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Inserting arbitrary lines before tables with Babel
  2022-10-29 21:09 Inserting arbitrary lines before tables with Babel Kierin Bell
@ 2022-10-30  4:43 ` Ihor Radchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Ihor Radchenko @ 2022-10-30  4:43 UTC (permalink / raw)
  To: Kierin Bell; +Cc: emacs-orgmode

Kierin Bell <bellkierin@gmail.com> writes:

> 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.

Just insert it right before #+results keyword. #+results is just another
affiliated keyword and can be combined with others.

#+NAME: random-table-1
#+RESULTS: get-random-table
|  0.645291731692851 |
| 0.0425670417025685 |
|  0.220107783330604 |
|  0.413881630403921 |
|  0.817712268792093 |
|  0.972381719155237 |

Also, if you use :exports results, you can refer to the table as
"get-random-table" - the same with the source block name.

> 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 is wrong with the example in 16.6 Results of Evaluation? It shows
how to prepend #+ATTR_LATEX :width 5cm. #+NAME: would be the same.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-30  4:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-29 21:09 Inserting arbitrary lines before tables with Babel Kierin Bell
2022-10-30  4:43 ` Ihor Radchenko

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).