Org Mode Tips and Tricks

Useful org mode snippets to remember

Adding Git Information

Formatting Numerical Table Entries

It can be rather painful to properly format numerical tables from R for export via org mode. Here is a slightly modified version of an example from the org mode manual that leaves integers untouched but prints floating point numbers with a specified format (almost). Alternatively, check out the ascii package for R.

#+NAME: round-tbl
#+BEGIN_SRC emacs-lisp :var tbl="" :var fmt="%.1f"
  (mapcar (lambda (row)
	    (mapcar (lambda (cell)
		      (if (floatp cell)
			  (format fmt cell)
			cell))
		    row))
	  tbl)
#+end_src

#+begin_src R :results replace :colnames yes :post round-tbl[:colnames yes](*this*)
  z <- dplyr::tribble(~i, ~x, 6, -pi, -4, exp(1), 1, 2)
  z
#+end_src

#+RESULTS:
|  i |    x |
|----+------|
|  6 | -3.1 |
| -4 |  2.7 |
|  1 |    2 |

Note that I get a 2 in the last cell, not the 2.0 that I was hoping for. FWIW, wrapping z in print.data.frame() in the R code does not solve this problem. The problem is that something in the emacs/orgmode/elisp processing is intercepting the 2.000 and deciding that it's an integer. Evidently I either need to figure that out or, alternatively, rewrite the elisp code to operate on the table in a column wise manner (ignoring the column header).

Note that it's easy to change the format used, but of course it doesn't do a thing about the "2".

#+begin_src R :results replace :colnames yes :post round-tbl[:colnames yes](*this*, fmt="%.3f")
  z <- dplyr::tribble(~i, ~x, 6, -pi, -4, exp(1), 1, 2)
  z
#+end_src

#+RESULTS:
|  i |      x |
|----+--------|
|  6 | -3.142 |
| -4 |  2.718 |
|  1 |      2 |

Replacing Table Headers

Sometimes I want to replace the variable names used as column header in R export with a custom, multi-row header. Here's an example of how this might be done. I haven't been careful about handling headers in the elisp code, so one may need to be careful about setting :colnames (and possibly :hlines) appropriately (note that :colnames nil is the default (and :hlines no). I have tried to illustrate this below.

For me the tables would normally be something that had been computed in R, but for simplicity I'm just reading the data straight from the org file.

#+name: headed_table
| a     |   b |     c |
|-------+-----+-------|
| red   | 148 | 77.08 |
| white | 140 | 72.92 |
| blue  | 102 | 53.13 |

#+name: headless_table
| red   | 148 | 77.08 |
| white | 140 | 72.92 |
| blue  | 102 | 53.13 |

#+name: replacement_header
|       | Number | Percentage |
|       | of     | of         |
| Color | Flags  | Flags      |

Here's the (simple) elisp code.

#+name: replace-header
#+BEGIN_SRC emacs-lisp :var tab="" new_header=""
  (append new_header (cons 'hline tab))
#+END_SRC

Here's how to call it straight from elisp to work with an existing org table.

#+call: replace-header(headed_table, replacement_header)

#+RESULTS:
|       | Number | Percentage |
|       |     of |         of |
| Color |  Flags |      Flags |
|-------+--------+------------|
| red   |    148 |      77.08 |
| white |    140 |      72.92 |
| blue  |    102 |      53.13 |

Here's calling it from R with a table having a header.

#+begin_src R :var data=headed_table :post replace-header(*this*, replacement_header)
  data
#+end_src

#+RESULTS:
|       | Number | Percentage |
|       |     of |         of |
| Color |  Flags |      Flags |
|-------+--------+------------|
| red   |    148 |      77.08 |
| white |    140 |      72.92 |
| blue  |    102 |      53.13 |

Here's calling it from R with a headless table (probably pretty rare in practice).

#+begin_src R :var data=headless_table :colnames no :post replace-header(*this*, replacement_header)
  data
#+end_src

#+RESULTS:
|       | Number | Percentage |
|       |     of |         of |
| Color |  Flags |      Flags |
|-------+--------+------------|
| red   |    148 |      77.08 |
| white |    140 |      72.92 |
| blue  |    102 |      53.13 |

And here is how to combine this with the formatting provided by the round-tbl function from elsewhere in this document: just compose the functions in the :post header argument.

#+begin_src R :var data=headed_table :post replace-header(round-tbl(*this*, "%.1f"), replacement_header)
  data
#+end_src

#+RESULTS:
|       | Number | Percentage |
|       |     of |         of |
| Color |  Flags |      Flags |
|-------+--------+------------|
| red   |    148 |       77.1 |
| white |    140 |       72.9 |
| blue  |    102 |       53.1 |

Naming Source Code Blocks and Their Results for Referencing

This response by Nicolas Goaziou is as clear an explanation as you'll ever see.

:results drawer and maybe :wrap export org

These might be useful for code blocks that need to be evaluated as org text. Check the org list from around 2020-11-11 for more.

Stacking Tables

The org-mode equivalent of rbind() in R is revealed here by Emmanuel Charpentier.

Org Mode Interface to Canvas

This project might be worth keeping an eye on if my use of Canvas expands.

Easy-Hugo

The easy-hugo package might be useful for maintaining my web site.

Brett Presnell
Brett Presnell
Emeritus Professor of Statistics

My research interests include nonparametric and computationally intensive statistics, model misspecification, statistical computing, and the analysis of directional data.