Learn R Programming

hwriterPlus (version 1.0-3)

hwriteOutput: Write R Output or an R Script in HTML Format

Description

hwriteOutput is a wrapper function for hwrite which formats output from R captured by capture.output so it appears correctly when hwrite is called.

hwriteScript is a very similar wrapper function for hwrite which formats an R session captured by script so it appears correctly when hwrite is called. hwriteScript also optionally trims lines added at the beginning and end of the session by script.

Usage

hwriteOutput(output, page = NULL, fontSize = "10pt", ..., link = NULL, name = NULL, br = NULL, div = NULL) hwriteScript(scriptFile, page = NULL, fontSize = "10pt", trim = TRUE, ..., link = NULL, name = NULL, br = NULL, div = NULL)

Arguments

output
Text. Output produced by R, captured by capture.output
page
An optional connection. A character string naming the file to write to or a page object returned by newPage or openPage.
fontSize
Character string. The font size in points in which the output will be displayed. A positive number followed by the characters "pt" without a space between. Default is "10pt".
scriptFile
File. Contains text from an R session captured by script.
trim
A logical. If TRUE, deletes the first and last two lines of the lines of text returned by script. Default is TRUE.
...
Optional arguments. See Details section of hwrite documentation.
link
A character vector containing the URLs the HTML element will point to. This argument is the equivalent of the attribute href of the HTML tag .
name
A character string naming the HTML element for further reference. This is the equivalent of the attribute name of the HTML tag.
br
A logical specifying if a breakline (carriage return) should be appended at the end of x. Default is FALSE.
div
A logical. If TRUE, places the HTML element into a HTML section, using the
HTML tag. This is helpful for styling a section. Default is FALSE.

Value

A character vector containing the output HTML code.

Details

The output obtained from R using capture.output is modified by these functions so that it will appear verbatim in the resulting HTML file, as preformatted text.

The output is first modified by adding characters "\n" to the end of each output line. Then preformatting tags are added to surround the output. The opening preformatting tag includes a style definition which sets the font size to that specified by fontSize.

In addition hwriteScript can trim off the first line and the last two lines of the text returned by a call to script. The first and last lines record the start and end times of the call to script. The second last line typically consists of the text > q() comprised of the R prompt and the call to q() which terminates the recording of the session by script.

See Also

hwrite

Examples

Run this code
### Example results written to standard output stream
### Annette Dobson (1990) "An Introduction to Generalized Linear Models".
### Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
plants <- data.frame(weight = weight, group = group)
strOutput <- capture.output(str(plants))
lm.D9 <- lm(weight ~ group, data = plants)
anovaOutput <- capture.output(anova(lm.D9))
hwrite("Analysis of Weight Data", heading = 1,
       center = FALSE, br = TRUE)
hwriteOutput(strOutput, fontSize = "8pt")
hwriteOutput(anovaOutput)

### Same example but written to a file
tmpDir <- tempdir()
fileName <- file.path(tmpDir, 'hwriterPlus.html')
p <- newPage(fileName)
hwrite("Analysis of Weight Data", p, heading = 1,
       center = FALSE, br = TRUE)
hwriteOutput(strOutput, p, fontSize = "8pt")
hwriteOutput(anovaOutput, p)
closePage(p)
### Open a web browser and examine the resulting file
if (interactive()) try(browseURL(fileName))


### Record of session captured by script written to temporary file
## Not run: tmpDir <- tempdir()
# tmpFile <- file.path(tmpDir, 'script.txt')
# script(tmpFile)
# ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
# trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
# group <- gl(2,10,20, labels=c("Ctl","Trt"))
# weight <- c(ctl, trt)
# plants <- data.frame(weight = weight, group = group)
# lm.D9 <- lm(weight ~ group, data = plants)
# q()
# sessionOut <- readLines(tmpFile)
# sessionOut
# hwriteScript(tmpFile, br = TRUE)
# hwriteScript(tmpFile, trim = FALSE, fontSize = "8pt", br = TRUE)
# ## End(Not run)

Run the code above in your browser using DataLab