[R] How to draw table in Latex without using xtable?

Duncan Mackay mackay at northnet.com.au
Fri Mar 23 23:45:34 CET 2012


Hi

I have not been following the thread and please  excuse the length 
and width of this - this is a large example of what you can do using 
xtable and a data.frame

the data.frame is copied to xx  in the chunck Table6H and reworked in 
the table itself resulting in a good presentation in a pdf. It just 
happens to be a \sidewaystable because of size
you need \usepackage{booktabs} in the preliminary. I put a space 
between the lines in the header. A similar thing would apply to \longtable

\begin{sidewaystable}[h]
\caption[Ewe numbers drenched by dcode and date by Farm]%
{Ewe numbers drenched by code and date by Farm}%
\label{tab:GPS6}%
\pdfbookmark[3]{Table:~6 Ewe numbers drenched by code and date by Farm}%
{tab:6}\label{tab:6}%
\centering
\begin{small}
\begin{tabular}{l *{24}{D{.}{.}{3.0}} }
\toprule
\addlinespace[3pt]
% Header
& \multicolumn{24}{c}{Drench Code}\\
\addlinespace[2pt]
\cmidrule(lr){2-25}
\addlinespace[2pt]
% Table6H
<<head1, echo=FALSE, results=tex>>=

   xx <- pparasiteE.tfNw
   for (j in 2:dim(xx)[2]) xx[xx[,j]==0,j] <- NA
   xx[,1] <- as.character(xx[,1])
   # datasets
   xx[,1] <- paste(xx[,1], apply(pparasiteE.ds,1,function(x) 
paste(ifelse(x[2] > 0,"*","\\;\\;"),ifelse(x[1] > 0,"*","\\;\\;"), sep = "") ))

   out <- unique(sub("[^01]+", "", names(xx)[-1]) )
   out <-
   c("\\multicolumn{1}{c}{Drench} & %\n",
     paste("\\multicolumn{3}{c}{",
           out,
           rep(c("} & %\n", "} \\\\"), c(length(out)-1, 1)), sep = ""), "\n"
   )

   cat(out,"\n", sep = "")
   rm(out)

   out <- rep(Farms, (dim(xx)[2]-1)/3)
   out <-
   c("\\addlinespace[2pt]\n\\cmidrule(lr){2-4}\n\\cmidrule(lr){5-7}\n\\cmidrule(lr){8-10}\n\\cmidrule(lr){11-13}\n\\cmidrule(lr){14-16}\n\\cmidrule(lr){17-19}\n\\cmidrule(lr){20-22}\n\\cmidrule(lr){23-25}\n\n\\addlinespace[2pt]\n\n\\multicolumn{1}{c}{Date} 
& %\n",
     paste("\\multicolumn{1}{c}{", out, rep(c("} & %\n", "} \\\\"), 
c(length(out)-1, 1)), sep = ""), "\n"
   )

   cat(out, sep = "")
   rm(out)

@ %% 6 end
\addlinespace[3pt]
\midrule
\addlinespace[5pt]
%Table6
<<Table6, keep.source=FALSE, results=tex, echo=FALSE>>=

   print(
   xtable(xx ,
          digits  = rep(c(0), dim(xx)[2]+1),
          ),
          type    = "latex",
          tabular.environment = "tabular",
          include.rownames = FALSE,
          include.colnames = FALSE,
          only.contents = TRUE,
          NA.string = "\\multicolumn{1}{c}{$\\;\\cdots\\;$}",
          sanitize.text.function = function(x){x},
          hline.after = NULL
   ) ## xtable

@ % Table6 end
\addlinespace[5pt]
\bottomrule
\end{tabular}
\end{small}
\end{sidewaystable}
\setlength\tabcolsep{7pt}

HTH

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
The University of New England
Armidale NSW 2351
Email : home: mackay at northnet.com.au


At 06:59 24/03/2012, you wrote:
>You can use xtable() or latex() and then edit the columns names in the
>generated .tex file.  That is a much better plan than reinventing either of
>those functions.
>
>On Thu, Mar 22, 2012 at 3:46 AM, Manish Gupta 
><mandecent.gupta at gmail.com>wrote:
>
> > Hi,
> >
> > I am working on table suing R and Latex.  I am writing .Rnw file first in
> > which i m reading input file and storing into dataframe. After filtering
> > certain values from this dataframe. I am planning to display it. I don't
> > want to use xtable since i need to change column names.
> >
> > *Sample .Rnw file*
> >
> > @
> > <<echo=FALSE>>=
> > cat("\begin{table}[ht]")
> > cat("\begin{center}")
> > cat("\begin{tabular}{lrrrrr}")
> > cat  ("\hline")
> > cat ("& Df & Sum Sq & Mean Sq & F value & Pr($>$F) \\")
> > cat  ("\hline")
> > cat("block       & 5 & 343.29 & 68.66 & 4.45 & 0.0159 \\")
> > cat  ("N           & 1 & 189.28 & 189.28 & 12.26 & 0.0044 \\")
> > cat  ("P           & 1 & 8.40 & 8.40 & 0.54 & 0.4749 \\")
> > cat  ("K           & 1 & 95.20 & 95.20 & 6.17 & 0.0288 \\")
> > cat  ("N:P         & 1 & 21.28 & 21.28 & 1.38 & 0.2632 \\")
> > cat  ("N:K         & 1 & 33.13 & 33.13 & 2.15 & 0.1686 \\")
> > cat  ("P:K         & 1 & 0.48 & 0.48 & 0.03 & 0.8628 \\")
> > cat  ("Residuals   & 12 & 185.29 & 15.44 &  &  \\")
> > cat   ("\hline")
> > cat("\end{tabular}")
> > cat("\end{center}")
> > cat("\end{table}")
> > @
> >
> > But i am getting error  cat("\" not supported.  In my case tabular values
> > are variable. How can i fix my pblm?
> >
> > Regards
> >
> > --
> > View this message in context:
> > 
> http://r.789695.n4.nabble.com/How-to-draw-table-in-Latex-without-using-xtable-tp4494781p4494781.html
> > Sent from the R help mailing list archive at Nabble.com.
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>         [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list