From 40f0905baf4a1a22290cb7ca2c6fe239690ae1d9 Mon Sep 17 00:00:00 2001 From: roosen Date: Mon, 30 Jan 2012 09:40:38 +0000 Subject: [PATCH] Added "toLatex()" method. Added "print.result" argument to "print.xtable()" function. git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@18 edb9625f-4e0d-4859-8d74-9fd3b1da38cb --- pkg/DESCRIPTION | 2 +- pkg/NAMESPACE | 9 +++++++- pkg/NEWS | 8 +++++++ pkg/R/print.xtable.R | 7 ++++-- pkg/R/toLatex.R | 40 ++++++++++++++++++++++++++++++++++ pkg/inst/doc/xtableGallery.snw | 22 +++++++++++++++++++ pkg/man/print.xtable.Rd | 4 +++- pkg/man/toLatex.Rd | 17 +++++++++++++++ 8 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 pkg/R/toLatex.R create mode 100644 pkg/man/toLatex.Rd diff --git a/pkg/DESCRIPTION b/pkg/DESCRIPTION index 40d1eae..4c8eab3 100644 --- a/pkg/DESCRIPTION +++ b/pkg/DESCRIPTION @@ -1,5 +1,5 @@ Package: xtable -Version: 1.6-0 +Version: 1.6-1 Date: 2011/10/06 Title: Export tables to LaTeX or HTML Author: David B. Dahl diff --git a/pkg/NAMESPACE b/pkg/NAMESPACE index 521cf2d..52dbdd6 100644 --- a/pkg/NAMESPACE +++ b/pkg/NAMESPACE @@ -1,11 +1,17 @@ # NAMESPACE work by Robert Gentleman # in e-mail on July 30, 2007. +# +# Extended by C Roosen, 30/01/2012 + +importFrom("utils", toLatex) export("caption<-", "caption", "label", "label<-", "align<-", "align", "digits<-", "digits", "display<-", - "display", "xtable") + "display", "xtable", "print.xtable", "toLatex.xtable") S3method("print", "xtable") +S3method("toLatex", "xtable") + S3method("caption<-", "xtable") S3method("caption", "xtable") S3method("label", "xtable") @@ -16,6 +22,7 @@ S3method("digits", "xtable") S3method("digits<-", "xtable") S3method("display<-", "xtable") S3method("display", "xtable") + S3method("xtable", "data.frame") S3method("xtable", "matrix") S3method("xtable", "table") diff --git a/pkg/NEWS b/pkg/NEWS index f085dc6..a2469ae 100644 --- a/pkg/NEWS +++ b/pkg/NEWS @@ -1,3 +1,11 @@ +1.6-1 (NOT YET RELEASED) + + * Added some vectorization code to improve performance. + * Added "toLatex" method. + * Included "print" in the exported methods in the NAMESPACE file. + * Added "print.result" argument to "print" that can be used to + suppress the printing. + 1.6-0 (2011-10-07) * Allow "table*" as a value for "floating.environment" in print.xtable(). * Fix bug under which multiple logical columns were formatted incorrectly. diff --git a/pkg/R/print.xtable.R b/pkg/R/print.xtable.R index c20fb62..c517939 100644 --- a/pkg/R/print.xtable.R +++ b/pkg/R/print.xtable.R @@ -44,6 +44,7 @@ print.xtable <- function( sanitize.colnames.function=sanitize.text.function, math.style.negative=FALSE, html.table.attributes="border=1", + print.result=TRUE, ...) { # Claudio Agostinelli dated 2006-07-28 hline.after # By default it print an \hline before and after the columns names independently they are printed or not and at the end of the table @@ -419,8 +420,10 @@ print.xtable <- function( result <- result + ETABLE } result <- sanitize.final(result) - print(result) - + if (print.result){ + print(result) + } + return(invisible(result$text)) } diff --git a/pkg/R/toLatex.R b/pkg/R/toLatex.R new file mode 100644 index 0000000..3a2ba58 --- /dev/null +++ b/pkg/R/toLatex.R @@ -0,0 +1,40 @@ +### xtable package +### +### Produce LaTeX and HTML tables from R objects. +### +### Copyright 2000-2012 David B. Dahl +### +### Maintained by Charles Roosen +### +### This file is part of the `xtable' library for R and related languages. +### It is made available under the terms of the GNU General Public +### License, version 2, or at your option, any later version, +### incorporated herein by reference. +### +### This program is distributed in the hope that it will be +### useful, but WITHOUT ANY WARRANTY; without even the implied +### warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +### PURPOSE. See the GNU General Public License for more +### details. +### +### You should have received a copy of the GNU General Public +### License along with this program; if not, write to the Free +### Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, +### MA 02111-1307, USA + +## The generic for toLatex() is declared in the base package "utils" + +toLatex.xtable <- function(object, ...){ + # Initially just capturing the output of print.xtable(). At some + # point this could be refactored to have print.xtable() call + # toLatex() instead. - CR, 30/01/2012 + dotArgs <- list(...) + dotArgs$x <- object + dotArgs$type <- "latex" + dotArgs$print.result <- FALSE + z <- do.call("print.xtable", dotArgs) + + z <- strsplit(z, split="\n")[[1]] + class(z) <- "Latex" + z +} diff --git a/pkg/inst/doc/xtableGallery.snw b/pkg/inst/doc/xtableGallery.snw index 995db16..39eb05e 100644 --- a/pkg/inst/doc/xtableGallery.snw +++ b/pkg/inst/doc/xtableGallery.snw @@ -431,6 +431,28 @@ print(x.small,floating.environment='sidewaystable') @ \clearpage +\section{Suppressing Printing} +By default the {\tt print} method will print the LaTeX or HTML to standard +output and also return the character strings invisibly. The printing to +standard output can be supproessed by specifying {\tt print.result = FALSE}. + +<<>>= +# This will print the output and store it. +x.out <- print(tli.table) + +# This will just store the value. +x.out2 <- print(tli.table, print.results = FALSE) +@ + +Formatted output can also be captured without the printing with the +{\tt toLatex} method. + +<<>>= +x.ltx <- toLatex(tli.table) +class(x.ltx) +x.ltx +@ + \section{Acknowledgements} Most of the examples in this gallery are taken from the {\tt xtable} documentation. \section{R Session information} diff --git a/pkg/man/print.xtable.Rd b/pkg/man/print.xtable.Rd index 0e32eb4..55ccf42 100644 --- a/pkg/man/print.xtable.Rd +++ b/pkg/man/print.xtable.Rd @@ -10,7 +10,8 @@ include.colnames=TRUE, only.contents=FALSE, add.to.row=NULL, sanitize.text.function=NULL, sanitize.rownames.function=sanitize.text.function, sanitize.colnames.function=sanitize.text.function, math.style.negative=FALSE, - html.table.attributes="border=1", ...)} + html.table.attributes="border=1", + print.result=TRUE, ...)} \arguments{ \item{x}{An object of class \code{"xtable"}.} \item{type}{Type of table to produce. Possible values for \code{type} are \code{"latex"} or \code{"html"}. @@ -43,6 +44,7 @@ \item{sanitize.colnames.function}{Like the \code{sanitize.text.function}, but applicable to column names. The default uses the \code{sanitize.text.function}.} \item{math.style.negative}{In a LaTeX table, if \code{TRUE}, then use $-$ for the negative sign (as was the behavior prior to version 1.5-3). Default value is \code{FALSE}.} \item{html.table.attributes}{In an HTML table, attributes associated with the \code{} tag. Default value is \code{border=1}.} + \item{print.result}{If \code{TRUE}, the generated table is printed to standard output. Set this to \code{FALSE} if you will just be using the character vector that is returned invisibly.} \item{...}{Additional arguments. (Currently ignored.)} } \details{ diff --git a/pkg/man/toLatex.Rd b/pkg/man/toLatex.Rd new file mode 100644 index 0000000..e0cc1cc --- /dev/null +++ b/pkg/man/toLatex.Rd @@ -0,0 +1,17 @@ +\name{toLatex.xtable} +\alias{toLatex.xtable} +\title{Convert Table to Latex} +\description{Function creating a LaTeX representation of an object of class \code{xtable}.} +\usage{ + \method{toLatex}{xtable}(object, ...)} +\arguments{ + \item{object}{An object of class \code{"xtable"}.} + \item{...}{Other arguments to \code{print.xtable}.} +} +\details{ + This function creates a LaTeX representation of an object of class \code{"xtable"}. This is a method for the generic \code{"toLatex"} in the core R package \code{"utils"}. +} +\author{Charles Roosen \email{roosen@gmail.com} with contributions and suggestions from many others (see source code).} +\seealso{\code{\link{print.xtable}}} + +\keyword{toLatex} -- 2.39.2