I'd like to be able to render an xtable in an automatically run piece of code, i.e. NOT via copy-and-paste, while controlling the number of significant digits. The only way that I know to render an xtable on a standard plot device is by using grid.table, but that method ignores the digits directive and plots all available digits. Here's a code example. Any advice?
library(xtable)
library(gridExtra)
x = rnorm(100)
y = x + rnorm(100)
m = lm(y ~ x)
print(xtable(m)) #too many decimal places
print(xtable(m, digits = 2)) #this works
grid.table(xtable(m, digits=2)) #this doesn't!!!
None of the bits of advice here seem useful for automated rendering: R: rendering xtable
xtable
produces a data.frame with further attributes that are used for formatting when printing to latex or html formats.grid.table
ignores them altogether and only deals with the bare data.frame, so any number formatting has to be done on the data itself, e.g. withsprintf
orformatC
– baptiste