Re: org babel output

2023-07-03 Thread Matt


  On Sat, 01 Jul 2023 05:52:42 +0200  Robert Synofzik  wrote --- 
 > Hello,
 > the exact same error occurs for me: 
 > https://emacs.stackexchange.com/questions/72243/macos-org-babel-python-with-session-output-give-python-el-eval
 >
 > do you possibly have any idea on how to fix this?
 
Just to check, because you didn't explicitly say it in your message, does the 
solution given in the StackExchange not work for you?  That is, do you get a 
different result when you name the session?  What happens when you run the 
following?

#+begin_src python :session my-named-session :results output
print("yes")
#+end_src



Re: org babel output

2023-07-03 Thread Ihor Radchenko
Robert Synofzik  writes:

> the exact same error occurs for me:
> https://emacs.stackexchange.com/questions/72243/macos-org-babel-python-with-session-output-give-python-el-eval
>
> I use python 3.9 so guessing switching the version won't help.

May you share your Org version as well?
If it is not the latest, may you try reproducing with the latest ELPA
Org version and with the latest development version?

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



org babel output

2023-07-03 Thread Robert Synofzik
Hello,

the exact same error occurs for me:
https://emacs.stackexchange.com/questions/72243/macos-org-babel-python-with-session-output-give-python-el-eval

I use python 3.9 so guessing switching the version won't help.

#+begin_src python :session :results output

print("yes")

#+End_src

#+Results:
: __PYTHON_EL_eval("try:\nwith
open('/var/folders/16/snlwm5z17zz1b1x09vvvrdg04v6pgk/T/babel-hWQZ8X/python-sPLVoL')
as __org_babel_python_tmpfile:\n
 exec(compile(__org_babel_python_tmpfile.read(), __
org_babel_python_tmpfile.name, 'exec'))\nexcept:\nraise\nfinally:\n
 print('


do you possibly have any idea on how to fix this?

Sorry to bother you and thanks for the attention!

Robert


Re: How to convert an HTML export bloc to a normal Org Babel output ?

2023-05-25 Thread Ihor Radchenko
copropriete27ruemo...@gmail.com writes:

> [ For some reason, an answer posted yesterday seems to have been lost. 
> Rewriting/reposting... ]

You did not CC Org mailing list.

> Nice answer ! I suppose that this is more a misdesign than a bug (limited 
> goals of Dyalog,that emacs-jupyter cannot alleviate).
>
> Your suggestion worked. But now, I have to pass to my `:post` function 
> various arguments that a "normal" source block handler should have done. Is 
> there a way for the `post` function to access the (say..) `:results`, 
> `:file`, etc... arguments of the source block ?

`org-babel-current-src-block-location' holds the location of the current
src block being executed. You can use it to determine header arguments
of your src block.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: How to convert an HTML export bloc to a normal Org Babel output ?

2023-05-25 Thread copropriete27ruemoret
[ For some reason, an answer posted yesterday seems to have been lost. 
Rewriting/reposting... ]

Nice answer ! I suppose that this is more a misdesign than a bug (limited goals 
of Dyalog,that emacs-jupyter cannot alleviate).

Your suggestion worked. But now, I have to pass to my `:post` function various 
arguments that a "normal" source block handler should have done. Is there a way 
for the `post` function to access the (say..) `:results`, `:file`, etc... 
arguments of the source block ?

Sincerely,

--
Emmanuel Charpentier



Re: How to convert an HTML export bloc to a normal Org Babel output ?

2023-05-24 Thread Ihor Radchenko
Emmanuel Charpentier  writes:

> #+begin_src jupyter-apl :exports both :results raw :post 
> html2text(data=*this*)
>   ⍳7
> #+end_src
>
> #+RESULTS:
> #+begin_export html 
>
> 1 2 3 4 5 6 7
> #+end_export 
>
> but what /enrages/ me is that the (correct) result is still wrapped in an 
> HTML block ; again, I have been
> unable to obtain a "normal" output.

Check if jupyter-apl has default :wrap header arg. If not, it directly
adds #+begin/end_export lines and the best you can do is (1) report it
as a bug; (2) manually filter out begin/end_export in your :post function.


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



How to convert an HTML export bloc to a normal Org Babel output ?

2023-05-24 Thread Emmanuel Charpentier
Dear list,

I stumbled on the conversion of an HTML Babel export block to a "normal" Babel 
output. What follows is the *verbatim* transcript of an org file explaining and 
demonstrating the problem :

```
# Attempt to use emacs-jupyter to insert Dyalog APL org-src blocks. 
#+options: author:nil toc:nil
#+latex_compiler: lualatex
#+latex_header: \usepackage{minted}
#+latex_header: \usepackage{fontspec}
#+latex_header: \setmonofont{Unifont}

#+property: header-args:jupyter-apl :kernel dyalog-kernel :session pa

I want to use APL `src` blocks in an Org document. This can be done by using
[[https://www.dyalog.com/][Dyalog APL]] interpreter though
[[https://github.com/emacs-jupyter/jupyter][jupyter-emacs]] and the Dyalog
[[https://github.com/Dyalog/dyalog-jupyter-kernel][jupyter kernel]].

This apparently works :


#+begin_src jupyter-apl :exports both :results replace
  ⍳7
#+end_src

#+RESULTS:
#+begin_export html
1 2 3 4 5 6 7

#+end_export

But, no matter what I try to use as parameters, Dyalog's kernel returns an HTML 
block, which exports to HTML
successfully (of course) but /not/ to LaTeX nor `.odt`/`docx` or even `Unicode 
text`. A glimpse at the
kernel's source code (partially in APL, beware !) suggests that's a /feature/ 
of this kernel, which returns
either HTML or graphics (which I didn't (yet) attempt to use).

I then attempted to work around this (mis)feature by converting the HTML block 
to text, using this
Stackoverflow 
[[https://stackoverflow.com/questions/64796235/html-to-text-conversion-in-emacs][suggestion]]
 :

#+name: html2text
#+begin_src emacs-lisp :var data="" :exports code :results silent
  (with-temp-buffer
(insert data)
(shr-render-region (point-min) (point-max))
(buffer-substring-no-properties (point-min) (point-max)))
#+end_src

#+RESULTS: html2text

The conversion works fine :

#+begin_src jupyter-apl :exports both :results raw :post html2text(data=*this*)
  ⍳7
#+end_src

#+RESULTS:
#+begin_export html 

1 2 3 4 5 6 7
#+end_export 

but what /enrages/ me is that the (correct) result is still wrapped in an HTML 
block ; again, I have been
unable to obtain a "normal" output.

Can someone suggest a way to transform an HTML export block to a normal output ?

```

Source file and various exports enclosed (HTML is correct, the other ones not).

The question still holds...


DyalogAttempt.html
Description: application/xhtml


DyalogAttempt.odt
Description: application/vnd.oasis.opendocument.text
# Attempt to use emacs-jupyter to insert Dyalog APL org-src blocks. 
#+options: author:nil toc:nil
#+latex_compiler: lualatex
#+latex_header: \usepackage{minted}
#+latex_header: \usepackage{fontspec}
#+latex_header: \setmonofont{Unifont}

#+property: header-args:jupyter-apl :kernel dyalog-kernel :session pa

I want to use APL `src` blocks in an Org document. This can be done by using [[https://www.dyalog.com/][Dyalog APL]] interpreter though [[https://github.com/emacs-jupyter/jupyter][jupyter-emacs]] and the Dyalog [[https://github.com/Dyalog/dyalog-jupyter-kernel][jupyter kernel]].

This apparently works :


#+begin_src jupyter-apl :exports both :results replace
  ⍳7
#+end_src

#+RESULTS:
#+begin_export html
1 2 3 4 5 6 7

#+end_export

But, no matter what I try to use as parameters, Dyalog's kernel returns an HTML block, which exports to HTML successfully (of course) but /not/ to LaTeX nor `.odt`/`docx` or even `Unicode text`. A glimpse at the kernel's source code (partially in APL, beware !) suggests that's a /feature/ of this kernel, which returns either HTML or graphics (which I didn't (yet) attempt to use).

I then attempted to work around this (mis)feature by converting the HTML block to text, using this Stackoverflow [[https://stackoverflow.com/questions/64796235/html-to-text-conversion-in-emacs][suggestion]] :

#+name: html2text
#+begin_src emacs-lisp :var data="" :exports code :results silent
  (with-temp-buffer
(insert data)
(shr-render-region (point-min) (point-max))
(buffer-substring-no-properties (point-min) (point-max)))
#+end_src

#+RESULTS: html2text

The conversion works fine :

#+begin_src jupyter-apl :exports both :results raw :post html2text(data=*this*)
  ⍳7
#+end_src

#+RESULTS:
#+begin_export html 

1 2 3 4 5 6 7
#+end_export 

but what /enrages/ me is that the (correct) result is still wrapped in an HTML block ; again, I have been unable to obtain a "normal" output.

Can someone suggest a way to transform an HTML export block to a normal output ?


DyalogAttempt.pdf
Description: Adobe PDF document
% Created 2023-05-24 mer. 11:50
% Intended LaTeX compiler: lualatex
\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{minted}
\usepackage{fontspec}
\setmonofont{Unifont}
\date{\today}
\title{}
\hypersetup{
 pdfauthor={Emmanuel Charpentier},
 pdftitle={},
 

Bug: Org-babel output malformed for MATLAB source blocks running in a session [9.3 (release_9.3 @ /usr/share/emacs/27.1/lisp/org/)]

2020-11-23 Thread Karthik Chikmagalur

Remember to cover the basics, that is, what you expected to happen and
what in fact did happen.  You don't know how to make a good report?  See

 https://orgmode.org/manual/Feedback.html#Feedback

Your bug report will be posted to the Org mailing list.

Org-babel's handling of MATLAB block output with the =:results session= 
argument is broken. Here is some sample code along with the expected result:

#+begin_src matlab :results output :session *MATLAB*
a = 3;
b = 4;
c = a + b
#+end_src

#+RESULTS:
: c =
: 
:  7

However here is the actual result:

#+begin_src matlab :results output :session *MATLAB*
a = 3;
b = 4;
c = a + b
#+end_src

#+RESULTS:
#+begin_example
a = 3;
b = 4;
c = a + b

c =

 7
'org_babel_eoe'

ans =

'org_babel_eoe'
#+end_example

There are two separate problems:

1. The =org-babel-octave-eoe-indicator= is not being stripped.
2. The comint input is being echoed in the output.

#1 is easy to fix. The problem is in the function 
=org-babel-octave-evaluate-session=, in the line:

 #+begin_src emacs-lisp
 (cdr (reverse (delq "" (mapcar #'org-strip-quotes
(mapcar #'org-trim raw)
 #+end_src

Where empty lines in the output are being removed with =delq=. =delq= compares 
with =eq= instead of =equal=, which fails on blank lines. Replacing this with 
=delete= works fine.

#2 is a much trickier problem to solve. Here is the body of the input as it 
appears in the MATLAB session:

#+begin_example
>> a = 3;
b = 4;
c = a + b
'org_babel_eoe'
a = 3;
>> b = 4;
>> c = a + b

c =

 7

>> 'org_babel_eoe'

ans =

'org_babel_eoe'

>> 
#+end_example

Here `>>' is the shell prompt, which is absent from the raw comint output as 
given to org by comint-mode. =matlab-shell= does not work like other comint 
shells in that it doesn't echo bulk (/i.e./ multi-line) input all at once. 
Instead, it echoes each line of input and follows it with its output. This 
interspersal of input and output is causing =org-babel-comint-with-output=, the 
function (actually macro) responsible for removing the comint input echo text 
from the raw comint output, to fail. This macro assumes that the raw comint 
output looks like 
#+begin_example
<>

<>
#+end_example

as you can see from this code from =org-babel-comint-with-output=:
#+begin_src emacs-lisp
 (when (and ,remove-echo ,full-body
(string-match
 (replace-regexp-in-string
  "\n" "[\r\n]+" (regexp-quote (or ,full-body "")))
 string-buffer))
   (setq string-buffer (substring string-buffer (match-end 0
#+end_src

To fix this, either =org-babel-octave-evaluate-session= or 
=org-babel-comint-with-output= needs to be modified. This problem is local to 
MATLAB, it does not happen with GNU Octave, which shares most of its org-babel 
code with MATLAB's. So I wrote a patch to the former that does additional 
line-by-line processing on the raw comint output to detect and remove the 
echoed input. (Patch is attached.) However while this fixes the problem it's 
not a robust solution.

-Karthik

--- ob-octave.el	2020-11-23 11:22:01.473682045 -0800
+++ ob-octave-new.el	2020-11-23 11:10:07.961900383 -0800
@@ -187,6 +187,7 @@
 			(org-babel-process-file-name tmp-file 'noquote)))
 	   (org-babel-octave-import-elisp-from-file tmp-file))
 
+
 (defun org-babel-octave-evaluate-session
 (session body result-type  matlabp)
   "Evaluate BODY in SESSION."
@@ -237,12 +238,31 @@
   (`output
(setq results
 	 (if matlabp
-		 (cdr (reverse (delq "" (mapcar #'org-strip-quotes
+		 (cdr (reverse (delete "" (mapcar #'org-strip-quotes
 		(mapcar #'org-trim raw)
 	   (cdr (member org-babel-octave-eoe-output
 			(reverse (mapcar #'org-strip-quotes
 	 (mapcar #'org-trim raw)))
-   (mapconcat #'identity (reverse results) "\n")
+   ;; This kludge is to remove the input lines from the output. Because of
+   ;; the special way that MATLAB processes bulk comint output (the output
+   ;; of each line follows that line) the macro
+   ;; `org-babel-comint-with-output' cannot remove the echoed commands. The
+   ;; following handles this manually, by splitting both the original input
+   ;; (`BODY') and full output (`RESULTS') on newlines, comparing them line
+   ;; by line and removing all lines in BODY from RESULTS. Note that RESULTS
+   ;; is already a list of strings so additional care is needed.
+   (let* ((body-lines (split-string body "\n+"))
+  (result-lines (flatten-list
+ (mapcar
+  (lambda (entry) (reverse (split-string entry "\n")))
+  results
+ (mapconcat
+  #'identity
+  (reverse (cl-remove-if
+(lambda (line) (member line 

Re: [O] suppress leading ":" in org-babel output

2019-05-31 Thread Jeremie Juste
Hello,

Many thanks updating org-mode resolved the issue

Best regards,
Jeremie
> FWIW, I just get the last line:
>
> #+NAME:mean_purchase_per_shopping_trip
>
> #+BEGIN_SRC R :var x=6 :results output :session *R*
> a <-sprintf("%.2f",12.234324)
> cat(a,sep="\n")
> #+END_SRC
>
> #+RESULTS:
> : 12.23
>
> This is with 
>
>GNU Emacs 27.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 2.24.32) of 
> 2019-05-01
>Org mode version 9.2.3 (release_9.2.3-367-gd79e80 @ 
> /home/nick/elisp/org-mode/lisp/)
>R version 3.5.3 (2019-03-11) -- "Great Truth"
>
> on Fedora 29.



Re: [O] suppress leading ":" in org-babel output

2019-05-29 Thread Nick Dokos
"Berry, Charles"  writes:

>> On May 27, 2019, at 7:17 PM, Nick Dokos  wrote:
>> 
>> Jeremie Juste  writes:
>> 
>>> Hello,
>>> 
>>> I recall seeing this thread on the mailing list but I cannot find it
>>> back.
>>> 
>>> I would like to suppress the first line leading ":" in the following output
>>> 
>>> 
>>> #+NAME:mean_purchase_per_shopping_trip
>>> 
>>> #+BEGIN_SRC R :var x=6 :results output :session *R*
>>> a <-sprintf("%.2f",12.234324)
>>> cat(a,sep="\n")
>>> #+END_SRC
>>> 
>>> #+RESULTS: mean_purchase_per_shopping_trip
>>> : 
>>> 
>>> : 12.23
>>> 
>> 
>> FWIW, I just get the last line:
>> 
>> #+NAME:mean_purchase_per_shopping_trip
>> 
>> #+BEGIN_SRC R :var x=6 :results output :session *R*
>> a <-sprintf("%.2f",12.234324)
>> cat(a,sep="\n")
>> #+END_SRC
>> 
>> #+RESULTS:
>> : 12.23
>> 
>> This is with 
>> 
>>   GNU Emacs 27.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 2.24.32) of 
>> 2019-05-01
>>   Org mode version 9.2.3 (release_9.2.3-367-gd79e80 @ 
>> /home/nick/elisp/org-mode/lisp/)
>>   R version 3.5.3 (2019-03-11) -- "Great Truth"
>> 
>> on Fedora 29.
>> 
>
> I get what Jeremie reports (but he has a blank line inserted after the first 
> line, IIUC)
>
> This is with:
>
> GNU Emacs 26.1 
> Org 9.2.3
> ess-version: 18.10.3
> R version 3.6.0 (2019-04-26)
>
> I believe this is new-ish behavior. I see some older files without the
> extra line. Also, I think there are recent changes in ESS that pertain
> to comint sessions.
>
> FWIW, here is a clip from my session:
>
>> x <- 6L
> a <-sprintf("%.2f",12.234324)
> cat(a,sep="\n")
> 'org_babel_R_eoe'
>> > 
> 12.23
>> 
> [1] "org_babel_R_eoe"
>
>
> If Nick sees something different maybe a change in ESS is the culprit.
>

Here's what I see in the session buffer:

,
| > > if (identical(getOption('pager'), file.path(R.home('bin'), 'pager'))) 
options(pager='cat') # rather take the ESS one 
| > options(STERM='iESS', str.dendrogram.last="'", editor='emacsclient', 
show.error.locations=TRUE)
| > x <- 6L
| a <-sprintf("%.2f",12.234324)
| cat(a,sep="\n")
| 'org_babel_R_eoe'
| x <- 6L
| > a <-sprintf("%.2f",12.234324)
| > cat(a,sep="\n")
| 12.23
| > 'org_babel_R_eoe'
| [1] "org_babel_R_eoe"
| > 
`

-- 
Nick

"There are only two hard problems in computer science: cache
invalidation, naming things, and off-by-one errors." -Martin Fowler




Re: [O] suppress leading ":" in org-babel output

2019-05-28 Thread Berry, Charles



> On May 27, 2019, at 7:17 PM, Nick Dokos  wrote:
> 
> Jeremie Juste  writes:
> 
>> Hello,
>> 
>> I recall seeing this thread on the mailing list but I cannot find it
>> back.
>> 
>> I would like to suppress the first line leading ":" in the following output
>> 
>> 
>> #+NAME:mean_purchase_per_shopping_trip
>> 
>> #+BEGIN_SRC R :var x=6 :results output :session *R*
>> a <-sprintf("%.2f",12.234324)
>> cat(a,sep="\n")
>> #+END_SRC
>> 
>> #+RESULTS: mean_purchase_per_shopping_trip
>> : 
>> 
>> : 12.23
>> 
> 
> FWIW, I just get the last line:
> 
> #+NAME:mean_purchase_per_shopping_trip
> 
> #+BEGIN_SRC R :var x=6 :results output :session *R*
> a <-sprintf("%.2f",12.234324)
> cat(a,sep="\n")
> #+END_SRC
> 
> #+RESULTS:
> : 12.23
> 
> This is with 
> 
>   GNU Emacs 27.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 2.24.32) of 
> 2019-05-01
>   Org mode version 9.2.3 (release_9.2.3-367-gd79e80 @ 
> /home/nick/elisp/org-mode/lisp/)
>   R version 3.5.3 (2019-03-11) -- "Great Truth"
> 
> on Fedora 29.
> 

I get what Jeremie reports (but he has a blank line inserted after the first 
line, IIUC)

This is with:

GNU Emacs 26.1 
Org 9.2.3
ess-version: 18.10.3
R version 3.6.0 (2019-04-26)

I believe this is new-ish behavior. I see some older files without the extra 
line. Also, I think there are recent changes in ESS that pertain to comint 
sessions.

FWIW, here is a clip from my session:

--8<---cut here---start->8---
> x <- 6L
a <-sprintf("%.2f",12.234324)
cat(a,sep="\n")
'org_babel_R_eoe'
> > 
12.23
> 
[1] "org_babel_R_eoe"
--8<---cut here---end--->8---


If Nick sees something different maybe a change in ESS is the culprit.

Not sure what the right path forward is ob-R.el, but as a workaround the OP can 
use a :post header arg to strip the offending line.

HTH,

Chuck







Re: [O] suppress leading ":" in org-babel output

2019-05-27 Thread Nick Dokos
Jeremie Juste  writes:

> Hello,
>
> I recall seeing this thread on the mailing list but I cannot find it
> back.
>
> I would like to suppress the first line leading ":" in the following output
>
>
> #+NAME:mean_purchase_per_shopping_trip
>
> #+BEGIN_SRC R :var x=6 :results output :session *R*
> a <-sprintf("%.2f",12.234324)
> cat(a,sep="\n")
> #+END_SRC
>
> #+RESULTS: mean_purchase_per_shopping_trip
> : 
>
> : 12.23
>

FWIW, I just get the last line:

#+NAME:mean_purchase_per_shopping_trip

#+BEGIN_SRC R :var x=6 :results output :session *R*
a <-sprintf("%.2f",12.234324)
cat(a,sep="\n")
#+END_SRC

#+RESULTS:
: 12.23

This is with 

   GNU Emacs 27.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 2.24.32) of 
2019-05-01
   Org mode version 9.2.3 (release_9.2.3-367-gd79e80 @ 
/home/nick/elisp/org-mode/lisp/)
   R version 3.5.3 (2019-03-11) -- "Great Truth"

on Fedora 29.

-- 
Nick

"There are only two hard problems in computer science: cache
invalidation, naming things, and off-by-one errors." -Martin Fowler




[O] suppress leading ":" in org-babel output

2019-05-27 Thread Jeremie Juste
Hello,

I recall seeing this thread on the mailing list but I cannot find it
back.

I would like to suppress the first line leading ":" in the following output


#+NAME:mean_purchase_per_shopping_trip
#+BEGIN_SRC R :var x=6 :results output :session *R*
a <-sprintf("%.2f",12.234324)
cat(a,sep="\n")
#+END_SRC

#+RESULTS: mean_purchase_per_shopping_trip
: 
: 12.23


Best regards,
Jeremie

Org mode version 9.2.1 (9.2.1-8-g1b1797-elpaplus @
/home/djj/.emacs.d/elpa/org-plus-contrib-20190211/)
GNU Emacs 26.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.11) of 
2019-05-16



Re: [O] formatting org-babel output

2013-05-22 Thread Jay Kerns
Hi Joe,

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



Re: [O] formatting org-babel output

2013-05-22 Thread Eric Schulte
Joe Bogner joebog...@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.

#+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
  1.55a
  23000   b
  3000c
  40004   e
  50003
  EOF
#+end_src

#+RESULTS:
| 1.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
  1.55a
  23000   b
  3000c
  40004   e
  50003
  EOF
#+end_src

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

Cheers,

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


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 schulte.e...@gmail.com wrote:
 Joe Bogner joebog...@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