emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [babel]  apply #+TABLEFM lines during export?
@ 2010-07-10 14:13 Austin Frank
  2010-07-11 22:26 ` Eric Schulte
  0 siblings, 1 reply; 5+ messages in thread
From: Austin Frank @ 2010-07-10 14:13 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 2918 bytes --]

Hi all--

I have a (hopefully quick question) about formatting tabular output from
ob-R.  Consider the following block:

--8<---------------cut here---------------start------------->8---
#+source:  anova-example
#+BEGIN_SRC R :cache yes :exports results :colnames yes :results value
library(ez)
library(lme4)

eza <- ezANOVA(dv = .(Reaction),
               sid = .(Subject),
               within = .(Days),
               data = sleepstudy)

print(eza$ANOVA)
#+END_SRC
--8<---------------cut here---------------end--------------->8---

This produces the following output:
--8<---------------cut here---------------start------------->8---
#+results[7e7015e41d95ed8986fb9a211a8b5c6e121ae99d]: anova-example
| Effect | DFn | DFd |             SSn |              SSd |                F |                    p | p<.05 |              pes |
|--------+-----+-----+-----------------+------------------+------------------+----------------------+-------+------------------|
| Days   |   9 | 153 | 166235.12250176 | 151101.038615303 | 18.7026979326383 | 8.99534541600196e-21 | *     | 0.52384550792003 |
--8<---------------cut here---------------end--------------->8---

That's more precision than I want in export, so I add a formula line to
the results, hoping to change the formatting of the floats.  After
hitting C-u C-u C-c C-c in the table, I get the following:

--8<---------------cut here---------------start------------->8---
#+results[7e7015e41d95ed8986fb9a211a8b5c6e121ae99d]: anova-example
| Effect | DFn | DFd |         SSn |         SSd |       F |      p | p<.05 |    pes |
|--------+-----+-----+-------------+-------------+---------+--------+-------+--------|
| Days   |   9 | 153 | 166235.1200 | 151101.0400 | 18.7027 | 0.0000 | *     | 0.5238 |
#+TBLFM: $4=$4;%.4f::$5=$5;%.4f::$6=$6;%.4f::$7=$7;%.4f::$9=$9;%.4f
--8<---------------cut here---------------end--------------->8---

That's much better!  But, if I export this section to LaTeX, the formula
line is not applied and I end up with the full precision in my table.

--8<---------------cut here---------------start------------->8---
\begin{center}
\begin{tabular}{lrrrrrrlr}
 Effect  &  DFn  &  DFd  &              SSn  &               SSd  &                 F  &                     p  &  p<.05  &               pes  \\
\hline
 Days    &    9  &  153  &  166235.12250176  &  151101.038615303  &  18.7026979326383  &  8.99534541600196e-21  &  *      &  0.52384550792003  \\
\end{tabular}
\end{center}
--8<---------------cut here---------------end--------------->8---

Is there a way to make sure that the table formula is applied to the
results block during export?  Alternatively, is there another way to
reduce the precision of the numbers produced during export?

Thanks for any help!
/au

-- 
Austin Frank
http://aufrank.net
GPG Public Key (D7398C2F): http://aufrank.net/personal.asc

[-- Attachment #1.2: Type: application/pgp-signature, Size: 194 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: [babel]  apply #+TABLEFM lines during export?
  2010-07-10 14:13 [babel] apply #+TABLEFM lines during export? Austin Frank
@ 2010-07-11 22:26 ` Eric Schulte
  2010-07-13  3:59   ` Austin Frank
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Schulte @ 2010-07-11 22:26 UTC (permalink / raw)
  To: Austin Frank; +Cc: emacs-orgmode

Hi Austin,

Austin Frank <austin.frank@gmail.com> writes:

> Hi all--
>
> I have a (hopefully quick question) about formatting tabular output from
> ob-R.  Consider the following block:
>
> #+source:  anova-example
> #+BEGIN_SRC R :cache yes :exports results :colnames yes :results value
> library(ez)
> library(lme4)
>
> eza <- ezANOVA(dv = .(Reaction),
>                sid = .(Subject),
>                within = .(Days),
>                data = sleepstudy)
>
> print(eza$ANOVA)
> #+END_SRC
>
> This produces the following output:
> #+results[7e7015e41d95ed8986fb9a211a8b5c6e121ae99d]: anova-example
> | Effect | DFn | DFd |             SSn |              SSd |                F |                    p | p<.05 |              pes |
> |--------+-----+-----+-----------------+------------------+------------------+----------------------+-------+------------------|
> | Days   |   9 | 153 | 166235.12250176 | 151101.038615303 | 18.7026979326383 | 8.99534541600196e-21 | *     | 0.52384550792003 |
>
> That's more precision than I want in export, so I add a formula line to
> the results, hoping to change the formatting of the floats.  After
> hitting C-u C-u C-c C-c in the table, I get the following:
>
> #+results[7e7015e41d95ed8986fb9a211a8b5c6e121ae99d]: anova-example
> | Effect | DFn | DFd |         SSn |         SSd |       F |      p | p<.05 |    pes |
> |--------+-----+-----+-------------+-------------+---------+--------+-------+--------|
> | Days   |   9 | 153 | 166235.1200 | 151101.0400 | 18.7027 | 0.0000 | *     | 0.5238 |
> #+TBLFM: $4=$4;%.4f::$5=$5;%.4f::$6=$6;%.4f::$7=$7;%.4f::$9=$9;%.4f
>
> That's much better!  But, if I export this section to LaTeX, the formula
> line is not applied and I end up with the full precision in my table.
>
> \begin{center}
> \begin{tabular}{lrrrrrrlr}
>  Effect  &  DFn  &  DFd  &              SSn  &               SSd  &                 F  &                     p  &  p<.05  &               pes  \\
> \hline
>  Days    &    9  &  153  &  166235.12250176  &  151101.038615303  &  18.7026979326383  &  8.99534541600196e-21  &  *      &  0.52384550792003  \\
> \end{tabular}
> \end{center}
>
> Is there a way to make sure that the table formula is applied to the
> results block during export?  Alternatively, is there another way to
> reduce the precision of the numbers produced during export?
>

Under the current setup, I don't know of a way to ensure that the
formula will be re-run.  This may be a good place for future
(post-feature-freeze) functionality.  There has also been discussion of
adding a header argument for "post-processing" code blocks which could
accept the output of the evaluated code block as input -- this might be
related.

As a work-around, I would suggest the following emacs-lisp code block.
Export of this code block will trigger the evaluation of the R code
block, and it will then trim the output of said block resulting in the
desired table precision.

--8<---------------cut here---------------start------------->8---
#+begin_src emacs-lisp :var tab=anova-example :colnames yes :cache yes
  (mapcar
   (lambda (row)
     (mapcar
      (lambda (cell) (if (numberp cell) (format "%.4f" cell) cell))
      row))
   tab)
#+end_src

#+results[16ac354f1e7a65594bb59e252ab221e6a4b10f80]:
| Effect |    DFn |      DFd |         SSn |         SSd |       F |                    p | p<.05 |    pes |
|--------+--------+----------+-------------+-------------+---------+----------------------+-------+--------|
| Days   | 9.0000 | 153.0000 | 166235.1225 | 151101.0386 | 18.7027 | 8.99534541600196e-21 | *     | 0.5238 |
--8<---------------cut here---------------end--------------->8---

Cheers -- Eric

>
> Thanks for any help!
> /au

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

* Re: [babel]  apply #+TABLEFM lines during export?
  2010-07-11 22:26 ` Eric Schulte
@ 2010-07-13  3:59   ` Austin Frank
  2010-07-13  5:06     ` Eric Schulte
  0 siblings, 1 reply; 5+ messages in thread
From: Austin Frank @ 2010-07-13  3:59 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 2172 bytes --]

On Sun, Jul 11 2010, Eric Schulte wrote:

> Under the current setup, I don't know of a way to ensure that the
> formula will be re-run.  This may be a good place for future
> (post-feature-freeze) functionality.  There has also been discussion
> of adding a header argument for "post-processing" code blocks which
> could accept the output of the evaluated code block as input -- this
> might be related.

Yes, eager to see where this goes.

> As a work-around, I would suggest the following emacs-lisp code block.
> Export of this code block will trigger the evaluation of the R code
> block, and it will then trim the output of said block resulting in the
> desired table precision.
>
> #+begin_src emacs-lisp :var tab=anova-example :colnames yes :cache yes
>   (mapcar
>    (lambda (row)
>      (mapcar
>       (lambda (cell) (if (numberp cell) (format "%.4f" cell) cell))
>       row))
>    tab)
> #+end_src


I tried to customize your approach to apply to the whole buffer.  This
code seems to do a nice job of changing the appearance of the tables
within the buffer, but the extra precision is still maintained on
export.

--8<---------------cut here---------------start------------->8---
#+source:  format-table-floats
#+BEGIN_SRC emacs-lisp :results silent :exports none
(add-hook 'org-export-preprocess-final-hook
          (lambda ()
            (while (re-search-forward
                    "|?[ ]+\\(-?[0-9]+\\.[0-9]+\\)\\(e-\\)?[ ]+|"
                    nil t)
              (progn
                (replace-match (concat
                                (format "%.4f"
                                        (string-to-number (match-string 1)))
                                " |"))
                (org-table-align)))))
#+END_SRC
--8<---------------cut here---------------end--------------->8---

Can anyone suggest another approach that would change the precision of
floats in all tables in the buffer before export?  Perhaps there's a
different hook I should be using for LaTeX export?

Thanks!
/au

-- 
Austin Frank
http://aufrank.net
GPG Public Key (D7398C2F): http://aufrank.net/personal.asc

[-- Attachment #1.2: Type: application/pgp-signature, Size: 194 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: Re: [babel]  apply #+TABLEFM lines during export?
  2010-07-13  3:59   ` Austin Frank
@ 2010-07-13  5:06     ` Eric Schulte
  2010-07-13 22:56       ` Austin Frank
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Schulte @ 2010-07-13  5:06 UTC (permalink / raw)
  To: Austin Frank; +Cc: emacs-orgmode

Austin Frank <austin.frank@gmail.com> writes:

> On Sun, Jul 11 2010, Eric Schulte wrote:
>
>> Under the current setup, I don't know of a way to ensure that the
>> formula will be re-run.  This may be a good place for future
>> (post-feature-freeze) functionality.  There has also been discussion
>> of adding a header argument for "post-processing" code blocks which
>> could accept the output of the evaluated code block as input -- this
>> might be related.
>
> Yes, eager to see where this goes.
>
>> As a work-around, I would suggest the following emacs-lisp code block.
>> Export of this code block will trigger the evaluation of the R code
>> block, and it will then trim the output of said block resulting in the
>> desired table precision.
>>
>> #+begin_src emacs-lisp :var tab=anova-example :colnames yes :cache yes
>>   (mapcar
>>    (lambda (row)
>>      (mapcar
>>       (lambda (cell) (if (numberp cell) (format "%.4f" cell) cell))
>>       row))
>>    tab)
>> #+end_src
>
>
> I tried to customize your approach to apply to the whole buffer.  This
> code seems to do a nice job of changing the appearance of the tables
> within the buffer, but the extra precision is still maintained on
> export.
>

if you want to use my approach above more widely you could name the code
block say #+source:less-precision, then add :exports none to each of
your existing code blocks that output over-precise data, and for each
such block add a call of the following form

#+call: less-precision(tab=overly-precise-R-block) :exports results

which should work, but would litter your file with these call lines.  I
guess this is an instance where a post-processing function specified at
the file/subtree level could potentially be useful.

Cheers -- Eric

>
> #+source: format-table-floats
> #+BEGIN_SRC emacs-lisp :results silent :exports none
> (add-hook 'org-export-preprocess-final-hook
>           (lambda ()
>             (while (re-search-forward
>                     "|?[ ]+\\(-?[0-9]+\\.[0-9]+\\)\\(e-\\)?[ ]+|"
>                     nil t)
>               (progn
>                 (replace-match (concat
>                                 (format "%.4f"
>                                         (string-to-number (match-string 1)))
>                                 " |"))
>                 (org-table-align)))))
> #+END_SRC
>
> Can anyone suggest another approach that would change the precision of
> floats in all tables in the buffer before export?  Perhaps there's a
> different hook I should be using for LaTeX export?
>
> Thanks!
> /au

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

* Re: [babel]  apply #+TABLEFM lines during export?
  2010-07-13  5:06     ` Eric Schulte
@ 2010-07-13 22:56       ` Austin Frank
  0 siblings, 0 replies; 5+ messages in thread
From: Austin Frank @ 2010-07-13 22:56 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 1122 bytes --]

On Tue, Jul 13 2010, Eric Schulte wrote:

> if you want to use my approach above more widely you could name the code
> block say #+source:less-precision, then add :exports none to each of
> your existing code blocks that output over-precise data, and for each
> such block add a call of the following form
>
> #+call: less-precision(tab=overly-precise-R-block) :exports results
>
> which should work, but would litter your file with these call lines.  I
> guess this is an instance where a post-processing function specified at
> the file/subtree level could potentially be useful.

Yes, the post-processing approach really seems appropriate here--
looking forward to seeing what you come up with.

In the mean time, is there definitely not any hook that applies

 a) during export, and
 b) after results are generated from source blocks?

If not, is it possible that adding a hook like

 org-post-execution-pre-export-hook

would be a reasonable suggestion?  Carsten, thoughts?

Thanks,
/au

-- 
Austin Frank
http://aufrank.net
GPG Public Key (D7398C2F): http://aufrank.net/personal.asc

[-- Attachment #1.2: Type: application/pgp-signature, Size: 194 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

end of thread, other threads:[~2010-07-13 22:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-10 14:13 [babel] apply #+TABLEFM lines during export? Austin Frank
2010-07-11 22:26 ` Eric Schulte
2010-07-13  3:59   ` Austin Frank
2010-07-13  5:06     ` Eric Schulte
2010-07-13 22:56       ` Austin Frank

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