ttt: Formatted tables the easy way

Description Usage Arguments Value Methods (by class) Examples

View source: R/ttt.R

Description

ttt stands for “The Table Tool” (or, if you prefer, “Tables! Tables! Tables!”). It allows you to creates formatted HTML tables of in a flexible and convenient way.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
ttt(x, ...)

## S3 method for class 'data.frame'
ttt(
  x,
  formula,
  ...,
  render,
  lab,
  caption,
  footnote,
  expand.along = c("rows", "columns"),
  drop = c("both", "rows", "columns", "none"),
  collapse.cells = TRUE,
  topclass = NULL,
  id = NULL,
  css = NULL,
  row.names = TRUE
)

## S3 method for class 'formula'
ttt(
  x,
  data,
  ...,
  render,
  lab,
  caption,
  footnote,
  expand.along = c("rows", "columns"),
  drop = c("both", "rows", "columns", "none"),
  collapse.cells = TRUE,
  topclass = NULL,
  id = NULL,
  css = NULL
)

## S3 method for class 'numeric'
ttt(
  x,
  rowvars,
  colvars,
  ...,
  render,
  lab,
  caption,
  footnote,
  expand.along = c("rows", "columns"),
  drop = c("both", "rows", "columns", "none"),
  collapse.cells = TRUE,
  topclass = NULL,
  id = NULL,
  css = NULL
)

## S3 method for class 'ftable'
ttt(
  x,
  text = matrix(as.character(x), nrow(x)),
  lab,
  caption,
  footnote,
  drop = c("both", "rows", "columns", "none"),
  collapse.cells = TRUE,
  html.class = NULL,
  topclass = NULL,
  id = NULL,
  css = NULL,
  ...
)

Arguments

x

An object.

...

Additional arguments passed to render.

formula

A three-part formula of the form v ~ r1 + r2 ~ c1 + c2 where v specifies a column of values, while r1, r2 specify row variables and c1, c2 column variables for splitting the values.

render

A function to render the contents of each cell to character data.

lab

Specify the contents of an extra table cell spanning over all column labels.

caption

A character string to be added as a caption to the table. The default is to omit the caption.

footnote

A character string to be added as a footnote to the table. The default is to omit the footnote.

expand.along

Specify the direction to expand the table when render returns a (named) vector.

drop

If TRUE (the default), rows and columns with zero counts will be dropped.

collapse.cells

If TRUE (the default), row/column header cells will be collapsed (merged) where appropriate.

topclass

A character string to be used as class attribute for the top-level <table> element.

id

A character string to be used as id attribute for the top-level <table> element.

css

A character string containing CSS code to be added before the top-level <table> element.

row.names

If TRUE (the default), row names will be shown in the first column of the table. Set to FALSE to suppress row names. Only applies when displaying whole data.frame.

data

A data.frame.

rowvars

A list of row variables for splitting the data.

colvars

A list of column variables for splitting the data.

text

A character matrix containing the textual content of each table cell.

html.class

A character matrix with the same dimensions as text specifying a class attribute for the corresponding <td> element.

Value

A character which contains an HTML table fragment. It has additional class attributes that cause it to be displayed in a browser in an interactive context, and rendered as HTML in a knitr context.

Methods (by class)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# mtcars examples
ttt(mtcars)
ttt(mtcars, mpg ~ gear | cyl, lab="Cylinders")
ttt(mpg ~ gear | cyl, data=mtcars, lab="Cylinders")
ttt(rownames(mtcars) ~ gear | cyl, data=mtcars,
  render=paste, collapse="<br/>", lab="Cylinders")

# OrchardSprays examples
ttt(head(OrchardSprays, 12))
ttt(head(OrchardSprays, 12), row.names=FALSE)
ttt(treatment ~ rowpos | colpos, data=OrchardSprays, lab="colpos")
ttt(paste(treatment, decrease, sep="<br/>") ~ rowpos | colpos, data=OrchardSprays, lab="colpos")

rndr.meansd <- function(x) formatC(c(Mean=mean(x), SD=sd(x)), digits=3)
ttt(decrease ~ treatment, data=OrchardSprays, render=rndr.meansd, expand.along="rows")
ttt(decrease ~ treatment, data=OrchardSprays, render=rndr.meansd, expand.along="columns")

# ToothGrowth examples
ttt(len ~ dose | supp, data=ToothGrowth, lab="Mean (SD)",
  render=function(x) sprintf("%0.3g (%0.3g)", mean(x), sd(x)))

ttt(len ~ dose | supp, data=ToothGrowth, lab="Supplement Type",
  render=rndr.meansd)

ttt(len ~ dose | supp, data=ToothGrowth, lab="Supplement Type",
  render=rndr.meansd, expand.along="columns")

ttt documentation built on May 7, 2021, 5:06 p.m.