From: dscott Date: Wed, 9 Dec 2015 04:58:38 +0000 (+0000) Subject: Added ability to produce mathematical arrays X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=41668fa2a665c2b0945c3455dc3b6b8b087e92e9;p=xtable.git Added ability to produce mathematical arrays git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@74 edb9625f-4e0d-4859-8d74-9fd3b1da38cb --- diff --git a/pkg/DESCRIPTION b/pkg/DESCRIPTION index 43273ee..dca3e58 100644 --- a/pkg/DESCRIPTION +++ b/pkg/DESCRIPTION @@ -1,6 +1,6 @@ Package: xtable -Version: 1.8-0 -Date: 2015-10-22 +Version: 1.8-1 +Date: 2015-12-09 Title: Export Tables to LaTeX or HTML Author: David B. Dahl Maintainer: David Scott diff --git a/pkg/NAMESPACE b/pkg/NAMESPACE index 07c3436..46e2163 100644 --- a/pkg/NAMESPACE +++ b/pkg/NAMESPACE @@ -4,10 +4,12 @@ importFrom("stats", "anova", "as.ts", "cycle", "end", "frequency", importFrom("utils", "packageDescription") export("caption<-", "caption", "label", "label<-", "align<-", "align", "digits<-", "digits", "display<-", - "display", "xtable", "print.xtable", "toLatex.xtable", + "display", "xtable", + "print.xtable", "print.xtableMatharray","toLatex.xtable", "autoformat", "xalign", "xdigits", "xdisplay") S3method("print", "xtable") +S3method("print", "xtableMatharray") S3method("toLatex", "xtable") S3method("caption<-", "xtable") @@ -23,6 +25,7 @@ S3method("display", "xtable") S3method("xtable", "data.frame") S3method("xtable", "matrix") +S3method("xtable", "xtableMatharray") S3method("xtable", "table") S3method("xtable", "anova") S3method("xtable", "aov") diff --git a/pkg/NEWS b/pkg/NEWS index edf19eb..bd83d92 100644 --- a/pkg/NEWS +++ b/pkg/NEWS @@ -1,3 +1,5 @@ +1.8-1 (NOT YET SUBMITTED TO CRAN) + 1.8-0 * autoformat, xalign, xdigits, xdisplay from Arni Magnusson, added along with help file. Feature request #5686. diff --git a/pkg/R/print.xtableMatharray.R b/pkg/R/print.xtableMatharray.R new file mode 100644 index 0000000..63515b8 --- /dev/null +++ b/pkg/R/print.xtableMatharray.R @@ -0,0 +1,20 @@ +print.xtableMatharray <- function(x, + print.results = FALSE, + format.args = getOption("xtable.format.args", NULL), + scalebox = getOption("xtable.scalebox", NULL), + comment = FALSE, + timestamp = NULL, + ...) +{ + class(x) <- c("xtableMatharray","data.frame") + print.xtable(x, floating = FALSE, + tabular.environment = 'array', + include.rownames = FALSE, include.colnames = FALSE, + hline.after = NULL, + print.results = print.results, + format.args = format.args, + scalebox = scalebox, + comment = comment, + timestamp = timestamp, + ...) +} diff --git a/pkg/R/toLatex.R b/pkg/R/toLatex.R index 854e6e3..86fada2 100644 --- a/pkg/R/toLatex.R +++ b/pkg/R/toLatex.R @@ -22,12 +22,12 @@ ### Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, ### MA 02111-1307, USA -## The generic for toLatex() is declared in the base package "utils" +### 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 + ## 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" diff --git a/pkg/R/xtable.R b/pkg/R/xtable.R index 011179d..76a4be9 100644 --- a/pkg/R/xtable.R +++ b/pkg/R/xtable.R @@ -26,7 +26,7 @@ xtable <- function(x, caption = NULL, label = NULL, align = NULL, } -## data.frame and matrix objects +### data.frame and matrix objects xtable.data.frame <- function(x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, @@ -63,9 +63,25 @@ xtable.matrix <- function(x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, ...) { return(xtable.data.frame(data.frame(x, check.names = FALSE), caption = caption, label = label, align = align, - digits = digits, display = display, auto = auto)) + digits = digits, display = display, auto = auto, + ...)) } +### xtableMatharray object +### To deal with numeric arrays such as a variance-covariance matrix +### From a request by James Curran, 16 October 2015 +xtable.xtableMatharray <- function(x, caption = NULL, label = NULL, + align = NULL, digits = NULL, + display = NULL, auto = FALSE, + ...) { + class(x) <- c("xtableMatharray","matrix") + xtbl <- xtable.matrix(x, + caption = caption, label = label, align = align, + digits = digits, display = display, auto = auto, + ...) + class(xtbl) <- c("xtableMatharray","xtable","data.frame") + return(xtbl) +} ### table objects (of 1 or 2 dimensions) by Guido Gay, 9 Feb 2007 ### Fixed to pass R checks by DBD, 9 May 2007 @@ -88,7 +104,7 @@ xtable.table <- function(x, caption = NULL, label = NULL, align = NULL, } -## anova objects +### anova objects xtable.anova <- function(x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, ...) { @@ -110,7 +126,7 @@ xtable.anova <- function(x, caption = NULL, label = NULL, align = NULL, } -## aov objects +### aov objects xtable.aov <- function(x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, ...) { @@ -155,7 +171,7 @@ xtable.aovlist <- function(x, caption = NULL, label = NULL, align = NULL, -## lm objects +### lm objects xtable.lm <- function(x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, ...) { @@ -182,7 +198,7 @@ xtable.summary.lm <- function(x, caption = NULL, label = NULL, align = NULL, } -## glm objects +### glm objects xtable.glm <- function(x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, ...) { @@ -199,7 +215,7 @@ xtable.summary.glm <- function(x, caption = NULL, label = NULL, align = NULL, } -## prcomp objects +### prcomp objects xtable.prcomp <- function(x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, ...) { @@ -249,8 +265,7 @@ xtable.coxph <- function (x, caption = NULL, label = NULL, align = NULL, tmp <- cbind(beta, exp(beta), se, beta/se, 1 - pchisq((beta/se)^2, 1)) dimnames(tmp) <- list(names(beta), c("coef", "exp(coef)", "se(coef)", "z", "p")) - } - else { + } else { tmp <- cbind( beta, exp(beta), se, beta/se, signif(1 - pchisq((beta/se)^2, 1), digits - 1)) dimnames(tmp) <- list(names(beta), @@ -279,8 +294,7 @@ xtable.ts <- function(x, caption = NULL, label = NULL, align = NULL, "Arg8", "Arg9", "Arg10", "Arg11", paste(tp.1, month.abb[tp.2], sep = " ")) tmp <- data.frame(x, row.names = ROWNAMES); - } - else if (inherits(x, "ts") && is.null(ncol(x))) { + } else if (inherits(x, "ts") && is.null(ncol(x))) { COLNAMES <- switch(frequency(x), "Value", "Arg2", "Arg3", # Dummy arguments diff --git a/pkg/man/print.xtableMatharray.Rd b/pkg/man/print.xtableMatharray.Rd new file mode 100644 index 0000000..fc76f2e --- /dev/null +++ b/pkg/man/print.xtableMatharray.Rd @@ -0,0 +1,67 @@ +\name{print.xtableMatharray} +\alias{print.xtableMatharray} +\title{Print Math Array} +\description{ + For an object of class \code{"xtableMatharray"}, returns the LaTeX + commands to produce an array. +} +\usage{ +\method{print}{xtableMatharray}(x, + print.results = FALSE, + format.args = getOption("xtable.format.args", NULL), + scalebox = getOption("xtable.scalebox", NULL), + comment = FALSE, + timestamp = NULL, + ...) +} +\arguments{ + \item{x}{An object of class \code{"xtableMatharray"}.} + \item{print.results}{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. + Default value is \code{FALSE}.} + \item{format.args}{List of arguments for the \code{formatC} function. + For example, standard German number separators can be specified as + \code{format.args=list(big.mark = "'", decimal.mark = ",")}. The + arguments \code{digits} and \code{format} should not be included in + this list. See details for function \code{\link{print.xtable}}. + Default value is \code{NULL}.} + \item{scalebox}{If not \code{NULL}, a \code{scalebox} clause will be + added around the tabular environment with the specified value used + as the scaling factor. + Default value is \code{NULL}.} + \item{comment}{If \code{TRUE}, the version and timestamp comment is + included. Default value is \code{FALSE}. } + \item{timestamp}{Timestamp to include in LaTeX comment. Set this + to \code{NULL} to exclude the timestamp. Default value is \code{NULL}. } + \item{...}{Additional arguments. (Currently ignored.) } +} +\details{ + Here are some details. +} +\value{ + A character vector containing the LaTeX code for incorporating an + array in a mathematical expression. +} + +\author{ + David Scott \email{d.scott@auckland.ac.nz}. +} +\seealso{ + \code{\link{print.xtable}} +} + +\examples{ +V <- matrix(c(1.140380e-03, 3.010497e-05, 7.334879e-05, + 3.010497e-05, 3.320683e-04, -5.284854e-05, + 7.334879e-05, -5.284854e-05, 3.520928e-04), nrow = 3) +V +class(V) <- c("xtableMatharray") +class(V) + +mth <- xtable(V) +str(mth) +mth +} + +\keyword{print} diff --git a/pkg/tests/test.matharray.R b/pkg/tests/test.matharray.R new file mode 100644 index 0000000..fed6f85 --- /dev/null +++ b/pkg/tests/test.matharray.R @@ -0,0 +1,40 @@ +require(xtable) +V <- matrix(c(1.140380e-03, 3.010497e-05, 7.334879e-05, + 3.010497e-05, 3.320683e-04, -5.284854e-05, + 7.334879e-05, -5.284854e-05, 3.520928e-04), nrow = 3) +V +class(V) <- c("xtableMatharray") +class(V) + +mth <- xtable(V) +str(mth) +mth + +tbl <- xtable(V, display = rep("E", 4)) +str(tbl) +tbl + +V <- matrix(c(1.140380e-03, 3.010497e-05, 7.334879e-05, + 3.010497e-05, 3.320683e-04, -5.284854e-05, + 7.334879e-05, -5.284854e-05, 3.520928e-04), nrow = 3) +V +mth <- xtable(V, display = rep("E", 4)) +class(mth) +mth +class(mth) <- c("xtableMatharray") +mth +print(mth) +print.xtableMatharray(xtable(V, display = rep("E", 4))) +class(mth) <- c("xtableMatharray") +mth + +V <- matrix(c(1.140380e-03, 3.010497e-05, 7.334879e-05, + 3.010497e-05, 3.320683e-04, -5.284854e-05, + 7.334879e-05, -5.284854e-05, 3.520928e-04), nrow = 3) +V + +print.xtableMatharray(xtable(V), + format.args = list(display = rep("E", 4))) +print.xtableMatharray(xtable(V), + format.args = list(digits = 6)) + diff --git a/pkg/vignettes/xtableGallery.Rnw b/pkg/vignettes/xtableGallery.Rnw index 8491e12..4aa6d43 100644 --- a/pkg/vignettes/xtableGallery.Rnw +++ b/pkg/vignettes/xtableGallery.Rnw @@ -7,6 +7,7 @@ %************************************************************************** \documentclass{article} \usepackage[a4paper,height=24cm]{geometry} % geometry first +\usepackage{listings} \usepackage{array} \usepackage{booktabs} \usepackage{longtable} @@ -20,6 +21,13 @@ \newcommand\code[1]{\texttt{#1}} \newcommand\pkg[1]{\textbf{#1}} \setcounter{tocdepth}{2} + +\lstset{language=R,basicstyle=\ttfamily, + keywordstyle=\ttfamily, + emphstyle=\itshape, + escapeinside = ||, + lineskip=0pt} + \begin{document} \title{The \pkg{xtable} Gallery} @@ -121,6 +129,36 @@ temp.table <- xtable(temp.ts, digits = 0) temp.table @ +\subsection{Mathematical arrays} +\label{sec:mathematical-arrays} + +Mathematical arrays (as compared to data arrays) can be printed +correctly by declaring the class as \code{xtableMatharray}. +<>= +V <- matrix(c(1.140380e-03, 3.010497e-05, 7.334879e-05, + 3.010497e-05, 3.320683e-04, -5.284854e-05, + 7.334879e-05, -5.284854e-05, 3.520928e-04), nrow = 3) +V +class(V) <- c("xtableMatharray") +class(V) +xtbl <- print(xtable(V, display = rep("E", 4))) +@ %def + +Then \code{xtbl} can be used in an expression such as +$V=\left[\Sexpr{xtbl}\right]$, produced by +\begin{lstlisting} +V=\left[\Sexp||r{xtbl}\right]. +\end{lstlisting} + + +An alternative display can be created using the \texttt{digits} +argument. If \code{xtbl} is defined instead according to + +<>= +xtbl <- print(xtable(V, digits = 6)) +@ %def +then we get\\ +$V=\left[\Sexpr{paste(print(xtable(V, digits = 6)))}\right].$ \newpage <>= @@ -209,6 +247,7 @@ print(wanttex, sanitize.text.function = function(str) gsub("_", "\\_", str, fixed = TRUE)) @ + \newpage \subsection{Markup in tables} @@ -237,6 +276,8 @@ money <- matrix(c("$1,000","$900","$100"), ncol = 3, print(xtable(money), sanitize.rownames.function = function(x) {x}) @ + + \section{Format examples} \subsection{Adding a centering environment} <>= @@ -291,6 +332,27 @@ digits(tli.table) <- matrix(0:4, nrow = 10, ncol = ncol(tli)+1) tli.table @ +\subsection{Minus signs} +\label{sec:minus-signs} + +The standard print functions do not render minus signs properly. They +are actually hyphens (-) rather than minus signs ($-$). True minus +signs can be obtained using the \code{math.style.negative} argument. + +\p +Here is the \LaTeX{} which is produced: + +<<>>= +pr1 <- prcomp(USArrests) +print(xtable(pr1), math.style.negative = TRUE) +@ + +And here is the resulting table. +<>= +print(xtable(pr1), math.style.negative = TRUE) +@ + + \newpage \subsection{Suppress row/column names}