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
----------------------------------
The
?panderOptions
page says 'digits' is passed toformat
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 yourformat()
-ed values:You do want to keep the "round" option at 2.
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 thensmall
parameter offormat()
which would setBut since pander only feeds numeric values to
format()
you can simply work around this by feeding the valuesas.character()
to pander:which results in: