Title: | HTML Writer - Outputs R Objects in HTML Format |
---|---|
Description: | Easy-to-use and versatile functions to output R objects in HTML format. |
Authors: | Gregoire Pau <[email protected]> |
Maintainer: | Gregoire Pau <[email protected]> |
License: | LGPL-2.1 |
Version: | 1.3.2.1 |
Built: | 2024-12-12 06:51:02 UTC |
Source: | CRAN |
hwriter
is an easy-to-use package able to format and
output R objects in HTML format. It supports advanced formatting,
tables, CSS styling, Javascript, images and provides a convenient mapping between
R tables and HTML tables. hwriter
generates XHTML 1.0 transitional
HTML code.
See Examples and hwrite
for more details.
hwriter
provides the following functions (but most of the job is
carried out by hwrite
):
hwrite
outputs an R object in HTML format.
hwriteImage
writes an image.
openPage
, closePage
handles HTML page/document
creation.
hmakeTag
is a low-level HTML tag formatting function.
Gregoire Pau, [email protected], 2008
hwrite
, hwriteImage
, openPage
, closePage
, hmakeTag
.
hwriter:::showExample()
hwriter:::showExample()
Formats an HTML/XML tag, using a low-level syntax.
hmakeTag(tag, data=NULL, ..., newline=FALSE)
hmakeTag(tag, data=NULL, ..., newline=FALSE)
tag |
a character vector or matrix containing the HTML/XML tags. |
data |
a character vector or matrix containing the tag bodies. |
newline |
a logical. Appends a newline |
... |
optional attributes that will be appended to the tags. |
This low-level function is used by hwrite
to build HTML tags.
This function is useful to build non-standard or rare HTML tags.
A character vector or matrix, containing the output HTML/XML tags.
Gregoire Pau, [email protected], 2008
## simple call hmakeTag('a','Centipede',href='http://en.wikipedia.org/wiki/Centipede') ## vectorized calls hmakeTag('tag',1:10,color='red') hmakeTag(colors()[1:10],1:10,semantic='color') ## nested calls hmakeTag('html',hmakeTag('body','Text'),'xml:lang'='en')
## simple call hmakeTag('a','Centipede',href='http://en.wikipedia.org/wiki/Centipede') ## vectorized calls hmakeTag('tag',1:10,color='red') hmakeTag(colors()[1:10],1:10,semantic='color') ## nested calls hmakeTag('html',hmakeTag('body','Text'),'xml:lang'='en')
Outputs an R object in HTML format.
hwrite(x, page=NULL, ...)
hwrite(x, page=NULL, ...)
x |
an R object to output in HTML format. Objects of type |
page |
an optional connection, a character string naming the file to write
to or a page object returned by |
... |
optional arguments. See Details. |
If x
is a vector of only one element, it will be written by default
as an HTML text element unless table
is TRUE
: in that case,
it will be written as an HTML table containing an unique element.
If x
is a vector of more than one element, a matrix or a data.frame, it will be
written by default as an HTML table unless table
is FALSE
: in that case,
it will be written as a vector or a matrix of HTML text elements.
Many optional arguments can be used to render an HTML object. See below for
additional information. Many comprehensive examples can be found in the
Examples section by typing example(hwrite)
.
A character vector containing the output HTML code.
The following optional arguments can always be used:
a logical specifying if a breakline (carriage return) should
be appended at the end of x
. Default is FALSE
.
a logical controlling if the object x
should be written as an HTML table. Default is TRUE
for matrices and vectors containing more than one element, and FALSE
otherwise. If set to FALSE
, the object is written as a vector (or a matrix) of HTML text elements.
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 <a>.
a character string naming the HTML element for further reference. This is the equivalent of the attribute name
of the HTML tag <a>.
a logical. If TRUE
, places the HTML element into a HTML section, using the <div> HTML tag. This is helpful for styling a section. Default is FALSE
.
a logical indicating if x
should be centered. Default is FALSE
. This element may interfere with the current CSS style. Please consider the use the CSS style attribute "text-align" instead.
Additional arguments are added to the HTML element as HTML attributes. For HTML tables, attributes are distributed on table cells using R recycling rules. For text elements, a <span> HTML tag (or <div> if div
is TRUE
) is used to accommodate the attributes.
If x
is rendered as an HTML text element, the following optional arguments can be used:
a numeric containing the heading level style. Valid values spans from 1 to 5. See Examples.
If x
is a vector with more than one element, the following optional
arguments can be used:
a couple of optional numeric values indicating the desired number of rows and columns in the table. This is useful to orient a vector.
logical. If TRUE
, the table is filled by
rows first, otherwise the table is filled by columns first. Default is FALSE
.
a logical indicating if the names of the elements should
be written if the vector is named. Default is TRUE
.
If x
is rendered as an HTML table element, the following optional arguments can be used:
a numeric. Specifies the table border width. A value of 0 implies that no borders will be drawn. This argument may interfere with the "border" CSS style attribute.
a logical value indicating whether the row (resp. column) names of
x
are to be written. Default is TRUE
.
a numeric. Defines the spacing and padding space in pixels between cells. These arguments may interfere with the "border" and "padding" CSS style attributes.
a character string. Specifies the global table width in HTML units (pixels or %).
a named character vector. Specifies the columns width
in HTML units (pixels or %) where names of col.width
are used to
point column names of x
. NAs may be used to let several column
widths unspecified.
a list of character vectors or a character vector. Distributes the attribute '*' on the HTML table cells, according to rows (resp. columns). Named lists (or vectors) point the corresponding rows/columns, according to their names. Unnamed lists (or vectors) point the rows/columns in the numeric order and NAs can be used to omit rows/columns. If pointed rows/columns sizes don't match, vector values are recycled using R rules.
a character string. Uses the global table attribute '*' to render the HTML table. The attribute is added to the main <table> tag. Some uses include setting of the "border" and "margin" CSS attributes that cannot be distributed on cells.
a character string, vector or matrix. Distributes the attribute '*' on the HTML table cells, using R recycling rules. Any valid HTML attributes can be used. The value may contain NAs to omit cells. Matrices may contain one extra row and/or column to target heading cells.
See Examples for many illustrated examples of all arguments.
Gregoire Pau, [email protected], 2008
openPage
, closePage
, hwriteImage
, hmakeTag
.
hwriter:::showExample()
hwriter:::showExample()
Inserts one or several images in an HTML document. The images can be either external ones specified by URL or file path, or captured from the current graphic device.
hwriteImage(image.url, page=NULL, ..., image.border=0, width=NULL, height=NULL, capture=FALSE)
hwriteImage(image.url, page=NULL, ..., image.border=0, width=NULL, height=NULL, capture=FALSE)
image.url |
a character vector or matrix containing the URL or the file path of images. |
page |
an optional connection, a character string naming the file to write
to or a page object returned by |
image.border |
an optional numeric value specifiying the width of the image border. Default is 0. |
width , height
|
an optional HTML length unit (in pixels) specifiying the width (resp. height) at which the image should be rendered. If missing, the default image width (resp. height) will be used. |
capture |
a logical. If |
... |
optional arguments that will be dispatched to the
underlying |
hwriteImage
constructs an HTML <img> tag to insert one or several images. This function can be seamlessly in conjuction with hwrite
to position an image. The capture
argument enables to capture
easily a current plot and to insert it in a web page.
By default, if image.url
is a vector the output value will be a
character string containing the HTML code of a table containing the
images. This behaviour is dictated by the underlying hwrite
call
made by hwriteImage
. The argument table
can be set to
TRUE
to obtain a vector of HTML image tags instead.
A character vector containing the output HTML code.
Gregoire Pau, [email protected], 2008
## Creates a new web page 'test.html' tmpdir <- tempdir() p <- openPage('test.html', dirname=tmpdir) ## Insert an external image img <- hwriteImage('http://www.ebi.ac.uk/~gpau/hwriter/iris1.jpg', center=TRUE) hwrite(c(img,caption='Iris'), p, dim=c(2,1), row.style=list(caption='text-align:center;background-color:#fac'), row.names=FALSE, br=TRUE) ## Closes the web page closePage(p) ## Opens a web browser to see the result if (interactive()) try(browseURL(file.path(tmpdir, 'test.html')))
## Creates a new web page 'test.html' tmpdir <- tempdir() p <- openPage('test.html', dirname=tmpdir) ## Insert an external image img <- hwriteImage('http://www.ebi.ac.uk/~gpau/hwriter/iris1.jpg', center=TRUE) hwrite(c(img,caption='Iris'), p, dim=c(2,1), row.style=list(caption='text-align:center;background-color:#fac'), row.names=FALSE, br=TRUE) ## Closes the web page closePage(p) ## Opens a web browser to see the result if (interactive()) try(browseURL(file.path(tmpdir, 'test.html')))
Opens and closes an HTML page/document, allowing a sequential building of an HTML page.
openPage(filename, dirname=NULL, title=filename, link.javascript=NULL, link.css=NULL, css=NULL, head=NULL, charset="utf-8", lang="en", head.attributes=NULL, body.attributes=NULL) closePage(page, splash=TRUE)
openPage(filename, dirname=NULL, title=filename, link.javascript=NULL, link.css=NULL, css=NULL, head=NULL, charset="utf-8", lang="en", head.attributes=NULL, body.attributes=NULL) closePage(page, splash=TRUE)
filename |
a character string containing the filename or the path of the HTML file to be created. |
dirname |
an optional character string containing the path of the directory where the file should be written. |
title |
an optional character string containing the title of the HTML page. |
link.javascript |
an optional character vector containing the URL of Javascripts to be associated with the page. |
link.css |
an optional character vector containing the URL of CSS stylesheets to be associated with the page. |
css |
an optional character vector containing inline CSS stylesheets to be associated with the page. |
head |
an optional character string containing an HTML fragment to be added in the <head> section of the page. |
charset |
an optional character string containing the current charset. Default is "utf-8". |
lang |
an optional character string containing the current langage. Default is "en". |
head.attributes |
an optional named list of character strings, containing the <head> attributes. |
body.attributes |
an optional named list of character strings, containing the <body> attributes. |
page |
a page handle returned by a previous |
splash |
a logical, indicating whether the hwriter splash tag 'generated by...' should be written at the end of the page. |
openPage
opens a new file for writing and returns a page handle
which is used by hwrite
to append HTML elements in a current
page. Any previous existing file will be overwritten.
The argument head
is useful to add extra HTML code in the <head> header code.
closePage
ends the HTML page formatting, flushes the pending
writing operations and closes the file.
A connection which is a handle to the current HTML page.
Gregoire Pau, [email protected], 2008
## Creates a new web page 'test.html' in the R temporary directory tmpdir <- tempdir() p <- openPage('test.html', dirname=tmpdir, link.css='http://www.ebi.ac.uk/~gpau/hwriter/hwriter.css') hwrite('Iris example', p, center=TRUE, heading=1) hwrite(paste('This famous (Fisher\'s or Anderson\'s) iris data set', 'gives the measurements in centimeters of the variables', 'sepal length and width and petal length and width, respectively,', 'for 50 flowers from each of 3 species of iris.'), p, class='king') hwrite(iris, p, row.bgcolor='#ffffaa') closePage(p) ## Opens a web browser on the web page if (interactive()) try(browseURL(file.path(tmpdir, 'test.html')))
## Creates a new web page 'test.html' in the R temporary directory tmpdir <- tempdir() p <- openPage('test.html', dirname=tmpdir, link.css='http://www.ebi.ac.uk/~gpau/hwriter/hwriter.css') hwrite('Iris example', p, center=TRUE, heading=1) hwrite(paste('This famous (Fisher\'s or Anderson\'s) iris data set', 'gives the measurements in centimeters of the variables', 'sepal length and width and petal length and width, respectively,', 'for 50 flowers from each of 3 species of iris.'), p, class='king') hwrite(iris, p, row.bgcolor='#ffffaa') closePage(p) ## Opens a web browser on the web page if (interactive()) try(browseURL(file.path(tmpdir, 'test.html')))