Description Usage Arguments Value Author(s) See Also Examples
Prints an object as a HTML table. Currently, only data.frame
and matrix
objects are supported.
Using the on
functions one can control the layout and the contents
of the table in full.
1 2 |
x |
Object whose values are to be printed in a table. |
... |
Named attribute list for the |
onTh |
Function to be called when a |
onTr |
Function to be called when a |
onTd |
Function to be called when a |
Returns nothing.
Henrik Bengtsson (http://www.braju.com/R/)
*printList()
,
*printOl()
,
*printUl()
and
*printDl()
.
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | require(R.lang) # load class System
# Writes the HTML code directly to the standard output and
# buffered to file. Note that the file will not be filled
# until the buffer stream flushed.
stdout <- PrintStream(ConnectionOutputStream(stdout()))
file <- File$createTempFile(suffix=".html")
fout <- FileOutputStream(file)
out2 <- BufferedOutputStream(fout)
mout <- MultiOutputStream(stdout, out2)
out <- HtmlPrintStream(mout)
# Simulate a data frame to be written to the HTML page
x <- rnorm(27)
x <- matrix(x, ncol=3)
colnames(x) <- c("A", "B", "C")
df <- as.data.frame(x)
# ----------------------------------------------------------------------
# Start writing the HTML document
# ----------------------------------------------------------------------
# Write document type
writeDocType(out)
# Start by writing a time stamp comment
printTimestamp(out)
# ----------------------------------------------------------------------
# Write the header
# ----------------------------------------------------------------------
pushTag(out, "html")
pushTag(out, "head")
printTag(out, "title", "Example of HtmlPrintStream")
popTag(out)
printTag(out, "meta", content="text/html; charset=iso-8859-1",
"http-equiv"="Content-Type", endTag=FALSE)
printTag(out, "meta", name="Author",
content="[R] and R.classes, http://www.r-project.org", endTag=FALSE)
pushTag(out, "body")
printComment(out, "B O D Y")
# ----------------------------------------------------------------------
# Write simple table
# ----------------------------------------------------------------------
printTag(out, "h1", "Easiest way to print a data frame")
pushTag(out, "center")
printTable(out, df, digits=3)
popTag(out)
# ----------------------------------------------------------------------
# Write advanced table
# ----------------------------------------------------------------------
printTag(out, "h1", "Advanced way to print a data frame")
pushTag(out, "center")
onTh <- function(column, hout, value) {
pushTag(hout, "th", bgcolor="blue", newline=FALSE)
pushTag(hout, "font", color="white", newline=FALSE)
print(hout, value);
popTag(hout, indent=FALSE)
popTag(hout, indent=FALSE)
}
onTd <- function(row, column, hout, value) {
# Every second line should be yellow
bgcolor <- c("yellow", "white")
idx <- (row - 2*as.integer(row/2)) + 1
pushTag(hout, "td", bgcolor=bgcolor[idx], align="right", newline=FALSE)
# Negative values should be in red
value <- as.numeric(value)
valueStr <- formatC(value, format="f", digits=3, width="0")
if (!is.na(value) && value < 0) {
pushTag(hout, "font", color="red", newline=FALSE)
print(hout, valueStr)
popTag(hout, indent=FALSE)
} else
print(hout, valueStr)
popTag(hout, indent=FALSE)
}
printTable(out, df, border=1)
popTag(out)
# ----------------------------------------------------------------------
# Finish writing the HTML document
# ----------------------------------------------------------------------
# Pop all tags left on the tag stack
popTags(out)
# Finish by writing a time stamp comment
printTimestamp(out)
# Don't forget to close the output stream(s)!
close(out)
cat("Tries to open the file in the default browser...\n")
System$openBrowser(file)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.