[R] xtable, sweave and resizebox

Ista Zahn
2009-08-10T13:12:15Z

> Hi again,
>
> one last annoying thing: how to deal with %, always when I use sweave to
get
> a latex document the % comes as a comment and I cannot have % in my tables,
> how to sort out this annoying detail?
>
> thanks!
> Regards
> Welma

Hi Welma,
You need to escape the % sign, just as you would if you were writing
the table by hand. You can do this using gsub, e.g.:
Data <- data.frame(X =
paste(1:10,"%",sep=""),Y=paste(rnorm(10),"%",sep=""))
 Data$X <- gsub("%","\\\\%",Data$X)
 Data$Y <- gsub("%","\\\\%",Data$Y)
 library(xtable)
 print(xtable(Data))
% latex table generated in R 2.9.1 by xtable 1.5-5 package
% Mon Aug 10 08:59:19 2009
\begin{table}[ht]
\begin{center}
\begin{tabular}{rll}
  \hline
 & X & Y \\
  \hline
1 & 1$\backslash$\% & -0.745912124497115$\backslash$\% \\
  2 & 2$\backslash$\% & 0.230686751706215$\backslash$\% \\
  3 & 3$\backslash$\% & -0.111726300126857$\backslash$\% \\
  4 & 4$\backslash$\% & 1.47083079027849$\backslash$\% \\
  5 & 5$\backslash$\% & 0.594836094641913$\backslash$\% \\
  6 & 6$\backslash$\% & 0.317505326039211$\backslash$\% \\
  7 & 7$\backslash$\% & -0.545536941018959$\backslash$\% \\
  8 & 8$\backslash$\% & 0.894792412111052$\backslash$\% \\
  9 & 9$\backslash$\% & 0.616813341143258$\backslash$\% \\
  10 & 10$\backslash$\% & -0.469868401424871$\backslash$\% \\
   \hline
   \end{tabular}
   \end{center}
   \end{table}

Also, regarding your initial issue with the wide table: consider
printing the table sideways instead of shrinking it. See the Sweave
examples vignette for an example.

-Ista