emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Number format for table results outut from R data.frame/tibble
@ 2021-08-06 15:38 John Hendy
  2021-08-06 19:30 ` Berry, Charles via General discussions about Org-mode.
  0 siblings, 1 reply; 5+ messages in thread
From: John Hendy @ 2021-08-06 15:38 UTC (permalink / raw)
  To: emacs-orgmode

Greetings,

I'm wondering how to align the results from the R buffer (which I
like) vs. the results printed by Org-mode for table results. Here's a
toy example:

#+begin_src R :session foo :output results :type table :results value
library(tibble)

tmp <- tibble(x=1:5, y=x/pi)
tmp
#+end_src

In org, I get this:

#+RESULTS:
| 1 | 0.318309886183791 |
| 2 | 0.636619772367581 |
| 3 | 0.954929658551372 |
| 4 |  1.27323954473516 |
| 5 |  1.59154943091895 |

In the buffer, I get this:
      x     y
  <int> <dbl>
1     1 0.318
2     2 0.637
3     3 0.955
4     4 1.27
5     5 1.59

From the tibble side, R points one to the pillar package[1], which
indeed affects the output in the buffer, but not in the Org results.

#+begin_src R :session foo :output results :type table :results value
library(tibble)
library(pillar)

options(pillar.sigfig=1)
tmp <- tibble(x=1:5, y=x/pi)
tmp
#+end_src

# A tibble: 5 × 2
      x     y
  <int> <dbl>
1     1   0.3
2     2   0.6
3     3   1.
4     4   1.
5     5   2.

The last example here touches on accomplish this[2]... but is the only
method to write an elisp block that post-process my R results? It
seems so enticing that R can output the correct format... can Org
latch onto the exact output somehow?

I also discovered the round_df package[3], so I could bring that into
the mix and round flexibly, it seems. I don't *really* want to round,
though, I just want more digestible output.

Many thanks for any pointers, I'm suspecting the answer is before my
eyes, but I haven't figured out the proper incantation of :results,
:type, :wrap, etc...


Best regards,
John


[1] https://pillar.r-lib.org/articles/digits.html
[2] https://orgmode.org/manual/Results-of-Evaluation.html
[3] https://rdrr.io/cran/forestmangr/man/round_df.html


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

* Re: Number format for table results outut from R data.frame/tibble
  2021-08-06 15:38 Number format for table results outut from R data.frame/tibble John Hendy
@ 2021-08-06 19:30 ` Berry, Charles via General discussions about Org-mode.
  2021-08-21 16:58   ` John Hendy
  0 siblings, 1 reply; 5+ messages in thread
From: Berry, Charles via General discussions about Org-mode. @ 2021-08-06 19:30 UTC (permalink / raw)
  To: John Hendy; +Cc: emacs-orgmode

John,

> On Aug 6, 2021, at 8:38 AM, John Hendy <jw.hendy@gmail.com> wrote:
> 
> Greetings,
> 
> I'm wondering how to align the results from the R buffer (which I
> like) vs. the results printed by Org-mode for table results. Here's a
> toy example:
> 

`tbl_df' objects come with their own print/show method. And it does things like add color and text formats. This is nice in a terminal, but not when you want to display them in org.

Turning off the color might be enough to give you a usable result:

#+begin_src R :session foo :results output drawer
library(tibble)
options( cli.num_colors=1 )
tmp <- tibble(x=1:5, y=x/pi)
tmp
#+end_src

#+RESULTS:
:results:

# A tibble: 5 x 2
      x     y
  <int> <dbl>
1     1   0.3
2     2   0.6
3     3   1. 
4     4   1. 
5     5   2.
:end:


If this is not close to what you need, I suggest writing your own formatting function. If you have limited emacs-lisp skill, I suggest doing this in R.

HTH,

Chuck


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

* Re: Number format for table results outut from R data.frame/tibble
  2021-08-06 19:30 ` Berry, Charles via General discussions about Org-mode.
@ 2021-08-21 16:58   ` John Hendy
  2021-08-21 17:39     ` Berry, Charles via General discussions about Org-mode.
  0 siblings, 1 reply; 5+ messages in thread
From: John Hendy @ 2021-08-21 16:58 UTC (permalink / raw)
  To: Berry, Charles; +Cc: emacs-orgmode

On Fri, Aug 6, 2021 at 2:30 PM Berry, Charles <ccberry@health.ucsd.edu> wrote:
>
> John,
>
> > On Aug 6, 2021, at 8:38 AM, John Hendy <jw.hendy@gmail.com> wrote:
> >
> > Greetings,
> >
> > I'm wondering how to align the results from the R buffer (which I
> > like) vs. the results printed by Org-mode for table results. Here's a
> > toy example:
> >
>
> `tbl_df' objects come with their own print/show method. And it does things like add color and text formats. This is nice in a terminal, but not when you want to display them in org.
>
> Turning off the color might be enough to give you a usable result:
>
> #+begin_src R :session foo :results output drawer
> library(tibble)
> options( cli.num_colors=1 )
> tmp <- tibble(x=1:5, y=x/pi)
> tmp
> #+end_src
>
> #+RESULTS:
> :results:
>
> # A tibble: 5 x 2
>       x     y
>   <int> <dbl>
> 1     1   0.3
> 2     2   0.6
> 3     3   1.
> 4     4   1.
> 5     5   2.
> :end:
>

Interesting, and thanks for taking a look. I don't think I specified
sufficiently that I'm actually aiming for latex/pdf output and
therefore want the org table. I can just add my own header row and get
booktabs formatting free upon export. If I keep with :results output,
the color options trick does remove a bunch of garbage from the
output... but :results value still yields:

#+RESULTS:
| 1 | 0.318309886183791 |
| 2 | 0.636619772367581 |
| 3 | 0.954929658551372 |
| 4 |  1.27323954473516 |
| 5 |  1.59154943091895 |

I was hoping there was a way to either (a) have org post-format or (b)
take the results as printed. I'm not sure on the exact mechanism used
which creates the mismatched output in the session vs. the table. I
get that behind the scenes I still have long, irrational numbers
here... it's just that if org can print them with x decimal places, is
there some way to get Org to intercept *those* values?

John

>
> If this is not close to what you need, I suggest writing your own formatting function. If you have limited emacs-lisp skill, I suggest doing this in R.
>
> HTH,
>
> Chuck


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

* Re: Number format for table results outut from R data.frame/tibble
  2021-08-21 16:58   ` John Hendy
@ 2021-08-21 17:39     ` Berry, Charles via General discussions about Org-mode.
  2021-08-21 19:55       ` Jeremie Juste
  0 siblings, 1 reply; 5+ messages in thread
From: Berry, Charles via General discussions about Org-mode. @ 2021-08-21 17:39 UTC (permalink / raw)
  To: John Hendy; +Cc: emacs-orgmode



> On Aug 21, 2021, at 9:58 AM, John Hendy <jw.hendy@gmail.com> wrote:
> 
> 
> Interesting, and thanks for taking a look. I don't think I specified
> sufficiently that I'm actually aiming for latex/pdf output and
> therefore want the org table. 

John,

If you want a latex export, you should use one of the many R packages[1] that format R objects for latex output.

For example:

#+begin_src R :session foo :results output latex
library(tibble)
library(xtable)
tmp <- tibble(x=1:5, y=x/pi)
xtable(tmp)
#+end_src

#+RESULTS:
#+begin_export latex

% latex table generated in R 4.1.0 by xtable 1.8-4 package
% Sat Aug 21 10:23:18 2021
\begin{table}[ht]
\centering
\begin{tabular}{rrr}
  \hline
 & x & y \\ 
  \hline
1 &   1 & 0.32 \\ 
  2 &   2 & 0.64 \\ 
  3 &   3 & 0.95 \\ 
  4 &   4 & 1.27 \\ 
  5 &   5 & 1.59 \\ 
   \hline
\end{tabular}
\end{table}
#+end_export

The xtable() and its print method have loads of options for formatting, setting environments and labels, and adding captions.

If you insist on doing this in org without the assistance of an R package, you will need to use a `:post' header argument.  If you have solid elisp skills, this is a viable option.

HTH,
Chuck

[1] One list of such is at: https://stackoverflow.com/questions/5465314/tools-for-making-latex-tables-in-r


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

* Re: Number format for table results outut from R data.frame/tibble
  2021-08-21 17:39     ` Berry, Charles via General discussions about Org-mode.
@ 2021-08-21 19:55       ` Jeremie Juste
  0 siblings, 0 replies; 5+ messages in thread
From: Jeremie Juste @ 2021-08-21 19:55 UTC (permalink / raw)
  To: Berry, Charles via General discussions about Org-mode.; +Cc: Berry, Charles


Hello, John

For my workflow, I generally use the minimum from xtable and do the
formatting in org-mode using the :post parameter to call another lisp code
block.


That said, as Chuck said, you can go a long way with xtable. In my
attic, I found the following code where I take only the core data from
xtable and do formatting in org-mode. This code may be a little
extravagant on the latex side but, it shows the possibilities.

HTH,

Jeremie

#+LATEX_HEADER: \usepackage{booktabs}
#+LATEX_HEADER: \usepackage{array}
#+LATEX_HEADER: \usepackage{setspace}
#+LATEX_HEADER: \usepackage{dcolumn}
#+LATEX_HEADER: \usepackage{array}
#+LATEX_HEADER: \usepackage{setspace}
#+LATEX_HEADER:\usepackage{longtable,tabularx,ltablex}
#+LATEX_HEADER:\usepackage{siunitx}
#+LATEX_HEADER:\usepackage[flushleft]{threeparttablex}


#+Name: add-table-env
#+BEGIN_SRC emacs-lisp :var  table="TABLE" notes="NOTES" caption="CAPTION" :results silent :exports none
(replace-regexp-in-string "\\\\begin{tabular}.*\\S.*\\toprule\\|\\\\end{tabular}" "" 
(format "\\scriptsize{
\\begin{center}
\\begin{TableNotes}\\footnotesize
 \\item[\\hspace{-\\fontdimen2\\font}]   
%s
\\end{TableNotes}
\\begin{ThreePartTable}
     \\sisetup{table-format=-2.3, table-space-text-post=***, table-number-alignment=center}
\\keepXColumns
\\begin{tabularx}{\\textwidth}{l *{3}{D..{5.3}}}
\\caption{%s} \\\\
\\toprule
\\toprule
\\multicolumn{1}{c}{colnum 1} &\\multicolumn{1}{c}{colnum 2}   \\\\
  \\cmidrule(lr){1-1}  \\cmidrule(lr){2-2}  

%s
\\insertTableNotes
\\end{tabularx}
\\end{ThreePartTable}
\\end{center}
}
\\pagebreak
" notes caption table))
 #+END_SRC




#+begin_src R :session foo :results output latex :post add-table-env(*this*, "Some note", "A caption" ) :exports results
  library(tibble)
  library(xtable)
  tmp <- tibble(x=1:5, y=x/pi)
  print.xtable(xtable(tmp),
	       include.colnames=FALSE,include.rownames=FALSE,floating=FALSE,
	       comment = FALSE,dcolumn=TRUE,booktabs=TRUE
	      ,sanitize.text.function = function(x) {
		x[x=="NA"] <-""
		x})
#+end_src



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

end of thread, other threads:[~2021-08-21 19:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-06 15:38 Number format for table results outut from R data.frame/tibble John Hendy
2021-08-06 19:30 ` Berry, Charles via General discussions about Org-mode.
2021-08-21 16:58   ` John Hendy
2021-08-21 17:39     ` Berry, Charles via General discussions about Org-mode.
2021-08-21 19:55       ` Jeremie Juste

Code repositories for project(s) associated with this 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).