### ftable objects, requested by Charles Roosen
### Feature request #2248, 2/9/2012
xtableFtable <- function(x, caption = NULL, label = NULL, align = NULL,
- digits = NULL, display = NULL,
+ digits = 0, display = NULL,
quote = FALSE,
method = c("non.compact", "row.compact",
"col.compact", "compact"),
- lsep = " | ", ...) {
+ lsep = " $\\vert$ ", ...) {
method <- match.arg(method)
saveMethod <- method
xDim <- dim(x)
nRowVars <- length(attr(x, "row.vars"))
nColVars <- length(attr(x, "col.vars"))
- if (nRowVars ==0){
+ if (nRowVars == 0){
if (method =="col.compact"){
method <- "non.compact"
} else if (method == "compact"){
method <- "row.compact"
}
}
- if (nColVars ==0){
+ if (nColVars == 0){
if (method =="row.compact"){
method <- "non.compact"
} else if (method == "compact"){
NA.string = getOption("xtable.NA.string", ""),
only.contents = getOption("xtable.only.contents", FALSE),
add.to.row = getOption("xtable.add.to.row", NULL),
+ sanitize.text.function = getOption("xtable.sanitize.text.function", as.is),
sanitize.rownames.function = getOption("xtable.sanitize.rownames.function",
sanitize.text.function),
sanitize.colnames.function = getOption("xtable.sanitize.colnames.function",
timestamp = getOption("xtable.timestamp", date()),
...) {
if (type == "latex"){
+ ## extract the information in the attributes
caption <- attr(x, "ftableCaption")
label <- attr(x, "ftableLabel")
align <- attr(x, "ftableAlign")
method <- attr(x, "method")
lsep <- attr(x, "lsep")
nCharRows <- attr(x, "nChars")[1]
- fmtFtbl <- stats:::format.ftable(x, quote = quote, digits = digits,
- method = method, lsep = lsep)
+ nCharCols <- attr(x, "nChars")[2]
+ nRowVars <- length(attr(x, "row.vars"))
+ nColVars <- length(attr(x, "col.vars"))
+
+ ## change class so format method will find format.ftable
+ ## even though format.ftable is not exported from 'stats'
+ class(x) <- "ftable"
+ fmtFtbl <- format(x, quote = quote, digits = digits,
+ method = method, lsep = lsep)
attr(fmtFtbl, "caption") <- caption
attr(fmtFtbl, "label") <- label
+
+ ## sanitization is possible for row names and/or column names
+ ## row names
+ if (is.null(sanitize.rownames.function)) {
+ fmtFtbl[nCharRows, 1:nRowVars] <-
+ sanitize(fmtFtbl[nCharRows, 1:nRowVars], type = type)
+ } else {
+ fmtFtbl[nCharRows, 1:nRowVars] <-
+ sanitize.rownames.function(fmtFtbl[nCharRows, 1:nRowVars])
+ }
+ ## column names
+ if (is.null(sanitize.colnames.function)) {
+ fmtFtbl[1:nColVars, nCharCols - 1] <-
+ sanitize(fmtFtbl[1:nColVars, nCharCols - 1],
+ type = type)
+ } else {
+ fmtFtbl[1:nColVars, nCharCols - 1] <-
+ sanitize.colnames.function(fmtFtbl[1:nColVars, nCharCols - 1])
+ }
+ ## rotations are possible
+ if (rotate.rownames){
+ fmtFtbl[1:dim(fmtFtbl)[1], 1:(nCharCols - 1)] <-
+ paste0("\\begin{sideways} ",
+ fmtFtbl[1:dim(fmtFtbl)[1], 1:(nCharCols - 1)],
+ "\\end{sideways}")
+ }
+ if (rotate.colnames){
+ if (rotate.rownames){
+ fmtFtbl[1:(nCharRows), (nCharCols):dim(fmtFtbl)[2]] <-
+ paste0("\\begin{sideways} ",
+ fmtFtbl[1:(nCharRows), (nCharCols):dim(fmtFtbl)[2]],
+ "\\end{sideways}")
+ } else {
+ fmtFtbl[1:(nCharRows), 1:dim(fmtFtbl)[2]] <-
+ paste0("\\begin{sideways} ",
+ fmtFtbl[1:(nCharRows), 1:dim(fmtFtbl)[2]],
+ "\\end{sideways}")
+ }
+ }
+
+
+ ## booktabs is incompatible with vertical lines in tables
+ if (booktabs) align <- gsub("|","", align, fixed = TRUE)
attr(fmtFtbl, "align") <- align
attr(fmtFtbl, "digits") <- digits
attr(fmtFtbl, "quote") <- quote
attr(fmtFtbl, "display") <- display
- print.xtable(fmtFtbl, hline.after = c(-1, nCharRows, dim(fmtFtbl)[1]),
- include.rownames = FALSE, include.colnames = FALSE)
+
+ ## labels should be left aligned
+ for (i in 1:nCharRows){
+ fmtFtbl[i, nCharCols:dim(fmtFtbl)[2]] <-
+ paste0("\\multicolumn{1}{l}{ ",
+ fmtFtbl[i, nCharCols:dim(fmtFtbl)[2]], "}")
+ }
+
+
+ if(is.null(hline.after)) {
+ hline.after <- c(-1, nCharRows, dim(fmtFtbl)[1])
+ }
+ print.xtable(fmtFtbl, hline.after = hline.after,
+ include.rownames = FALSE, include.colnames = FALSE,
+ booktabs = booktabs,
+ sanitize.text.function = as.is,
+ file = file,
+ append = append,
+ floating = floating,
+ floating.environment = floating.environment,
+ table.placement = table.placement,
+ caption.placement = caption.placement,
+ caption.width = caption.width,
+ latex.environments = latex.environments,
+ tabular.environment = tabular.environment,
+ size = size,
+ NA.string = NA.string,
+ only.contents = only.contents,
+ add.to.row = add.to.row,,
+ math.style.negative = math.style.negative,
+ math.style.exponents = math.style.exponents,
+ print.results = print.results,
+ format.args = format.args,
+ scalebox = scalebox,
+ width = width,
+ comment = comment,
+ timestamp = timestamp,
+ ...)
} else {
stop("print.xtableFtable not yet implemented for this type")
}