Controlando o tamanho das tabelas produzidas a partir das funções R tabular() e latex()

Controlando o tamanho das tabelas produzidas a partir das funções R tabular() e latex()

I'll state up front that I'm not absolutely certain whether this question is more appropriate for CrossValidated or for this site. However, since I've seen several questions about the latex() command from Hmisc here, I think this question is suitable for this site.

I'm a relatively experienced R user but pretty new to LaTeX. I am trying to automatically create a large number of pdfs from a set of results generated within R. I'd prefer to create these files entirely within R, if possible. After some searching, I decided to do this through a combination of the tables package and the latex() function from Hmisc.

Algumas das tabelas que estou criando são bastante grandes e, quando examino os PDFs que criei, as tabelas tendem a sair da página. O que eu gostaria de fazer é usar o latex()comando ou o tabular()comando interno Rpara diminuir o tamanho das tabelas que aparecem nos meus PDFs resultantes. A latexfunção tem um sizeargumento " ", mas a especificação size = "small"não parece funcionar com tabelas geradas por chamadas para tabular(). Alguém tem algum conselho?

Alguns exemplos de código podem ser encontrados abaixo.

require(tables)
require(Hmisc)

# Create sample data
Condition <- factor(rep(c("Control", "Experimental"), 150))
Time <- factor(rep(c("Baseline", "Baseline", "Three days", "Three days", 
   "Three weeks", "Three weeks"), 50))
var1 <- rnorm(300)
var2 <- rnorm(300)
var3 <- rnorm(300)
var4 <- rnorm(300)
var5 <- rnorm(300)
var6 <- rnorm(300)

# Add to data frame, sprinkle in missing values
d <- data.frame(Condition, Time, var1, var2, var3, var4, var5, var6)
d[sample(1:300, 30), c("var1", "var2", "var3", "var4")] <- NA
d[sample(1:300, 20), c("var5", "var6")] <- NA

# Create helper functions
mean.narm <- function(x) mean(x, na.rm = TRUE)
sd.narm <- function(x) sd(x, na.rm = TRUE)
count.obs <- function(x) sum(!is.na(x))
# Use tabular() from tables
tab <- tabular((Heading("Variable 1") * var1 + Heading("Variable 2") * var2 + 
   Heading("Variable 3") * var3 + Heading("Variable 4") * var4 + Heading("Variable 5") * var5 + 
   Heading("Variable 6") * var6) * Time ~ Condition * ((N=count.obs) + (Mean=mean.narm) + (SD=sd.narm)),
           data = d)

# Ensure that pdflatex is used when I call latex(), below
options(latexcmd = "pdflatex")
options(dviExtension = "pdf")
options(xdvicmd = "C:/PROGRA~1/R/R-215~1.3/bin/i386/open.exe")
# Use the latex() function
latex(tab, "table1.tex")

Editar: Conforme solicitado, abaixo está o código obtido de uma chamada para latex(tab, ""). Como mencionei acima, o que quero fazer não é apenas modificar manualmente o LaTeX produzido pela latex()função, mas garantir que o código LaTeX gerado a partir da chamada latex()produza uma tabela menor.

\begin{tabular}{llcccccc}
\hline
& & \multicolumn{6}{c}{Condition} \\ 
& & \multicolumn{3}{c}{Control} & \multicolumn{3}{c}{Experimental} \\ 
 & Time & N & Mean & SD & N & Mean & \multicolumn{1}{c}{SD} \\ 
\hline
Variable 1 & Baseline  & $42$ & $\phantom{-}0.029831$ & $1.0657$ & $46$ & $\phantom{-}0.0067612$ & $1.0687$ \\
 & Three days  & $44$ & $\phantom{-}0.109871$ & $1.0161$ & $43$ & $-0.1633312$ & $1.1552$ \\
 & Three weeks  & $47$ & $\phantom{-}0.160282$ & $1.0613$ & $48$ & $\phantom{-}0.1055877$ & $0.9537$ \\
Variable 2 & Baseline  & $42$ & $-0.166005$ & $1.1078$ & $46$ & $\phantom{-}0.0012202$ & $0.9659$ \\
 & Three days  & $44$ & $-0.205125$ & $0.9315$ & $43$ & $-0.2294237$ & $1.0693$ \\
 & Three weeks  & $47$ & $-0.164718$ & $1.0122$ & $48$ & $-0.0769583$ & $1.0418$ \\
Variable 3 & Baseline  & $42$ & $\phantom{-}0.077019$ & $1.0643$ & $46$ & $\phantom{-}0.0798812$ & $1.0396$ \\
 & Three days  & $44$ & $-0.282688$ & $0.9023$ & $43$ & $\phantom{-}0.1299074$ & $0.9916$ \\
 & Three weeks  & $47$ & $-0.291495$ & $1.0138$ & $48$ & $-0.0240293$ & $1.0309$ \\
Variable 4 & Baseline  & $42$ & $-0.247300$ & $1.0963$ & $46$ & $-0.0409480$ & $1.0901$ \\
 & Three days  & $44$ & $-0.158524$ & $1.0995$ & $43$ & $\phantom{-}0.1486800$ & $0.8722$ \\
 & Three weeks  & $47$ & $-0.149038$ & $0.8798$ & $48$ & $-0.0002842$ & $0.9750$ \\
Variable 5 & Baseline  & $49$ & $\phantom{-}0.008081$ & $0.8102$ & $47$ & $-0.3131353$ & $1.0592$ \\
 & Three days  & $45$ & $-0.241864$ & $1.0037$ & $45$ & $\phantom{-}0.1130470$ & $0.8106$ \\
 & Three weeks  & $44$ & $-0.177473$ & $1.0706$ & $50$ & $-0.0264635$ & $0.7805$ \\
Variable 6 & Baseline  & $49$ & $\phantom{-}0.013659$ & $1.0833$ & $47$ & $-0.0967571$ & $1.0676$ \\
 & Three days  & $45$ & $\phantom{-}0.098045$ & $0.9842$ & $45$ & $\phantom{-}0.0756736$ & $0.9613$ \\
 & Three weeks  & $44$ & $-0.152400$ & $1.0654$ & $50$ & $-0.2145893$ & $1.0157$ \\
\hline 
\end{tabular}

Responder1

Como você está usando tableso pacote R, você deve estar usando Sweave. Aqui está um .Rnwarquivo que pode funcionar para você. Eu não tenho muita experiência com R. Eu apenas o uso para alguns cálculos estatísticos básicos. Então, sinto muito se não posso lhe dar uma resposta definitiva se você pode configurar o tamanho da tabela usando o latex()comando. Se essa fosse sua única opção, sugiro perguntar emValidação Cruzada.

Salve como foobar.Rnwe coloque-o em seu diretório de trabalho. Agora, em R, defina seu diretório de trabalho usando setwd()(se ainda não o fez). Agora, corra

Sweave("foobar.Rnw")

Se você tiver sucesso fazendo isso, você deverá ver

You can now run (pdf)latex on ‘tableprosweave.tex’

no final.

Agora, em seu diretório de trabalho, abra foobar.texcom seu editor TeX favorito e execute pdflatexou apenas execute

pdflatex foobar

em seu terminal/linha de comando.

foobar.Rnw:

\documentclass{article}

\usepackage{Sweave}
\usepackage{booktabs}

\begin{document}
\SweaveOpts{concordance=TRUE, keep.source=TRUE}
<<echo=false>>=
options(width=60)
@

<<>>=
require(tables)
require(Hmisc)

booktabs() % To use booktabs formatting

# Create sample data
Condition <- factor(rep(c("Control", "Experimental"), 150))
Time <- factor(rep(c("Baseline", "Baseline", "Three days", "Three days", 
   "Three weeks", "Three weeks"), 50))
var1 <- rnorm(300)
var2 <- rnorm(300)
var3 <- rnorm(300)
var4 <- rnorm(300)
var5 <- rnorm(300)
var6 <- rnorm(300)

# Add to data frame, sprinkle in missing values
d <- data.frame(Condition, Time, var1, var2, var3, var4, var5, var6)
d[sample(1:300, 30), c("var1", "var2", "var3", "var4")] <- NA
d[sample(1:300, 20), c("var5", "var6")] <- NA

# Create helper functions
mean.narm <- function(x) mean(x, na.rm = TRUE)
sd.narm <- function(x) sd(x, na.rm = TRUE)
count.obs <- function(x) sum(!is.na(x))
# Use tabular() from tables

tab <- tabular((Heading("Variable 1") * var1 + Heading("Variable 2") * var2 + 
   Heading("Variable 3") * var3 + Heading("Variable 4") * var4 + Heading("Variable 5") * var5 + 
   Heading("Variable 6") * var6) * Time ~ Condition * ((N=count.obs) + (Mean=mean.narm) + (SD=sd.narm)),
           data = d)
@

\newpage

% Here, \small was written just before the code chunk.

\begin{center}
\small
<<results=tex,echo=FALSE>>=
latex(
tab
)
@
\end{center}

\vfill

% Here, \scriptsize was written just before the code chunk.

\begin{center}
\scriptsize
<<results=tex,echo=FALSE>>=
latex(
tab
)
@
\end{center}

%<<>>=
%# Ensure that pdflatex is used when I call latex(), below
%options(latexcmd = "pdflatex")
%options(dviExtension = "pdf")
%options(xdvicmd = "C:/PROGRA~1/R/R-215~1.3/bin/i386/open.exe")
%# Use the latex() function
%#latex(tab, file="table1.tex")
%@

\end{document}

Comentei as últimas linhas porque realmente não sei o que elas fazem e para compilar o arquivo no meu computador. :-)Aqui está a saída da segunda página.

insira a descrição da imagem aqui

Se você realmente está logo após a saída da tabela, sugiro que você escreva \smallor \footnotesize, etc, antes do trecho de código. Novamente, não sei como fazer isso apenas com a latex()função de R. Vermanualpara obter mais informações sobre Sweave.

Alguns editores fazem você editar e compilar .Rnwdiretamente da fonte, comoRstudio. ATÉ ONDE SEI,Texmakertambém pode lidar com isso.

informação relacionada