]> git.donarmstrong.com Git - xtable.git/blob - pkg/R/xtableFtable.R
c4e036963be2ff5e16fdc2c8838dcbc74f61b4db
[xtable.git] / pkg / R / xtableFtable.R
1 ### ftable objects, requested by Charles Roosen
2 ### Feature request #2248, 2/9/2012
3 xtableFtable <- function(x, caption = NULL, label = NULL, align = NULL,
4                          digits = 0, display = NULL,
5                          quote = FALSE,
6                          method = c("non.compact", "row.compact",
7                                     "col.compact", "compact"),
8                          lsep = " $\\vert$ ", ...) {
9   method <- match.arg(method)
10   saveMethod <- method
11   xDim <- dim(x)
12   nRowVars <- length(attr(x, "row.vars"))
13   nColVars <- length(attr(x, "col.vars"))
14   if (nRowVars == 0){
15     if (method =="col.compact"){
16       method <- "non.compact"
17     } else if (method == "compact"){
18       method <- "row.compact"
19     }
20   }
21   if (nColVars == 0){
22     if (method =="row.compact"){
23       method <- "non.compact"
24     } else if (method == "compact"){
25       method <- "col.compact"
26     }
27   }
28   if (method == "non.compact"){
29     nCharCols <- nRowVars + 2
30     nCharRows <- nColVars + 1
31   }
32   if (method == "row.compact"){
33     nCharCols <- nRowVars + 2
34     nCharRows <- nColVars
35   }
36   if (method == "col.compact"){
37     nCharCols <- nRowVars + 1
38     nCharRows <- nColVars + 1
39   }
40   if (method == "compact"){
41     nCharCols <- nRowVars + 1
42     nCharRows <- nColVars
43   }
44
45   if(is.null(align)) {
46     align <- c(rep("l", nCharCols - 1), "l |", rep("r", xDim[2]))
47   }
48   if(is.null(display)) {
49     display <- c(rep("s", nCharCols), rep("d", xDim[2]))
50   }
51
52   attr(x, "ftableCaption") <- caption
53   attr(x, "ftableLabel") <- label
54   attr(x, "ftableAlign") <- align
55   attr(x, "ftableDigits") <- digits
56   attr(x, "quote") <- quote
57   attr(x, "ftableDisplay") <- display
58   attr(x, "method") <- method
59   attr(x, "lsep") <- lsep
60   attr(x, "nChars") <- c(nCharRows, nCharCols)
61   class(x) <- c("xtableFtable", "ftable")
62   return(x)
63 }
64
65 print.xtableFtable <- function(x,
66   type = getOption("xtable.type", "latex"),
67   file = getOption("xtable.file", ""),
68   append = getOption("xtable.append", FALSE),
69   floating = getOption("xtable.floating", TRUE),
70   floating.environment = getOption("xtable.floating.environment", "table"),
71   table.placement = getOption("xtable.table.placement", "ht"),
72   caption.placement = getOption("xtable.caption.placement", "bottom"),
73   caption.width = getOption("xtable.caption.width", NULL),
74   latex.environments = getOption("xtable.latex.environments", c("center")),
75   tabular.environment = getOption("xtable.tabular.environment", "tabular"),
76   size = getOption("xtable.size", NULL),
77   hline.after = getOption("xtable.hline.after", NULL),
78   NA.string = getOption("xtable.NA.string", ""),
79   only.contents = getOption("xtable.only.contents", FALSE),
80   add.to.row = getOption("xtable.add.to.row", NULL),
81   sanitize.text.function = getOption("xtable.sanitize.text.function",
82                                      function(x){x}),
83   sanitize.rownames.function = getOption("xtable.sanitize.rownames.function",
84                                          sanitize.text.function),
85   sanitize.colnames.function = getOption("xtable.sanitize.colnames.function",
86                                          sanitize.text.function),
87   math.style.negative = getOption("xtable.math.style.negative", FALSE),
88   math.style.exponents = getOption("xtable.math.style.exponents", FALSE),
89   html.table.attributes = getOption("xtable.html.table.attributes", "border=1"),
90   print.results = getOption("xtable.print.results", TRUE),
91   format.args = getOption("xtable.format.args", NULL),
92   rotate.rownames = getOption("xtable.rotate.rownames", FALSE),
93   rotate.colnames = getOption("xtable.rotate.colnames", FALSE),
94   booktabs = getOption("xtable.booktabs", FALSE),
95   scalebox = getOption("xtable.scalebox", NULL),
96   width = getOption("xtable.width", NULL),
97   comment = getOption("xtable.comment", TRUE),
98   timestamp = getOption("xtable.timestamp", date()),
99   ...) {
100   if (type == "latex"){
101
102     caption <- attr(x, "ftableCaption")
103     label <- attr(x, "ftableLabel")
104     align <- attr(x, "ftableAlign")
105     digits <- attr(x, "ftableDigits")
106     quote <- attr(x, "quote")
107     digits <- attr(x, "ftabelDigits")
108     method <- attr(x, "method")
109     lsep <- attr(x, "lsep")
110     nCharRows <- attr(x, "nChars")[1]
111     nCharCols <- attr(x, "nChars")[2]
112     nRowVars <- length(attr(x, "row.vars"))
113     nColVars <- length(attr(x, "col.vars"))
114     fmtFtbl <- stats:::format.ftable(x, quote = quote, digits = digits,
115                                      method = method, lsep = lsep)
116     attr(fmtFtbl, "caption") <- caption
117     attr(fmtFtbl, "label") <- label
118     ## if method is "compact", rotate both if either requested
119     ## if (method == "compact"){
120     ##   if (rotate.rownames) rotate.colnames <- TRUE
121     ##   if (rotate.colnames) rotate.rownames <- TRUE
122     ## }
123
124     ## sanitization is possible for row names and/or column names
125     ## row names
126     if (is.null(sanitize.rownames.function)) {
127       fmtFtbl[nCharRows, 1:nRowVars] <-
128         sanitize(fmtFtbl[nCharRows, 1:nRowVars], type = type)
129     } else {
130       fmtFtbl[nCharRows, 1:nRowVars] <-
131         sanitize.rownames.function(fmtFtbl[nCharRows, 1:nRowVars])
132     }
133     ## column names
134     if (is.null(sanitize.colnames.function)) {
135       fmtFtbl[1:nColVars, nCharCols - 1] <-
136         sanitize(fmtFtbl[1:nColVars, nCharCols - 1],
137                  type = type)
138     } else {
139       fmtFtbl[1:nColVars, nCharCols - 1] <-
140         sanitize.colnames.function(fmtFtbl[1:nColVars, nCharCols - 1])
141     }
142     ## rotations are possible
143     if (rotate.rownames){
144       fmtFtbl[1:dim(fmtFtbl)[1], 1:(nCharCols - 1)] <-
145         paste0("\\begin{sideways} ",
146                fmtFtbl[1:dim(fmtFtbl)[1], 1:(nCharCols - 1)],
147                "\\end{sideways}")
148     }
149     if (rotate.colnames){
150       if (rotate.rownames){
151         fmtFtbl[1:(nCharRows), (nCharCols):dim(fmtFtbl)[2]] <-
152           paste0("\\begin{sideways} ",
153                  fmtFtbl[1:(nCharRows), (nCharCols):dim(fmtFtbl)[2]],
154                  "\\end{sideways}")
155       } else {
156         fmtFtbl[1:(nCharRows), 1:dim(fmtFtbl)[2]] <-
157           paste0("\\begin{sideways} ",
158                  fmtFtbl[1:(nCharRows), 1:dim(fmtFtbl)[2]],
159                  "\\end{sideways}")
160       }
161     }
162
163
164     ## booktabs is incompatible with vertical lines in tables
165     if (booktabs) align <- gsub("|","", align, fixed = TRUE)
166     attr(fmtFtbl, "align") <- align
167     attr(fmtFtbl, "digits") <- digits
168     attr(fmtFtbl, "quote") <- quote
169     attr(fmtFtbl, "display") <- display
170
171     ## labels should be left aligned
172     for (i in 1:nCharRows){
173       fmtFtbl[i, nCharCols:dim(fmtFtbl)[2]] <-
174         paste0("\\multicolumn{1}{l}{ ",
175                fmtFtbl[i, nCharCols:dim(fmtFtbl)[2]], "}")
176     }
177
178
179     if(is.null(hline.after)) {
180       hline.after <- c(-1, nCharRows, dim(fmtFtbl)[1])
181     }
182     print.xtable(fmtFtbl, hline.after = hline.after,
183                  include.rownames = FALSE, include.colnames = FALSE,
184                  booktabs = booktabs,
185                  sanitize.text.function = function(x){x})
186   } else {
187     stop("print.xtableFtable not yet implemented for this type")
188   }
189 }
190