Learn R Programming

prettyR (version 2.2-3)

delim.table: Format a 2D table

Description

Format a 2D table with delimiters and other formatting commands

Usage

delim.table(x,filename="",delim=",",tabegin="",bor="",eor="\n",tablend="",
  label=deparse(substitute(x)),header=NULL,trailer=NULL,html=FALSE,
  show.rownames=TRUE,leading.delim=FALSE,show.all=FALSE,nsignif=4,
  con,open.con=FALSE)

Arguments

x

A list, matrix or data frame that is to be formatted.

filename

Name for a file to which the result will be written.

delim

The delimiter to place between entries in the table(s).

tabegin

Any formatting commands to be placed before the table.

bor

The formatting command for the beginning of a table row.

eor

The formatting command for the end of a table row.

tablend

Any formatting commands to be placed after the table.

label

A label to be displayed before the table.

header

A character string to be written at the beginning of the output file.

trailer

A character string to be written at the end of the output file.

html

If TRUE, all table formatting commands that are not explicitly specified will be altered to HTML tags.

show.rownames

Whether to display the rownames of a table.

leading.delim

Whether to add an extra delimiter at the beginning of the table. See Details.

show.all

Whether to show all the components of a list. The default FALSE is to show only those components that look like 2D tables.

nsignif

Number of significant digits for numeric display.

con

A connection to which the output will be written. If a filename is passed, it will be ignored if con is not missing.

open.con

A flag for an open connection. This argument is used by the function to keep track of connections in recursive calls and should not be altered by the user.

Value

nil

Details

delim.table tries to format its first argument into one or more tables that will be displayed in another application. The most common use is to produce a CSV style file that can be imported into a spreadsheet. The default values for delim and eor should be adequate for this, and all the user has to do is to supply a filename as in the first example. When a filename is provided, the function attempts to open the file, write its output to it and close it again. In order to deal with the multilevel lists that are often produced by other functions, the function calls itself until it reaches the lowest level of the list, where it can successfully format the contents. Thus the function only passes the connection, not the filename, in recursive calls. If the user passes both a filename and a valid connection, the output will be written to the connection and the filename will not be used.

delim.table will fail if passed a table with more than two dimensions. However, the function will process 2D "slices" of such tables if called with one of the apply family of functions or manually for each slice.

delim.table can also be used to format HTML tables as in the second example. If the user wants different tags from the defaults, pass these explicitly. In principle, any markup language that can produce a table using commands that include; commands to begin and end the table, a command to start and end a row, and a command to start a new cell.

delim.table is consistent with the default CSV arguments, adding an extra delimiter to the first line if there are row names. The user should set leading.delim to FALSE for a table without the empty cell at the upper left. When delim.table is used to format tables for htmlize, it should not attempt to open a new connection.

An unexpected use of delim.table is producing tables that can be imported into OpenOffice Writer and most other word processors. While the tables in an HTML listing can be imported directly, the formatting is usually not suitable. If a table is output to an HTML or text document formatted with TAB characters as the delimiter as in the third example, the output can be copied and pasted into the word processor document and then converted to a table.

See Also

htmlize

Examples

Run this code
# NOT RUN {
 testdf<-data.frame(a=sample(0:1,100,TRUE),b=rnorm(100),c=rnorm(100))
 testglm<-summary(glm(a~b*c,testdf,family="binomial"))
 # produce a CSV file to import into a spreadsheet, just the coefficients
 delim.table(testglm$coefficients,"testglm.csv")
 # now create an HTML file of the three tables in the result
 # add a background color different from the default
 delim.table(testglm,"testglm.html",header="<html><body bgcolor=\"#ffaaff\">",
  html=TRUE)
 # these tables can be pasted into a word processor and converted
 # using "Insert|Table" or similar commands
 delim.table(testglm,"testglm.tab",delim="\t",leading.delim=FALSE)
 # to clean up, delete the files "testglm.csv", "testglm.tab" and "testglm.html"
# }

Run the code above in your browser using DataLab