pander number of digits after decimal point
Asked Answered
S

3

3

I am trying to output a table using pander in a .rmd file as a pdf with 2 digits following the decimal point, but I get no digits using the following rmd:

---
title: "Long table test"
output: pdf_document
---

Here is a table:

```{r setup}
library (data.table)
library (pander)

set.seed(1984)
longString <- "description string"
dt <- data.table(id=c(1:3),description=rep(longString,3),value=rnorm(3,mean=10000,sd=1))
```

```{r pander-table}
panderOptions('round',2)
panderOptions('digits',2)
panderOptions('keep.trailing.zeros',TRUE)
pander(dt, split.cell = 80, split.table = Inf)
```

results in

-------------------------------
 id     description      value 
---- ------------------ -------
 1   description string  10000 

 2   description string  10000 

 3   description string  10001 
-------------------------------

Would like to see

----------------------------------
 id     description      value 
---- ------------------ ----------
 1   description string  10000.41 

 2   description string   9999.68 

 3   description string  10000.64 
----------------------------------
Saraann answered 21/4, 2016 at 18:31 Comment(4)
My first change would be to drop the digits option. If it's linked to the signif function in R it would have the effect you are seeing: signif( mean(rnorm(1000,1000)), 2) returns [1] 1000. And signif( mean(rnorm(10,1000, 200)), 2) returns [1] 990Euphorbiaceous
Increase digits to 7.Moorish
Setting digits to 7 will give me 9999.677 (i.e. 3 digits after decimal place) or 10000.64. If I set digits to 7 and round to 2 panderOptions('digits',7); panderOptions('round',2) then I get the desired result. Seems like more of a work around than a general solution since it cannot be applied consistently to all data, I need to know how many digits I will have in my data and adjust appropriately. It would be nice if pander behaved in a similar way to kable(x, digits=2) which results in 2 digits after the decimal.Saraann
@DavidDickson knitr::kable passes digits to round -- what you can achieve in pander as well by setting round to 2, I think this is pretty sane and provides great flexibility. What's wrong with setting a large digits and round to 2?Moorish
P
1

Setting round doesn't have any direct influence on the number of digits (though some indirect influence due to potentially rendering digits insignificant (0)). The main problem here is pander doesn't allow you to set the nsmall parameter of format() which would set

the minimum number of digits to the right of the decimal point in formatting real/complex numbers in non-scientific formats. Allowed values are 0 <= nsmall <= 20.

But since pander only feeds numeric values to format() you can simply work around this by feeding the values as.character() to pander:

library (data.table)
library(magrittr)
library (pander)

set.seed(1984)
longString <- "description string"
dt <- data.table(id = c(1:3),
                 description = rep(longString, 3),
                 value = rnorm(3, mean = 10000, sd = 1))

pander(
  x = dt %>% mutate(value = value %>% round(2) %>% as.character()),
  split.cell = 80,
  split.table = Inf,
  justify = "ccr"
)

which results in:

------------------------------------
 id      description           value
---- -------------------- ----------
 1    description string    10000.41

 2    description string     9999.68

 3    description string    10000.64
------------------------------------
Populate answered 20/11, 2017 at 23:1 Comment(0)
E
0

The ?panderOptions page notes that 'digits' is passed to format where it is interpreted as the number of "significant digits". Significant digits really have very little to do with decimal places. You can have 2 significant digits in a decimal value of 0.000041. You can see the effect of your parameter on your format()-ed values:

> format(c( 10000.41,  9999.68, 10000.64 ), digits=2)
[1] "10000" "10000" "10001"

You do want to keep the "round" option at 2.

Euphorbiaceous answered 21/4, 2016 at 19:27 Comment(3)
removing the panderOptions('digits',2) and keeping panderOptions('round',2) produced the exact same result.Saraann
Maybe the digits setting is "durable"? Did you try setting it to NA or 7?Euphorbiaceous
@42- setting digits to 7 will achieve the desired results, see my comment above.Moorish
R
0

The problem was the digits part. You have to increase the number to the maximum digits before and after the decimal point. Your biggest numbers (concerning digits) have 5 digits before and 2 after the decimal (e.g. 10000.41). So you need to set digits to 7 and (leave) round at 2:

```{r pander-table}
panderOptions('round',2)
panderOptions('digits',7)
panderOptions('keep.trailing.zeros',TRUE)
pander(dt, split.cell = 80, split.table = Inf)
```
Rajab answered 13/4, 2020 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.