11

I would like that all numbers generated by my knitr codes don´t look like an oldfashioned calculator.

enter image description here

Is there any option to get the numbers like the last one (with ·10 instead of e or E) ?

options(scipen=...) doesn't seem to have that option.

I've been searching information and I've found that it can be done directly in LaTex with the package siunitx, writing every number like this \num{1e-10}

But I'd like knitr did it automatically for all numbers, including those within tables.

PD: And how can I avoid that [1] when I print something?

PD2: Maybe something with gsub?

PD3:
I'm coming back to this problem. Imagine I don't define my own table but I get it from a regression and use xtable to produce it.

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\sisetup{     group-minimum-digits = {3},    group-separator = {,}, exponent-product = \cdot     }
\begin{document}

<<r, results='asis'>>=

library(xtable)
data(tli)
fm2 <- lm(tlimth ~ sex*ethnicty, data = tli)
xxx <- xtable(fm2)
print(xxx, booktabs = TRUE)

@
\end{document}

But it doesn't work well. What options should I use?

This is the result just with print enter image description here

And this is the result with print+"booktabs=T"+my function beauty(). enter image description here regards.

I don't know why it produces two tables instead of 1. And the numbers are not properly aligned. Anyway, I would like not to depend on my beauty() function but just use suintx, how can I do it?

skan
  • 7,423
  • 14
  • 59
  • 96

3 Answers3

9
---
output: pdf_document
---


```{r, results='asis'}
x <- 6.22e-21

cat(x)

cat(sfsmisc::pretty10exp(x, lab.type = 'latex')[[1]])
```

enter image description here

rawr
  • 20,481
  • 4
  • 44
  • 78
  • How do you apply it to a whole table o document? – skan Apr 02 '16 at 23:32
  • Imagine you got a table from a regression xtable(lm(...)). How would you apply your method with to that table? – skan Apr 03 '16 at 00:55
  • @skan in the examples I tried, xtable converted scientific notation to standard (eg pvalue 2e-16 to 0.00). since xtable does the formatting and prints out some characters, then you might have to grep the output and insert the formatting. for output that you create yourself, you can apply the format or override the print method like baptiste suggested. both of those would be more brute force than just a `options(something = ...)` solution – rawr Apr 03 '16 at 01:37
  • I'm gonna submit a request on the xtable web site to ask them to add some option to do it automatically. – skan Apr 03 '16 at 10:46
9

I prefer to leave the formatting to siunitx, but the pre-processing in R can get a bit fiddly,

---
output: 
  pdf_document: 
    keep_tex: yes
header-includes:
- \usepackage{siunitx}
- \usepackage{booktabs}
---

\sisetup{detect-all, tight-spacing=false, exponent-product = \cdot,
round-mode = figures,
round-precision = 3}

```{r, results='asis'}
as.sci_fy <- function(x) {class(x) <- c("sci_fy", class(x)); x}
format.sci_fy <-  function(x) sprintf("\\num{%e}", x)
print.sci_fy <- function(x) cat(format(x))


x <- 6.22e-21
as.sci_fy(x)

d <- data.frame(x=rnorm(10), y=rnorm(10), f=letters[1:10])
d <- lapply(d, function(c) if(is.numeric(c)) format(as.sci_fy(c)) else c)

library(xtable)
d <- xtable(data.frame(d, stringsAsFactors = FALSE))
 print(d, type="latex", align = c("l", "l", "l"),
        table.placement = "!htpb",
        floating.environment = "table",
        include.rownames=FALSE, 
       sanitize.text.function = function(x){x}, booktabs=TRUE)

```

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • What's the mining of a function that doesn't seem to do anything? "function(x){x}" – skan Apr 02 '16 at 23:42
  • my failure to dig up `?identity`. It's required when you have to pass a function, but don't want it to do anything. Here I don't want sanity. – baptiste Apr 02 '16 at 23:57
  • Then whom you pass the function to? I think I understand, this is the way you execute \num{} on every cell – skan Apr 03 '16 at 00:47
  • Imagine you got a table from a regression xtable(lm(...)). How would you apply your method with to that table? – skan Apr 03 '16 at 00:55
  • i don't know much about xtable to be honest – baptiste Apr 03 '16 at 02:35
  • I'm coming back to this problem. Imagine I don't define my own table but I get it from a regression and use xtable to produce it. <>= library(xtable) data(tli) fm2 <- lm(tlimth ~ sex*ethnicty, data = tli) xxx <- xtable(fm2) print(xxx, booktabs = TRUE) @ What options do I need to use in my rnw file or R code to use automatically siunitx. I've tried \usepackage{siunitx} and several \sisetup{} but it doesn't seem to make any difference – skan Nov 18 '16 at 12:43
0

I think the solution could be something like this

beauty <- function(xx) {cat(gsub("\\s([-+]?[0-9]*\\.?[0-9]+)([eE]([-+]?[0-9]+))\\s", " $\\1\\\\cdot{}10^{\\3}$ ", xx, perl=T)) }

d <- data.frame(x=1:10*10000, y=1:10*1e22,f=letters[1:10])

beauty(print(xtable(d, digits=5, display=c("s","G", "G", "s")), type="latex"))

It's not perfect anyway. If anybody can optimize it it would be great. Anyway I've sent a request to xtable to add this feature but it doesn't seem to be much development lately.

Another option would be to use the sanitize option with gsub.

skan
  • 7,423
  • 14
  • 59
  • 96