textTable.xtable {tablesgg}R Documentation

Create a texttable from an xtable Object

Description

Create a textTable from an xtable object produced by the xtable package. The textTable can then be styled and plotted.

Usage

## S3 method for class 'xtable'
textTable(x, title, subtitle=character(0), foot=character(0), 
    row.names="", na="", mathExponents=TRUE, ...)

Arguments

x

An xtable object as produced by the xtable package.

title

Optional character vector containing title lines for the table. May be empty (character(0)). The default is to use the first element of the caption attribute of x, if present.

subtitle, foot

Optional character vectors providing additional annotation for the table. May be empty (i.e., character(0), the default).

row.names

A logical scalar or a character string. If FALSE, the row names of x are not included in the table. If TRUE, the row names are included as row headers, with a row header label of "row.names". If a character string, row names are included as row headers, and if the string is not empty, it is used as the row header label.

na

String to be used to represent missing values in x. The default value is the empty string "".

mathExponents

Logical scalar. If TRUE, then numerical values in x that are formatted into scientific notation (i.e., strings like "3.14e-02", specified by values of e or E in the display attribute of x) will be plotted in math style, with the power of 10 shown as a superscript.

...

Additional named arguments passed to formatC, when converting values in x to character strings. They must not include digits or format, which are specified within x itself.

Details

This function was designed based on the structure of objects produced by version 1.8-4 of the xtable package.

An xtable object is a data frame that contains the columns of the table and attributes that specify how those columns are to be formatted. This function uses those attributes to created formatted character strings for each table entry, and assembles them into a textTable object, which may then be styled and plotted.

Formatting is done by formatC using the digits and display attributes of x. The align attribute is used to set the justification attributes in the returned textTable. (Vertical rule characters, |, within align are ignored; use an hvruleStyle or the addHvrule function to insert vertical rules into the plotted table, as shown in the examples.)

Value

An object with S3 class textTable. See the documentation for the generic for details about its structure.

Examples

# 'tli_xtab' is an 'xtable' object created using 'xtable::xtable':
class(tli_xtab)
# This package provides a 'textTable' method for such objects:
ttbl <- textTable(tli_xtab)
plot(ttbl)

if (requireNamespace("xtable", quietly=TRUE)) withAutoprint({
  data(tli, package="xtable")
  
  # ANOVA table.
  fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli)
  plt1 <- plot(textTable(fm1.table <- xtable::xtable(fm1), 
                         title="xtable: ANOVA table"))

  # Table of linear regression results.
  fm2 <- lm(tlimth ~ sex*ethnicty, data = tli)
  plt2 <- plot(textTable(fm2.table <- xtable::xtable(fm2), 
                         title="xtable: Linear regression"))
  
  # Time series table.
  temp.ts <- ts(cumsum(1 + round(rnorm(100), 0)), start = c(1954, 7), 
                frequency = 12)
  plt3 <- plot(textTable(xtable::xtable(temp.ts, digits = 0), 
                         title="xtable: Time series"))
  
  # Math style for scientific notation.
  plt4 <- plot(textTable(xtable::xtable(data.frame(text = c("foo","bar"),
                                                   googols = c(10e10,50e10),
                                                   small = c(8e-24,7e-5),
                                                   row.names = c("A","B")),
                                        display = c("s","s","g","g")),
                         mathExponents = TRUE, 
                     title=c("xtable:", "Math style for scientific notation")))
  print(plt1, position=c(0.1, 0.9))
  print(plt2, position=c(0.1, 0.5), newpage=FALSE)
  print(plt3, position=c(0.1, 0.1), newpage=FALSE)
  print(plt4, position=c(0.9, 0.9), newpage=FALSE)
  
  # By default vertical rules specified by '|' characters in 'align' are 
  # ignored.  They can be added afterward using the 'addHvrule' function 
  # as follows:
  tli.table <- xtable::xtable(tli[1:10, ])
  xtable::align(tli.table) <- "|rrl|l|lr|"
  plt <- plot(textTable(tli.table, 
                        title="xtable: Vertical rules derived from 'align'"))
  pipe_posn <- which(unlist(strsplit(attr(tli.table, "align"), "")) == "|")
  vrule_acol <- pipe_posn - seq_along(pipe_posn) + 0.5
  for (ac in vrule_acol)  plt <- addHvrule(plt, direction="vrule", acols=ac, 
                                           arows=arow(plt, "colhead_and_body"), 
                                           props=element_hvrule(linetype=1, 
                                                                color="black"))
  plt
})
  

[Package tablesgg version 0.9-1 Index]