Graphics, Figures & TablesTable wide auto-adjust with R-Sweave

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
NicolasRaphael
Posts: 2
Joined: Tue Oct 25, 2011 1:27 pm

Table wide auto-adjust with R-Sweave

Post by NicolasRaphael »

Hello,
I'm writing a report. I use R to analyse data and Latex (on Windows or Linux) to write the report. I use Sweave to directly write the latex report from R analysis.

My problem is about "tables print", they are not adjusted to the page width. I use latex() function in sweave file (*.rnw) to create table like this:

Code: Select all

<<echo=FALSE,results=tex>>=
library(xtable)
XTabAgeGirIniFreq <- xtable(TabAgeGirIniFreq, caption="Pourcentage des résidents par age en fonction de leur score Gir", digits = c(0,0,0,0,0,0), label="TabAgeGirIniFreq")
align(XTabAgeGirIniFreq) <- "lccccc";
print(XTabAgeGirIniFreq,caption.placement = "top");
@
that generate:
Image

I would that table auto-adjust it on page.
Someone know how do?
Thanks

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
pyrogerg
Posts: 9
Joined: Fri Aug 12, 2011 1:24 am

Re: Table wide auto-adjust with R-Sweave

Post by pyrogerg »

The width of a table is largely determined by the number and width of the columns. Column width is set automatically to match the width of the data, or you can specify column with the "align" argument to the xtable command (see help(xtable) for details).

If that's not doing it, I'd have to play around with the code. Could you provide a little R code that would create a data frame just a few rows long and which creates a table approximately the width that you're getting?
NicolasRaphael
Posts: 2
Joined: Tue Oct 25, 2011 1:27 pm

Table wide auto-adjust with R-Sweave

Post by NicolasRaphael »

In first, thanks for your answer pyrogerg. Your post permit me to find track for solution.

The solution I was found is the use of the latex() function with "collabel.just" and "col.just" as argument.
Like that:

Code: Select all

<<echo=FALSE,results=tex>>=
library(Hmisc)
XTableau <- latex(Tableau, col.just = c("c{0.8in}","c{0.8in}","c{0.8in}","c{0.8in}","c{0.8in}"),  collabel.just = c("p{0.8in}","p{0.8in}","p{0.8in}","p{0.8in}","p{0.8in}"), file="", caption="Titre du tableau", cgroup=c("Titre des colonnes"), n.cgroup=c(5), rgroup=c("Titre des lignes"), n.rgroup=c(5), first.hline.double=F, cdec=c(0,0,0,0,0), rowlabel="", label="Tableau", where="!htbp")
print(XTableau,caption.placement = "top");
@
Erratum: In my first message, there is a mistake in the code: I used latex() function, not table() function.
Post Reply