exportDF: Export a Data Frame as HTML/TEX Code

View source: R/plainMethod_exportDF.r

exportDFR Documentation

Export a Data Frame as HTML/TEX Code

Description

Generates code to include tabular data in a tex document or web site.

Usage

exportDF(
  x,
  tex = FALSE,
  colnames = NULL,
  width = NULL,
  align = NULL,
  funHead = NULL,
  funCell = NULL,
  lines = TRUE,
  indent = 2
)

Arguments

x

The data frame being exported.

tex

Logical. Allows to switch between generation of TEX code and HTML.

colnames

Displayed column names. If NULL, the original names of x are used. Otherwise it must be a named vector with element names corresponding to column names in x. It is OK to supply alternative names for selected columns only.

width

Either NULL (all columns get equal width) or a named vector with element names corresponding to column names in x. If tex == TRUE, values (between 0 and 1) are needed for columns with align code 'p' only. They are interpreted as a multiplier for '\textwidth'. If tex == FALSE, values (between 0 and 100) should be supplied for all columns of x.

align

Either NULL (to use automatic alignment) or a named vector with element names corresponding to column names in x. If tex == FALSE valid alignment codes are 'left', 'right', 'center'. If tex == TRUE valid alignment codes are 'l', 'r', 'c', and 'p'. For columns with code 'p' a corresponding value of width should be set. It is OK to supply alignment codes for selected columns only.

funHead

Either NULL or a list of functions whose names correspond to column names of x. The functions should have a single formal argument; the respective column names of x are used as the actual arguments. It is OK to supply functions for selected columns only (an empty function is applied to the remaining columns). See below for some typical examples.

funCell

Like funHead but these functions are applied to the cells in columns rather that to the column names.

lines

Logical. Switches table borders on/off.

indent

Integer. Number of blanks used to indent the generated code.

Value

A character string (usually needs to be exported to a file).

Note

The functions funHead and funCell are useful to apply formatting or character replacement. For example, one could use

function(x) {paste0("\\bold{",toupper(x),"}")}

to generate bold, uppercase column names in a TEX table.

Author(s)

David Kneis david.kneis@tu-dresden.de

See Also

The xtable packages provides similar functionality with more sophisticated options. Consider the 'pandoc' software do convert documents from one markup language to another one. Finally, consider the latex package 'datatools' for direct inclusion of delimited text files (e.g. produced by write.table) in tex documents.

Examples

# Create example table
df <- data.frame(stringsAsFactors=FALSE, name= c("growth", "dead"),
  unit= c("1/d","1/d"), expression= c("r * N * (1 - N/K)"," d * N"))

# Export as TEX: header in bold, 1st colum in italics, last column as math
tex <- exportDF(df, tex=TRUE,
  colnames=c(expression="process rate expression"),
  width=c(expression=0.5),
  align=c(expression="p"),
  funHead=setNames(replicate(ncol(df),
    function(x){paste0("\\textbf{",x,"}")}),names(df)),
  funCell=c(name=function(x){paste0("\\textit{",x,"}")},
    expression=function(x){paste0("$",x,"$")})
)
cat(tex,"\n")

# Export as HTML: non-standard colors are used for all columns
tf <- tempfile(fileext=".html")
write(x= exportDF(df, tex=FALSE,
  funHead=setNames(replicate(ncol(df),
    function(x){paste0("<font color='red'>",x,"</font>")}),names(df)),
  funCell=setNames(replicate(ncol(df),
    function(x){paste0("<font color='blue'>",x,"</font>")}),names(df))
), file=tf)
## Not run: 
  browseURL(tf)
  file.remove(tf)

## End(Not run)

dkneis/rodeo documentation built on Jan. 4, 2024, 2:18 p.m.