]> git.donarmstrong.com Git - xtable.git/commitdiff
Added ability to produce mathematical arrays
authordscott <dscott@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Wed, 9 Dec 2015 04:58:38 +0000 (04:58 +0000)
committerdscott <dscott@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Wed, 9 Dec 2015 04:58:38 +0000 (04:58 +0000)
git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@74 edb9625f-4e0d-4859-8d74-9fd3b1da38cb

pkg/DESCRIPTION
pkg/NAMESPACE
pkg/NEWS
pkg/R/print.xtableMatharray.R [new file with mode: 0644]
pkg/R/toLatex.R
pkg/R/xtable.R
pkg/man/print.xtableMatharray.Rd [new file with mode: 0644]
pkg/tests/test.matharray.R [new file with mode: 0644]
pkg/vignettes/xtableGallery.Rnw

index 43273ee6e6abf585c3bdb712e8056523afbda623..dca3e583c725287410239d0aa92a7ddeaba232ba 100644 (file)
@@ -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 <dahl@stat.byu.edu>
 Maintainer: David Scott <d.scott@auckland.ac.nz>
index 07c343661081bd781b5dec9f94c7a9cd925cd7b5..46e21633af9c16afb8271a266870d08dc97ca181 100644 (file)
@@ -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")
index edf19ebdda638e0b7346072b3c31d0878c8447c6..bd83d92bcbb400228d19921bd01ff5a79c27a749 100644 (file)
--- 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 (file)
index 0000000..63515b8
--- /dev/null
@@ -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,
+               ...)
+}
index 854e6e36528fb0b8f6ff5863b767bf7d0c445900..86fada2cf361df0749099d6e1e54dde9e080fc86 100644 (file)
 ### Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,\r
 ### MA 02111-1307, USA\r
 \r
-## The generic for toLatex() is declared in the base package "utils"\r
+### The generic for toLatex() is declared in the base package "utils"\r
 \r
 toLatex.xtable <- function(object, ...){\r
-  # Initially just capturing the output of print.xtable().  At some\r
-  # point this could be refactored to have print.xtable() call\r
-  # toLatex() instead. - CR, 30/01/2012\r
+  ## Initially just capturing the output of print.xtable().  At some\r
+  ## point this could be refactored to have print.xtable() call\r
+  ## toLatex() instead. - CR, 30/01/2012\r
   dotArgs <- list(...)\r
   dotArgs$x <- object\r
   dotArgs$type <- "latex"\r
index 011179d3feaf7cc6412cbb1c13872dde3cdb2fa2..76a4be963ee125dd138f5dd1a8ea69447ec15c7d 100644 (file)
@@ -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 (file)
index 0000000..fc76f2e
--- /dev/null
@@ -0,0 +1,67 @@
+\name{print.xtableMatharray}\r
+\alias{print.xtableMatharray}\r
+\title{Print Math Array}\r
+\description{\r
+  For an object of class \code{"xtableMatharray"}, returns the LaTeX\r
+  commands to produce an array.\r
+}\r
+\usage{\r
+\method{print}{xtableMatharray}(x,\r
+  print.results = FALSE,\r
+  format.args = getOption("xtable.format.args", NULL),\r
+  scalebox = getOption("xtable.scalebox", NULL),\r
+  comment = FALSE,\r
+  timestamp = NULL,\r
+  ...)\r
+}\r
+\arguments{\r
+  \item{x}{An object of class \code{"xtableMatharray"}.}\r
+  \item{print.results}{If \code{TRUE}, the generated table is printed to\r
+    standard output.  Set this to \code{FALSE} if you will just be using\r
+    the character vector that is returned invisibly.\r
+    Default value is \code{FALSE}.} \r
+  \item{format.args}{List of arguments for the \code{formatC} function.\r
+    For example, standard German number separators can be specified as\r
+    \code{format.args=list(big.mark = "'", decimal.mark = ",")}. The\r
+    arguments \code{digits} and \code{format} should not be included in\r
+    this list. See details for function \code{\link{print.xtable}}.\r
+    Default value is \code{NULL}.}\r
+  \item{scalebox}{If not \code{NULL}, a \code{scalebox} clause will be\r
+    added around the tabular environment with the specified value used\r
+    as the scaling factor.\r
+    Default value is \code{NULL}.}\r
+  \item{comment}{If \code{TRUE}, the version and timestamp comment is\r
+    included.  Default value is \code{FALSE}. }\r
+  \item{timestamp}{Timestamp to include in LaTeX comment.  Set this\r
+    to \code{NULL} to exclude the timestamp. Default value is \code{NULL}. }\r
+  \item{...}{Additional arguments.  (Currently ignored.) }\r
+}\r
+\details{\r
+ Here are some details.\r
+}\r
+\value{\r
+  A character vector containing the LaTeX code for incorporating an\r
+  array in a mathematical expression.\r
+}\r
+\r
+\author{\r
+  David Scott \email{d.scott@auckland.ac.nz}.\r
+}\r
+\seealso{\r
+  \code{\link{print.xtable}}\r
+}\r
+\r
+\examples{\r
+V <- matrix(c(1.140380e-03,  3.010497e-05,  7.334879e-05,\r
+              3.010497e-05,  3.320683e-04, -5.284854e-05,\r
+              7.334879e-05, -5.284854e-05,  3.520928e-04), nrow = 3)\r
+V\r
+class(V) <- c("xtableMatharray")\r
+class(V)\r
+\r
+mth <- xtable(V)\r
+str(mth)\r
+mth\r
+}\r
+\r
+\keyword{print}\r
diff --git a/pkg/tests/test.matharray.R b/pkg/tests/test.matharray.R
new file mode 100644 (file)
index 0000000..fed6f85
--- /dev/null
@@ -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))
+
index 8491e129fb493da37ecfeccc5c2f894f2ae17561..4aa6d43f9fca060f2db4338efd7b767189e7110a 100644 (file)
@@ -7,6 +7,7 @@
 %**************************************************************************\r
 \documentclass{article}\r
 \usepackage[a4paper,height=24cm]{geometry} % geometry first\r
+\usepackage{listings}\r
 \usepackage{array}\r
 \usepackage{booktabs}\r
 \usepackage{longtable}\r
 \newcommand\code[1]{\texttt{#1}}\r
 \newcommand\pkg[1]{\textbf{#1}}\r
 \setcounter{tocdepth}{2}\r
+\r
+\lstset{language=R,basicstyle=\ttfamily,\r
+        keywordstyle=\ttfamily,\r
+        emphstyle=\itshape,\r
+        escapeinside = ||,\r
+        lineskip=0pt}\r
+\r
 \begin{document}\r
 \r
 \title{The \pkg{xtable} Gallery}\r
@@ -121,6 +129,36 @@ temp.table <- xtable(temp.ts, digits = 0)
 temp.table\r
 @\r
 \r
+\subsection{Mathematical arrays}\r
+\label{sec:mathematical-arrays}\r
+\r
+Mathematical arrays (as compared to data arrays) can be printed\r
+correctly by declaring the class as \code{xtableMatharray}.\r
+<<matharraydata>>=\r
+V <- matrix(c(1.140380e-03,  3.010497e-05,  7.334879e-05,\r
+              3.010497e-05,  3.320683e-04, -5.284854e-05,\r
+              7.334879e-05, -5.284854e-05,  3.520928e-04), nrow = 3)\r
+V\r
+class(V) <- c("xtableMatharray")\r
+class(V)\r
+xtbl <- print(xtable(V, display = rep("E", 4)))\r
+@ %def\r
+\r
+Then \code{xtbl} can be used in an expression such as\r
+$V=\left[\Sexpr{xtbl}\right]$, produced by\r
+\begin{lstlisting}\r
+V=\left[\Sexp||r{xtbl}\right]. \r
+\end{lstlisting}\r
+\r
+\r
+An alternative display can be created using the \texttt{digits}\r
+argument. If \code{xtbl} is defined instead according to\r
+\r
+<<altmatharray, results = 'hide'>>=\r
+xtbl <- print(xtable(V, digits = 6))\r
+@ %def \r
+then we get\\\r
+$V=\left[\Sexpr{paste(print(xtable(V, digits = 6)))}\right].$ \r
 \newpage\r
 \r
 <<include=FALSE>>=\r
@@ -209,6 +247,7 @@ print(wanttex, sanitize.text.function =
       function(str) gsub("_", "\\_", str, fixed = TRUE))\r
 @\r
 \r
+\r
 \newpage\r
 \r
 \subsection{Markup in tables}\r
@@ -237,6 +276,8 @@ money <- matrix(c("$1,000","$900","$100"), ncol = 3,
 print(xtable(money), sanitize.rownames.function = function(x) {x})\r
 @\r
 \r
+\r
+\r
 \section{Format examples}\r
 \subsection{Adding a centering environment}\r
 <<results='asis'>>=\r
@@ -291,6 +332,27 @@ digits(tli.table) <- matrix(0:4, nrow = 10, ncol = ncol(tli)+1)
 tli.table\r
 @\r
 \r
+\subsection{Minus signs}\r
+\label{sec:minus-signs}\r
+\r
+The standard print functions do not render minus signs properly. They\r
+are actually hyphens (-) rather than minus signs ($-$). True minus\r
+signs can be obtained using the \code{math.style.negative} argument.\r
+\r
+\p\r
+Here is the \LaTeX{} which is produced:\r
+\r
+<<>>=\r
+pr1 <- prcomp(USArrests)\r
+print(xtable(pr1), math.style.negative = TRUE)\r
+@ \r
+\r
+And here is the resulting table.\r
+<<results='asis'>>=\r
+print(xtable(pr1), math.style.negative = TRUE)\r
+@\r
+\r
+\r
 \newpage\r
 \r
 \subsection{Suppress row/column names}\r