emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* formatting org-babel output
@ 2013-05-22 11:18 Joe Bogner
  2013-05-22 13:12 ` Eric Schulte
  2013-05-22 13:14 ` Jay Kerns
  0 siblings, 2 replies; 4+ messages in thread
From: Joe Bogner @ 2013-05-22 11:18 UTC (permalink / raw)
  To: emacs-orgmode

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



#+BEGIN_SRC emacs-lisp
(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

#+RESULTS:
: group-number

 #+NAME: example-table
 | 100000000.55 | a |
 |     23000 | b |
 |      3000 | c |
 |     40004 | e |
 |      5000 | 3 |


This is the table I want formatted

#+BEGIN_SRC emacs-lisp :var data=example-table
data
#+END_SRC

 #+RESULTS:
 | 100,000,000.55 | a |
 | 23,000         | b |
 | 3,000          | c |
 | 40,004         | e |
 | 5,000          | 3 |
#+TBLFM: $1='(group-number $1)'

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

* Re: formatting org-babel output
  2013-05-22 11:18 formatting org-babel output Joe Bogner
@ 2013-05-22 13:12 ` Eric Schulte
  2013-05-22 16:27   ` Joe Bogner
  2013-05-22 13:14 ` Jay Kerns
  1 sibling, 1 reply; 4+ messages in thread
From: Eric Schulte @ 2013-05-22 13:12 UTC (permalink / raw)
  To: Joe Bogner; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1015 bytes --]

Joe Bogner <joebogner@gmail.com> 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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: post-processing.org --]
[-- Type: text/x-org, Size: 1966 bytes --]

#+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 <<EOF
  100000000.55    a
  23000   b
  3000    c
  40004   e
  5000    3
  EOF
#+end_src

#+RESULTS:
| 100000000.55 | a |
|        23000 | b |
|         3000 | c |
|        40004 | e |
|         5000 | 3 |

Above is the poorly formatted output of your block.  Below we add the
"post" header argument to your block, which will use the
"table-number-grouper" code block above to post-process your results.

#+begin_src sh :post table-number-grouper(*this*)
  cat <<EOF
  100000000.55    a
  23000   b
  3000    c
  40004   e
  5000    3
  EOF
#+end_src

#+RESULTS:
| 100,000,000.55 | a |
| 23,000         | b |
| 3,000          | c |
| 40,004         | e |
| 5,000          | 3 |

[-- Attachment #3: Type: text/plain, Size: 55 bytes --]


Cheers,

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

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

* Re: formatting org-babel output
  2013-05-22 11:18 formatting org-babel output Joe Bogner
  2013-05-22 13:12 ` Eric Schulte
@ 2013-05-22 13:14 ` Jay Kerns
  1 sibling, 0 replies; 4+ messages in thread
From: Jay Kerns @ 2013-05-22 13:14 UTC (permalink / raw)
  To: Joe Bogner; +Cc: emacs-orgmode

Hi Joe,

On Wed, May 22, 2013 at 7:18 AM, Joe Bogner <joebogner@gmail.com> wrote:
> 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.
>

[snip]

Have you considered doing your formatting in R before it ever gets to
the org table?  The format function is very powerful. For instance,

> format(163328361.2423, nsmall = 2, big.mark =",")
[1] "163,328,361.24"

Just an idea.

-- 
Jay

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

* Re: formatting org-babel output
  2013-05-22 13:12 ` Eric Schulte
@ 2013-05-22 16:27   ` Joe Bogner
  0 siblings, 0 replies; 4+ messages in thread
From: Joe Bogner @ 2013-05-22 16:27 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode

Eric - That is perfect. It's exactly what I was looking for. Thank you
for very much!

Jay - format is a good option too.I also found the scales packages

> scales::comma(scales::dollar(55555))
[1] "$55,555"

In the end, the org-mode post solution feels more automatic and in the
right layer - since I'm using org-mode for presentation of the R
results.

Thanks again
Joe

On Wed, May 22, 2013 at 9:12 AM, Eric Schulte <schulte.eric@gmail.com> wrote:
> Joe Bogner <joebogner@gmail.com> 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.
>
>
>
> Cheers,
>
> --
> Eric Schulte
> http://cs.unm.edu/~eschulte
>

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

end of thread, other threads:[~2013-05-22 16:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-22 11:18 formatting org-babel output Joe Bogner
2013-05-22 13:12 ` Eric Schulte
2013-05-22 16:27   ` Joe Bogner
2013-05-22 13:14 ` Jay Kerns

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