[O] Calling org babel to each row in a table

2013-06-30 Thread Joe Bogner
I often need to transform a table or list of values into a block of text.
For example, if I have a list of 4 files that I want to generate a SQL
script for.

After hunting around, this is the best I came up with:

#+name: table
| File |
| a|
| b|
| c|
| d|

#+name: template
#+begin_src org :results verbatim :var name="abc"
DROP TABLE $name

CREATE TABLE $name (name varchar(100))

BULK INSERT $name FROM '\\1.1.1.1\$name.txt' ...
#+end_src

#+name: apply-template
#+begin_src emacs-lisp :results silent :var table=()
(let (result-table)
(dolist (line (cdr table))
(setq result-table (cons (concat "#+call: template(\"" (car line)
"\") :results raw drawer") result-table)))
(mapconcat 'identity (nreverse result-table) "\n"))
#+end_src

#+call: apply-template(table) :results org :exports both

#+RESULTS: apply-template(table):results org :exports both
#+BEGIN_SRC org
#+END_SRC



Is there a more straightforward method to apply this type of
transformation? Ideally I would skip the emacs-lisp block and use some
syntax to apply a org-babel block for each row in a table.


It sounded similar to this:
http://thread.gmane.org/gmane.emacs.orgmode/69326/focus=69340*, *but that
didn't have a full example for me to build off of

Is there a more straightforward to accomplish this transformation?

Thanks,
Joe


Re: [O] Org-babel & R exporting multiple tables

2013-05-22 Thread Joe Bogner
Figured it out. Option #2 works with ob-org as long as the defaults
are set to output the results

(setq org-babel-default-header-args:org '((:results . "raw") (:exports
. "results")))

By default it looks like it's silent.



On Wed, May 22, 2013 at 5:26 PM, Joe Bogner  wrote:
> I have a list of data frames that I would like to output. The number
> of frames may vary
>
> Is there a way to export multiple tables formatted? These are the
> three options I came up with. None of them work very well:
>
> Option #3 works the best, but it will append each time it's run in the
> doc, so I can't have the contents in the doc without having it
> duplicated.
>
> Option #2 would be good if I could just instruct it to render the table as 
> HTML
>
> There was a post about 2 years ago that I attempted implement by
> adding (require 'ob-org) to my .emacs for #2 but it didn't seem to
> work 
> (http://article.gmane.org/gmane.emacs.orgmode/29286/match=results+org+babel+ascii).
>
>
>
> * Option 1
>
> Will combine the frames
>
> #+begin_src R :session *R* :colnames yes
> frames <- list()
> frames[[1]] <- data.frame(col=c("a","b"))
> frames[[2]] <- data.frame(col2=c("a","b"))
> frames
> #+end_src
>
> #+RESULTS:
> | col | col2 |
> |-+--|
> | a   | a|
> | b   | b|
>
>
>
> * Option 2
>
> It won't format on export
>
> #+begin_src R :session *R* :results output org :exports both
> frames <- list()
> frames[[1]] <- data.frame(col=c("a","b"))
> frames[[2]] <- data.frame(col2=c("a","b"))
> for(i in 1:2) {
> print(ascii(frames[[i]]), type="org")
> cat("\n")
> }
> #+end_src
>
> #+RESULTS:
> #+BEGIN_SRC org
>  |   | col |
>  |---+-|
>  | 1 | a   |
>  | 2 | b   |
>
> |   | col2 |
> |---+--|
> | 1 | a|
> | 2 | b|
> #+END_SRC
>
>
> * Option 3
>
> It will format on export but won't replace if it's run in the document
>
> #+begin_src R :session *R* :results output raw :exports both
> frames <- list()
> frames[[1]] <- data.frame(col=c("a","b"))
> frames[[2]] <- data.frame(col2=c("a","b"))
> for(i in 1:2) {
> print(ascii(frames[[i]]), type="org")
> cat("\n")
> }
> #+end_src



[O] Org-babel & R exporting multiple tables

2013-05-22 Thread Joe Bogner
I have a list of data frames that I would like to output. The number
of frames may vary

Is there a way to export multiple tables formatted? These are the
three options I came up with. None of them work very well:

Option #3 works the best, but it will append each time it's run in the
doc, so I can't have the contents in the doc without having it
duplicated.

Option #2 would be good if I could just instruct it to render the table as HTML

There was a post about 2 years ago that I attempted implement by
adding (require 'ob-org) to my .emacs for #2 but it didn't seem to
work 
(http://article.gmane.org/gmane.emacs.orgmode/29286/match=results+org+babel+ascii).



* Option 1

Will combine the frames

#+begin_src R :session *R* :colnames yes
frames <- list()
frames[[1]] <- data.frame(col=c("a","b"))
frames[[2]] <- data.frame(col2=c("a","b"))
frames
#+end_src

#+RESULTS:
| col | col2 |
|-+--|
| a   | a|
| b   | b|



* Option 2

It won't format on export

#+begin_src R :session *R* :results output org :exports both
frames <- list()
frames[[1]] <- data.frame(col=c("a","b"))
frames[[2]] <- data.frame(col2=c("a","b"))
for(i in 1:2) {
print(ascii(frames[[i]]), type="org")
cat("\n")
}
#+end_src

#+RESULTS:
#+BEGIN_SRC org
 |   | col |
 |---+-|
 | 1 | a   |
 | 2 | b   |

|   | col2 |
|---+--|
| 1 | a|
| 2 | b|
#+END_SRC


* Option 3

It will format on export but won't replace if it's run in the document

#+begin_src R :session *R* :results output raw :exports both
frames <- list()
frames[[1]] <- data.frame(col=c("a","b"))
frames[[2]] <- data.frame(col2=c("a","b"))
for(i in 1:2) {
print(ascii(frames[[i]]), type="org")
cat("\n")
}
#+end_src



Re: [O] formatting org-babel output

2013-05-22 Thread Joe Bogner
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(5))
[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  wrote:
> 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.
>
>
>
> Cheers,
>
> --
> Eric Schulte
> http://cs.unm.edu/~eschulte
>



[O] formatting org-babel output

2013-05-22 Thread Joe Bogner
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
 | 1.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)'