Generate HTML Table of Data

DESCRIPTION:

Generate a vector of character strings or a bdCharacter representing a vector, a bdVector, a matrix, a data.frame, or a bdFrame as an HTML table. The vector or bdVector will contain one string for each line of HTML. This may be written to a file by specifying the file argument, or may be manipulated and later written to a file using the write function.

USAGE:

html.table(x, file="", append=F, main="", center=F, html.tag=F, table.attributes = "BORDER",
        column.attributes=<<see below>>, cell.attributes="", row.header.attributes="ALIGN_RIGHT",
        row.header.column.attributes="", data.column.attributes="", ...) 

REQUIRED ARGUMENTS:

x
Vector, bdVector, matrix, data.frame, or bdFrame to be placed in table. May also be a list containing vectors, bdVectors, matrices, data frames, or bdFrames, in which case multiple tables will be generated.

OPTIONAL ARGUMENTS:

file
character string naming the file to write to. The empty string "" means the standard output.
append
if TRUE, html.table adds to an existing file; otherwise, it creates a new file (destroying any existing file by that name). Only relevant if file is specified.
main
character string used as table caption if x is not a list, and as a header if x is a list.
center
if TRUE, the result is wrapped in a CENTER tag. This centers the table or tables on the page.
html.tag
if TRUE, the result is wrapped in an HTML tag. This is needed by some browsers for the file to be recognized as HTML.
table.attributes
column.attributes
the row.header.column.attributes and data.column.attributes combined.
cell.attributes
attributes for each data cell. If scalar, this is extended to a matrix the shape of x.
row.header.attributes
attributes for the row headers (the cells to the left of each row). If scalar, this is repeated to be nrow(x) long.
row.header.column.attributes
attributes for the first column (the one consisting of row headers).
data.column.attributes
attributes for all but the first column (the columns containing data cells). These can be a vector or bdVector ncol(x) long or a scalar. If scalar, it is repeated to be ncol(x) long.
...
any arguments to format. These are arguments such as digits, which are useful for formatting the data.

VALUE:

if file=="", a vector of character strings or a or bdCharacter is returned, with each string containing one line of HTML. Otherwise, the HTML is written to the specified file, and the name of the file is returned invisibly.

SIDE EFFECTS:

if file!="", the HTML is written to the specified file.

DETAILS:

"Attributes" can control the color and size of cells in the table, as well as fonts and other things. Typical ones include width="10%" (this column should be 10% the width of the entire table), bgcolor="red" (the background of this cell, column, or row should be red), and align="center" (center data in this cell, column, or row).

SEE ALSO:

,

EXAMPLES:

# Simple examples 
html.table(round(cor(state.x77), digits=4), file="my.htm") 
html.table(catalyst, file="my.htm") 
         
# Multiple matrices 
my.results <- list("Regression Coefficients"=round(coef(lm(Mileage~Weight,  
     fuel.frame)),digits=4), "Correlations"=round(cor(fuel.frame[,1:3]), 
     digits=4)) 
html.table(my.results, file="my.htm") 

# Show correlation matrix with high correlations in red
# and put no borders on anything.
z <- cor(state.x77)
html.table(round(z,2), file="my.htm",
    table.attributes = "",
    cell.attributes = array(dim = dim(z),
    paste("bgcolor=", ifelse(abs(z)>.7 & abs(z)<1.0, "red", "white"))))

# make all data cells the same width (allowing 30% of width for row headers),
# center data cell entries,
# and left justify the row headers
html.table(round(z,2), file="my.htm",
    cell.attributes="align=center",
    data.column.attributes=paste("width=\"",70/ncol(z),"%\"",sep=""),
    row.header.attributes="align=left")

# Time series 
my.lynx <- lynx 
names(my.lynx) <- time(lynx) 
html.table(my.lynx,file="my.htm",main="Lynx Data") 

# Use write() to add other headings or comments 
write("<H3> S-PLUS Code for the above </H3> <P> Put code here </P>",  
    file="my.htm", append=T) 

# Embedding preformatted output 
sink("my.htm") 
cat(sep="", "<", "H3", ">", " Linear Model Results ", "<", "/H3", ">", "\n") 
cat(sep="", "<", "PRE", ">") 
summary(lm(Mileage~Weight, fuel.frame)) 
cat(sep="", "<", "/PRE", ">") 
sink() 

# Embedding a plot in S-PLUS for Windows 
sink("my.htm", append=T) 
cat(sep="", "<", "IMG SRC=\"my.gif\"", ">") 
sink() 
graphsheet(format="GIF", file="my.gif") 
xyplot(Mileage~Weight, fuel.frame) 
dev.off()