From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Schulte Subject: Re: formatting org-babel output Date: Wed, 22 May 2013 07:12:41 -0600 Message-ID: <8761ybrpk5.fsf@gmail.com> References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([208.118.235.92]:41983) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UfBQl-0001wH-Vn for emacs-orgmode@gnu.org; Wed, 22 May 2013 11:58:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UfB9g-0001uW-1Z for emacs-orgmode@gnu.org; Wed, 22 May 2013 11:41:39 -0400 Received: from mail-pd0-f179.google.com ([209.85.192.179]:57070) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UfB9f-0001uI-NK for emacs-orgmode@gnu.org; Wed, 22 May 2013 11:40:43 -0400 Received: by mail-pd0-f179.google.com with SMTP id q11so1813305pdj.38 for ; Wed, 22 May 2013 08:40:42 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Joe Bogner Cc: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Joe Bogner writes: > I am using org-mode and babel with R for reproducible research. I > would like certain numbers in the output tables to be formatted for > easier reading - such as eliminating decimals and adding commas for > readability. > > The best I came up with is to use a TBLFM line at the bottom of my > results table using a function I found on the ElispCookbook on > emacswiki. A simple example is below that doesn't require R to > reproduce. > > It's a two step process currently to execute the R code in org-babel > and then jump to the result table TBLFM line and Ctrl+c Ctrl+c to > format the table > > Is there a better way to format table outputs for simple things like > currency? I would be content if it's only during the org export > process too > You can use the recently introduced :post header argument to post-process the output of a code block. The following example demonstrates the use of this argument to apply your number-grouper function to table output. --=-=-= Content-Type: text/x-org Content-Disposition: inline; filename=post-processing.org #+BEGIN_SRC emacs-lisp :results silent (defun group-number (num &optional size char) "Format NUM as string grouped to SIZE with CHAR." ;; Based on code for `math-group-float' in calc-ext.el (let* ((size (or size 3)) (char (or char ",")) (str (if (stringp num) (number-to-string (string-to-number (replace-regexp-in-string "," "" num))) (number-to-string num))) (pt (or (string-match "[^0-9a-zA-Z]" str) (length str)))) (while (> pt size) (setq str (concat (substring str 0 (- pt size)) char (substring str (- pt size))) pt (- pt size))) str)) #+END_SRC The following code block will call the group-number Emacs Lisp function on every cell in its input argument which is a number. #+name: table-number-grouper #+BEGIN_SRC emacs-lisp :var data='() (mapcar (lambda (row) (if (equalp row 'hline) 'hline (mapcar (lambda (cell) (if (numberp cell) (group-number cell) cell)) row))) data) #+END_SRC Here's a simple shell code block which produces the table (this is a stand in for your R code block). #+begin_src sh cat <