]> git.donarmstrong.com Git - xtable.git/blob - pkg/R/print.xtable.R
c0cce93ef7f9a2a37050769bf1b71e3a141a4fc2
[xtable.git] / pkg / R / print.xtable.R
1 ### xtable package\r
2 ###\r
3 ### Produce LaTeX and HTML tables from R objects.\r
4 ###\r
5 ### Copyright 2000-2012 David B. Dahl <dahl@stat.tamu.edu>\r
6 ###\r
7 ### Maintained by Charles Roosen <croosen@mango-solutions.com>\r
8 ###\r
9 ### This file is part of the `xtable' library for R and related languages.\r
10 ### It is made available under the terms of the GNU General Public\r
11 ### License, version 2, or at your option, any later version,\r
12 ### incorporated herein by reference.\r
13 ###\r
14 ### This program is distributed in the hope that it will be\r
15 ### useful, but WITHOUT ANY WARRANTY; without even the implied\r
16 ### warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR\r
17 ### PURPOSE.  See the GNU General Public License for more\r
18 ### details.\r
19 ###\r
20 ### You should have received a copy of the GNU General Public\r
21 ### License along with this program; if not, write to the Free\r
22 ### Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,\r
23 ### MA 02111-1307, USA\r
24 print.xtable <- function(x,\r
25   type = getOption("xtable.type", "latex"),\r
26   file = getOption("xtable.file", ""),\r
27   append = getOption("xtable.append", FALSE),\r
28   floating = getOption("xtable.floating", TRUE),\r
29   floating.environment = getOption("xtable.floating.environment", "table"),\r
30   table.placement = getOption("xtable.table.placement", "ht"),\r
31   caption.placement = getOption("xtable.caption.placement", "bottom"),\r
32   latex.environments = getOption("xtable.latex.environments", c("center")),\r
33   tabular.environment = getOption("xtable.tabular.environment", "tabular"),\r
34   size = getOption("xtable.size", NULL),\r
35   hline.after = getOption("xtable.hline.after", c(-1,0,nrow(x))),\r
36   NA.string = getOption("xtable.NA.string", ""),\r
37   include.rownames = getOption("xtable.include.rownames", TRUE),\r
38   include.colnames = getOption("xtable.include.colnames", TRUE),\r
39   only.contents = getOption("xtable.only.contents", FALSE),\r
40   add.to.row = getOption("xtable.add.to.row", NULL),\r
41   sanitize.text.function = getOption("xtable.sanitize.text.function", NULL),\r
42   sanitize.rownames.function = getOption("xtable.sanitize.rownames.function",\r
43                                          sanitize.text.function),\r
44   sanitize.colnames.function = getOption("xtable.sanitize.colnames.function",\r
45                                          sanitize.text.function),\r
46   math.style.negative = getOption("xtable.math.style.negative", FALSE),\r
47   html.table.attributes = getOption("xtable.html.table.attributes", "border=1"),\r
48   print.results = getOption("xtable.print.results", TRUE),\r
49   format.args = getOption("xtable.format.args", NULL),\r
50   rotate.rownames = getOption("xtable.rotate.rownames", FALSE),\r
51   rotate.colnames = getOption("xtable.rotate.colnames", FALSE),\r
52   booktabs = getOption("xtable.booktabs", FALSE),\r
53   scalebox = getOption("xtable.scalebox", NULL),\r
54   width = getOption("xtable.width", NULL),\r
55   timestamp = date(),\r
56   ...)\r
57 {\r
58     ## If caption is length 2, treat the second value as the "short caption"\r
59     caption <- attr(x,"caption",exact = TRUE)\r
60     short.caption <- NULL\r
61     if (!is.null(caption) && length(caption) > 1){\r
62         short.caption <- caption[2]\r
63         caption <- caption[1]\r
64     }\r
65 \r
66     ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 hline.after\r
67     ## By default it print an \hline before and after the columns names\r
68     ## independently they are printed or not and at the end of the table\r
69     ## Old code that set hline.after should include c(-1, 0, nrow(x)) in the\r
70     ## hline.after vector\r
71     ## If you do not want any \hline inside the data, set hline.after to NULL\r
72     ## PHEADER instead the string '\\hline\n' is used in the code\r
73     ## Now hline.after counts how many time a position appear\r
74     ## I left an automatic PHEADER in the longtable is this correct?\r
75 \r
76     ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 include.rownames,\r
77     ## include.colnames\r
78     pos <- 0\r
79     if (include.rownames) pos <- 1\r
80 \r
81     ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
82     ## hline.after checks\r
83     if (any(hline.after < -1) | any(hline.after > nrow(x))) {\r
84         stop("'hline.after' must be inside [-1, nrow(x)]")\r
85     }\r
86 \r
87     ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
88     ## add.to.row checks\r
89     if (!is.null(add.to.row)) {\r
90         if (is.list(add.to.row) && length(add.to.row) == 2) {\r
91             if (is.null(names(add.to.row))) {\r
92                 names(add.to.row) <- c('pos', 'command')\r
93             } else if (any(sort(names(add.to.row))!= c('command', 'pos'))) {\r
94                 stop("the names of the elements of 'add.to.row' must be 'pos' and 'command'")\r
95             }\r
96             if (is.list(add.to.row$pos) && is.vector(add.to.row$command,\r
97                                                      mode = 'character')) {\r
98                 if ((npos <- length(add.to.row$pos)) !=\r
99                     length(add.to.row$command)) {\r
100                     stop("the length of 'add.to.row$pos' must be equal to the length of 'add.to.row$command'")\r
101                 }\r
102                 if (any(unlist(add.to.row$pos) < -1) |\r
103                     any(unlist(add.to.row$pos) > nrow(x))) {\r
104                     stop("the values in add.to.row$pos must be inside the interval [-1, nrow(x)]")\r
105                 }\r
106             } else {\r
107                 stop("the first argument ('pos') of 'add.to.row' must be a list, the second argument ('command') must be a vector of mode character")\r
108             }\r
109         } else {\r
110             stop("'add.to.row' argument must be a list of length 2")\r
111         }\r
112     } else {\r
113         add.to.row <- list(pos = list(),\r
114                            command = vector(length = 0, mode = "character"))\r
115         npos <- 0\r
116     }\r
117 \r
118     ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 add.to.row\r
119     ## Add further commands at the end of rows\r
120     if (type == "latex") {\r
121         ## Original code before changes in version 1.6-1\r
122         ## PHEADER <- "\\hline\n"\r
123 \r
124         ## booktabs code from Matthieu Stigler <matthieu.stigler@gmail.com>,\r
125         ## 1 Feb 2012\r
126         if(!booktabs){\r
127             PHEADER <- "\\hline\n"\r
128         } else {\r
129             PHEADER <- ifelse(-1%in%hline.after, "\\toprule\n", "")\r
130             if(0%in%hline.after) {\r
131                 PHEADER <- c(PHEADER, "\\midrule\n")\r
132             }\r
133             if(nrow(x)%in%hline.after) {\r
134                 PHEADER <- c(PHEADER, "\\bottomrule\n")\r
135             }\r
136         }\r
137     } else {\r
138         PHEADER <- ""\r
139     }\r
140 \r
141     lastcol <- rep(" ", nrow(x)+2)\r
142     if (!is.null(hline.after)) {\r
143         ## booktabs change - Matthieu Stigler: fill the hline arguments\r
144         ## separately, 1 Feb 2012\r
145         ##\r
146         ## Code before booktabs change was:\r
147         ##    add.to.row$pos[[npos+1]] <- hline.after\r
148 \r
149         if (!booktabs){\r
150             add.to.row$pos[[npos+1]] <- hline.after\r
151         } else {\r
152             for(i in 1:length(hline.after)) {\r
153                 add.to.row$pos[[npos+i]] <- hline.after[i]\r
154             }\r
155         }\r
156         add.to.row$command <- c(add.to.row$command, PHEADER)\r
157     }\r
158 \r
159     if ( length(add.to.row$command) > 0 ) {\r
160         for (i in 1:length(add.to.row$command)) {\r
161             addpos <- add.to.row$pos[[i]]\r
162             freq <- table(addpos)\r
163             addpos <- unique(addpos)\r
164             for (j in 1:length(addpos)) {\r
165                 lastcol[addpos[j]+2] <- paste(lastcol[addpos[j]+2],\r
166                                               paste(rep(add.to.row$command[i],\r
167                                                         freq[j]),\r
168                                                     sep = "", collapse = ""),\r
169                                               sep = " ")\r
170             }\r
171         }\r
172     }\r
173 \r
174     if (length(type)>1) stop("\"type\" must have length 1")\r
175     type <- tolower(type)\r
176     if (!all(!is.na(match(type, c("latex","html"))))) {\r
177         stop("\"type\" must be in {\"latex\", \"html\"}")\r
178     }\r
179     if (!all(!is.na(match(floating.environment,\r
180                           c("table","table*","sidewaystable",\r
181                             "margintable"))))) {\r
182         stop("\"type\" must be in {\"table\", \"table*\", \"sidewaystable\", \"margintable\"}")\r
183     }\r
184     if (("margintable" %in% floating.environment)\r
185         & (!is.null(table.placement))) {\r
186         warning("margintable does not allow for table placement; setting table.placement to NULL")\r
187         table.placement <- NULL\r
188     }\r
189     if (!is.null(table.placement) &&\r
190         !all(!is.na(match(unlist(strsplit(table.placement,  split = "")),\r
191                           c("H","h","t","b","p","!"))))) {\r
192         stop("\"table.placement\" must contain only elements of {\"h\",\"t\",\"b\",\"p\",\"!\"}")\r
193     }\r
194     if (!all(!is.na(match(caption.placement, c("bottom","top"))))) {\r
195         stop("\"caption.placement\" must be either {\"bottom\",\"top\"}")\r
196     }\r
197 \r
198     if (type == "latex") {\r
199         BCOMMENT <- "% "\r
200         ECOMMENT <- "\n"\r
201         ## See e-mail from "John S. Walker <jsw9c@uic.edu>" dated 5-19-2003\r
202         ## regarding "texfloat"\r
203         ## See e-mail form "Fernando Henrique Ferraz P. da Rosa"\r
204         ## <academic@feferraz.net>" dated 10-28-2005 regarding "longtable"\r
205         if ( tabular.environment == "longtable" & floating == TRUE ) {\r
206             warning("Attempt to use \"longtable\" with floating = TRUE. Changing to FALSE.")\r
207             floating <- FALSE\r
208         }\r
209         if ( floating == TRUE ) {\r
210             ## See e-mail from "Pfaff, Bernhard <Bernhard.Pfaff@drkw.com>"\r
211             ## dated 7-09-2003 regarding "suggestion for an amendment of\r
212             ## the source"\r
213             ## See e-mail from "Mitchell, David"\r
214             ## <David.Mitchell@dotars.gov.au>" dated 2003-07-09 regarding\r
215             ## "Additions to R xtable package"\r
216             ## See e-mail from "Garbade, Sven"\r
217             ## <Sven.Garbade@med.uni-heidelberg.de> dated 2006-05-22\r
218             ## regarding the floating environment.\r
219             BTABLE <- paste("\\begin{", floating.environment, "}",\r
220                             ifelse(!is.null(table.placement),\r
221                                    paste("[", table.placement, "]", sep = ""),\r
222                                    ""), "\n", sep = "")\r
223             if ( is.null(latex.environments) ||\r
224                 (length(latex.environments) == 0) ) {\r
225                 BENVIRONMENT <- ""\r
226                 EENVIRONMENT <- ""\r
227             } else {\r
228                 BENVIRONMENT <- ""\r
229                 EENVIRONMENT <- ""\r
230                 if ("center" %in% latex.environments){\r
231                     BENVIRONMENT <- paste(BENVIRONMENT, "\\centering\n",\r
232                                           sep = "")\r
233                 }\r
234                 for (i in 1:length(latex.environments)) {\r
235                     if (latex.environments[i] == "") next\r
236                     if (latex.environments[i] != "center"){\r
237                         BENVIRONMENT <- paste(BENVIRONMENT,\r
238                                               "\\begin{", latex.environments[i],\r
239                                               "}\n", sep = "")\r
240                         EENVIRONMENT <- paste("\\end{", latex.environments[i],\r
241                                               "}\n", EENVIRONMENT, sep = "")\r
242                     }\r
243                 }\r
244             }\r
245             ETABLE <- paste("\\end{", floating.environment, "}\n", sep = "")\r
246         } else {\r
247             BTABLE <- ""\r
248             ETABLE <- ""\r
249             BENVIRONMENT <- ""\r
250             EENVIRONMENT <- ""\r
251         }\r
252 \r
253         tmp.index.start <- 1\r
254         if ( ! include.rownames ) {\r
255             while ( attr(x, "align", exact = TRUE)[tmp.index.start] == '|' )\r
256                 tmp.index.start <- tmp.index.start + 1\r
257             tmp.index.start <- tmp.index.start + 1\r
258         }\r
259         ## Added "width" argument for use with "tabular*" or\r
260         ## "tabularx" environments - CR, 7/2/12\r
261         if (is.null(width)){\r
262             WIDTH <-""\r
263         } else if (is.element(tabular.environment,\r
264                               c("tabular", "longtable"))){\r
265             warning("Ignoring 'width' argument.  The 'tabular' and 'longtable' environments do not support a width specification.  Use another environment such as 'tabular*' or 'tabularx' to specify the width.")\r
266             WIDTH <- ""\r
267         } else {\r
268             WIDTH <- paste("{", width, "}", sep = "")\r
269         }\r
270 \r
271         BTABULAR <-\r
272             paste("\\begin{", tabular.environment, "}",\r
273                   WIDTH, "{",\r
274                   paste(c(attr(x, "align",\r
275                                exact = TRUE)[\r
276                                tmp.index.start:length(attr(x, "align",\r
277                                                            exact = TRUE))],\r
278                           "}\n"),\r
279                         sep = "", collapse = ""),\r
280                   sep = "")\r
281 \r
282         ## fix 10-26-09 (robert.castelo@upf.edu) the following\r
283         ## 'if' condition is added here to support\r
284         ## a caption on the top of a longtable\r
285         if (tabular.environment == "longtable" && caption.placement == "top") {\r
286             if (is.null(short.caption)){\r
287                 BCAPTION <- "\\caption{"\r
288             } else {\r
289                 BCAPTION <- paste("\\caption[", short.caption, "]{", sep = "")\r
290             }\r
291             ECAPTION <- "} \\\\ \n"\r
292             if ((!is.null(caption)) && (type == "latex")) {\r
293                 BTABULAR <- paste(BTABULAR,  BCAPTION, caption, ECAPTION,\r
294                                   sep = "")\r
295             }\r
296         }\r
297         ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
298         ## add.to.row position -1\r
299         BTABULAR <- paste(BTABULAR, lastcol[1], sep = "")\r
300         ## the \hline at the end, if present, is set in full matrix\r
301         ETABULAR <- paste("\\end{", tabular.environment, "}\n", sep = "")\r
302 \r
303         ## Add scalebox - CR, 7/2/12\r
304         if (!is.null(scalebox)){\r
305             BTABULAR <- paste("\\scalebox{", scalebox, "}{\n", BTABULAR,\r
306                               sep = "")\r
307             ETABULAR <- paste(ETABULAR, "}\n", sep = "")\r
308         }\r
309 \r
310         ## BSIZE contributed by Benno <puetz@mpipsykl.mpg.de> in e-mail\r
311         ## dated Wednesday, December 01, 2004\r
312         if (is.null(size) || !is.character(size)) {\r
313             BSIZE <- ""\r
314             ESIZE <- ""\r
315         } else {\r
316             if(length(grep("^\\\\", size)) == 0){\r
317                 size <- paste("\\", size, sep = "")\r
318             }\r
319             BSIZE <- paste("{", size, "\n", sep = "")\r
320             ESIZE <- "}\n"\r
321         }\r
322         BLABEL <- "\\label{"\r
323         ELABEL <- "}\n"\r
324         if (is.null(short.caption)){\r
325         BCAPTION <- "\\caption{"\r
326     } else {\r
327         BCAPTION <- paste("\\caption[", short.caption, "]{", sep = "")\r
328     }\r
329         ECAPTION <- "}\n"\r
330         BROW <- ""\r
331         EROW <- " \\\\ \n"\r
332         BTH <- ""\r
333         ETH <- ""\r
334         STH <- " & "\r
335         BTD1 <- " & "\r
336         BTD2 <- ""\r
337         BTD3 <- ""\r
338         ETD  <- ""\r
339         ## Based on contribution from Jonathan Swinton\r
340         ## <jonathan@swintons.net> in e-mail dated Wednesday, January 17, 2007\r
341         sanitize <- function(str) {\r
342             result <- str\r
343             result <- gsub("\\\\", "SANITIZE.BACKSLASH", result)\r
344             result <- gsub("$", "\\$", result, fixed = TRUE)\r
345             result <- gsub(">", "$>$", result, fixed = TRUE)\r
346             result <- gsub("<", "$<$", result, fixed = TRUE)\r
347             result <- gsub("|", "$|$", result, fixed = TRUE)\r
348             result <- gsub("{", "\\{", result, fixed = TRUE)\r
349             result <- gsub("}", "\\}", result, fixed = TRUE)\r
350             result <- gsub("%", "\\%", result, fixed = TRUE)\r
351             result <- gsub("&", "\\&", result, fixed = TRUE)\r
352             result <- gsub("_", "\\_", result, fixed = TRUE)\r
353             result <- gsub("#", "\\#", result, fixed = TRUE)\r
354             result <- gsub("^", "\\verb|^|", result, fixed = TRUE)\r
355             result <- gsub("~", "\\~{}", result, fixed = TRUE)\r
356             result <- gsub("SANITIZE.BACKSLASH", "$\\backslash$",\r
357                            result, fixed = TRUE)\r
358             return(result)\r
359         }\r
360         sanitize.numbers <- function(x) {\r
361             result <- x\r
362             if ( math.style.negative ) {\r
363                 ## Jake Bowers <jwbowers@illinois.edu> in e-mail\r
364                 ## from 2008-08-20 suggested disabling this feature to avoid\r
365                 ## problems with LaTeX's dcolumn package.\r
366                 ## by Florian Wickelmaier <florian.wickelmaier@uni-tuebingen.de>\r
367                 ## in e-mail from 2008-10-03 requested the ability to use the\r
368                 ## old behavior.\r
369                 for(i in 1:length(x)) {\r
370                     result[i] <- gsub("-", "$-$", result[i], fixed = TRUE)\r
371                 }\r
372             }\r
373             return(result)\r
374         }\r
375         sanitize.final <- function(result) {\r
376             return(result)\r
377         }\r
378     } else {\r
379         BCOMMENT <- "<!-- "\r
380         ECOMMENT <- " -->\n"\r
381         BTABLE <- paste("<TABLE ", html.table.attributes, ">\n", sep = "")\r
382         ETABLE <- "</TABLE>\n"\r
383         BENVIRONMENT <- ""\r
384         EENVIRONMENT <- ""\r
385         BTABULAR <- ""\r
386         ETABULAR <- ""\r
387         BSIZE <- ""\r
388         ESIZE <- ""\r
389         BLABEL <- "<A NAME="\r
390         ELABEL <- "></A>\n"\r
391         BCAPTION <- paste("<CAPTION ALIGN=\"", caption.placement, "\"> ",\r
392                           sep = "")\r
393         ECAPTION <- " </CAPTION>\n"\r
394         BROW <- "<TR>"\r
395         EROW <- " </TR>\n"\r
396         BTH <- " <TH> "\r
397         ETH <- " </TH> "\r
398         STH <- " </TH> <TH> "\r
399         BTD1 <- " <TD align=\""\r
400         align.tmp <- attr(x, "align", exact = TRUE)\r
401         align.tmp <- align.tmp[align.tmp!="|"]\r
402         BTD2 <- matrix(align.tmp[(2-pos):(ncol(x)+1)],\r
403                        nrow = nrow(x), ncol = ncol(x)+pos, byrow = TRUE)\r
404         ## Based on contribution from Jonathan Swinton <jonathan@swintons.net>\r
405         ## in e-mail dated Wednesday, January 17, 2007\r
406         BTD2[regexpr("^p", BTD2)>0] <- "left"\r
407         BTD2[BTD2 == "r"] <- "right"\r
408         BTD2[BTD2 == "l"] <- "left"\r
409         BTD2[BTD2 == "c"] <- "center"\r
410         BTD3 <- "\"> "\r
411         ETD  <- " </TD>"\r
412         sanitize <- function(str) {\r
413             result <- str\r
414             result <- gsub("&", "&amp ", result, fixed = TRUE)\r
415             result <- gsub(">", "&gt ", result, fixed = TRUE)\r
416             result <- gsub("<", "&lt ", result, fixed = TRUE)\r
417             ## Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> on 2006/10/05\r
418             ## recommended not escaping underscores.\r
419             ## result <- gsub("_", "\\_", result, fixed=TRUE)\r
420             return(result)\r
421         }\r
422         sanitize.numbers <- function(x) {\r
423             return(x)\r
424         }\r
425         sanitize.final <- function(result) {\r
426             ## Suggested by Uwe Ligges <ligges@statistik.uni-dortmund.de>\r
427             ## in e-mail dated 2005-07-30.\r
428             result$text <- gsub("  *", " ",  result$text, fixed = TRUE)\r
429             result$text <- gsub(' align="left"',  "", result$text,\r
430                                 fixed = TRUE)\r
431             return(result)\r
432         }\r
433     }\r
434 \r
435     result <- string("", file = file, append = append)\r
436     info <- R.Version()\r
437     ## modified Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
438     ## to set automatically the package version\r
439     result <- result + BCOMMENT + type + " table generated in " +\r
440               info$language + " " + info$major + "." + info$minor +\r
441               " by xtable " +  packageDescription('xtable')$Version +\r
442               " package" + ECOMMENT\r
443     if (!is.null(timestamp)){             \r
444         result <- result + BCOMMENT + timestamp + ECOMMENT\r
445     }           \r
446     ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 only.contents\r
447     if (!only.contents) {\r
448         result <- result + BTABLE\r
449         result <- result + BENVIRONMENT\r
450         if ( floating == TRUE ) {\r
451             if ((!is.null(caption)) &&\r
452                 (type == "html" ||caption.placement == "top")) {\r
453                 result <- result + BCAPTION + caption + ECAPTION\r
454             }\r
455             if (!is.null(attr(x, "label", exact = TRUE)) &&\r
456                 (type == "latex" && caption.placement == "top")) {\r
457                 result <- result + BLABEL +\r
458                           attr(x, "label", exact = TRUE) + ELABEL\r
459             }\r
460         }\r
461         result <- result + BSIZE\r
462         result <- result + BTABULAR\r
463     }\r
464     ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
465     ## include.colnames, include.rownames\r
466     if (include.colnames) {\r
467         result <- result + BROW + BTH\r
468         if (include.rownames) {\r
469             result <- result + STH\r
470         }\r
471         ## David G. Whiting in e-mail 2007-10-09\r
472         if (is.null(sanitize.colnames.function)) {\r
473             CNAMES <- sanitize(names(x))\r
474         } else {\r
475             CNAMES <- sanitize.colnames.function(names(x))\r
476         }\r
477         if (rotate.colnames) {\r
478             ##added by Markus Loecher, 2009-11-16\r
479             CNAMES <- paste("\\begin{sideways}", CNAMES, "\\end{sideways}")\r
480         }\r
481         result <- result + paste(CNAMES, collapse = STH)\r
482 \r
483         result <- result + ETH + EROW\r
484     }\r
485 \r
486     cols <- matrix("", nrow = nrow(x), ncol = ncol(x)+pos)\r
487     if (include.rownames) {\r
488         ## David G. Whiting in e-mail 2007-10-09\r
489         if (is.null(sanitize.rownames.function)) {\r
490             RNAMES <- sanitize(row.names(x))\r
491         } else {\r
492             RNAMES <- sanitize.rownames.function(row.names(x))\r
493         }\r
494         if (rotate.rownames) {\r
495             ##added by Markus Loecher, 2009-11-16\r
496             RNAMES <- paste("\\begin{sideways}", RNAMES, "\\end{sideways}")\r
497         }\r
498         cols[, 1] <- RNAMES\r
499     }\r
500 \r
501 ## Begin vectorizing the formatting code by Ian Fellows [ian@fellstat.com]\r
502 ## 06 Dec 2011\r
503 ##\r
504 ##  disp <- function(y) {\r
505 ##    if (is.factor(y)) {\r
506 ##      y <- levels(y)[y]\r
507 ##    }\r
508 ##    if (is.list(y)) {\r
509 ##      y <- unlist(y)\r
510 ##    }\r
511 ##    return(y)\r
512 ##  }\r
513     varying.digits <- is.matrix( attr( x, "digits", exact = TRUE ) )\r
514     ## Code for letting "digits" be a matrix was provided by\r
515     ## Arne Henningsen <ahenningsen@agric-econ.uni-kiel.de>\r
516     ## in e-mail dated 2005-06-04.\r
517     ##if( !varying.digits ) {\r
518     ## modified Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
519     ##  attr(x,"digits") <- matrix( attr( x, "digits",exact=TRUE ),\r
520     ## nrow = nrow(x), ncol = ncol(x)+1, byrow = TRUE )\r
521     ##}\r
522     for(i in 1:ncol(x)) {\r
523         xcol <- x[, i]\r
524         if(is.factor(xcol))\r
525             xcol <- as.character(xcol)\r
526         if(is.list(xcol))\r
527             xcol <- sapply(xcol, unlist)\r
528         ina <- is.na(xcol)\r
529         is.numeric.column <- is.numeric(xcol)\r
530 \r
531         if(is.character(xcol)) {\r
532             cols[, i+pos] <- xcol\r
533         } else {\r
534             if (is.null(format.args)){\r
535                 format.args <- list()\r
536             }\r
537             if (is.null(format.args$decimal.mark)){\r
538                 format.args$decimal.mark <- options()$OutDec\r
539             }\r
540             if(!varying.digits){\r
541                 curFormatArgs <- c(list(\r
542                                    x = xcol,\r
543                                    format = ifelse( attr( x, "digits",\r
544                                    exact = TRUE )[i+1] < 0, "E",\r
545                                    attr( x, "display", exact = TRUE )[i+1] ),\r
546                                    digits = abs( attr( x, "digits",\r
547                                    exact = TRUE )[i+1] )),\r
548                                    format.args)\r
549                 cols[, i+pos] <- do.call("formatC", curFormatArgs)\r
550             }else{\r
551                 for( j in 1:nrow( cols ) ) {\r
552                     curFormatArgs <- c(list(\r
553                                        x = xcol[j],\r
554                                        format = ifelse( attr( x, "digits",\r
555                                        exact = TRUE )[j, i+1] < 0, "E",\r
556                                        attr( x, "display",\r
557                                             exact = TRUE )[i+1] ),\r
558                                        digits = abs( attr( x, "digits",\r
559                                        exact = TRUE )[j, i+1] )),\r
560                                        format.args)\r
561                     cols[j, i+pos] <- do.call("formatC", curFormatArgs)\r
562                 }\r
563             }\r
564         }\r
565         ## End Ian Fellows changes\r
566 \r
567         if ( any(ina) ) cols[ina, i+pos] <- NA.string\r
568         ## Based on contribution from Jonathan Swinton <jonathan@swintons.net>\r
569         ## in e-mail dated Wednesday, January 17, 2007\r
570         if ( is.numeric.column ) {\r
571             cols[, i+pos] <- sanitize.numbers(cols[, i+pos])\r
572         } else {\r
573             if (is.null(sanitize.text.function)) {\r
574                 cols[, i+pos] <- sanitize(cols[, i+pos])\r
575             } else {\r
576                 cols[, i+pos] <- sanitize.text.function(cols[, i+pos])\r
577             }\r
578         }\r
579     }\r
580 \r
581     multiplier <- 5\r
582     full <- matrix("", nrow = nrow(x), ncol = multiplier*(ncol(x)+pos)+2)\r
583     full[, 1] <- BROW\r
584     full[, multiplier*(0:(ncol(x)+pos-1))+2] <- BTD1\r
585     full[, multiplier*(0:(ncol(x)+pos-1))+3] <- BTD2\r
586     full[, multiplier*(0:(ncol(x)+pos-1))+4] <- BTD3\r
587     full[, multiplier*(0:(ncol(x)+pos-1))+5] <- cols\r
588     full[, multiplier*(0:(ncol(x)+pos-1))+6] <- ETD\r
589 \r
590     full[, multiplier*(ncol(x)+pos)+2] <- paste(EROW, lastcol[-(1:2)],\r
591                                                 sep = " ")\r
592 \r
593     if (type == "latex") full[, 2] <- ""\r
594     result <- result + lastcol[2] + paste(t(full), collapse = "")\r
595     if (!only.contents) {\r
596         if (tabular.environment == "longtable") {\r
597             ## booktabs change added the if() - 1 Feb 2012\r
598             if(!booktabs) {\r
599                 result <- result + PHEADER\r
600             }\r
601 \r
602             ## fix 10-27-09 Liviu Andronic (landronimirc@gmail.com) the\r
603             ## following 'if' condition is inserted in order to avoid\r
604             ## that bottom caption interferes with a top caption of a longtable\r
605             if(caption.placement == "bottom"){\r
606                 if ((!is.null(caption)) && (type == "latex")) {\r
607                     result <- result + BCAPTION + caption + ECAPTION\r
608                 }\r
609             }\r
610             if (!is.null(attr(x, "label", exact = TRUE))) {\r
611                 result <- result + BLABEL + attr(x, "label", exact = TRUE) +\r
612                     ELABEL\r
613             }\r
614             ETABULAR <- "\\end{longtable}\n"\r
615         }\r
616         result <- result + ETABULAR\r
617         result <- result + ESIZE\r
618         if ( floating == TRUE ) {\r
619             if ((!is.null(caption)) &&\r
620                 (type == "latex" && caption.placement == "bottom")) {\r
621                 result <- result + BCAPTION + caption + ECAPTION\r
622             }\r
623             if (!is.null(attr(x, "label", exact = TRUE)) &&\r
624                 caption.placement == "bottom") {\r
625                 result <- result + BLABEL + attr(x, "label", exact = TRUE) +\r
626                     ELABEL\r
627             }\r
628         }\r
629         result <- result + EENVIRONMENT\r
630         result <- result + ETABLE\r
631     }\r
632     result <- sanitize.final(result)\r
633 \r
634     if (print.results){\r
635         print(result)\r
636     }\r
637 \r
638     return(invisible(result$text))\r
639 }\r
640 \r
641 "+.string" <- function(x, y) {\r
642     x$text <- paste(x$text, as.string(y)$text, sep = "")\r
643     return(x)\r
644 }\r
645 \r
646 print.string <- function(x, ...) {\r
647     cat(x$text, file = x$file, append = x$append)\r
648     return(invisible())\r
649 }\r
650 \r
651 string <- function(text, file = "", append = FALSE) {\r
652     x <- list(text = text, file = file, append = append)\r
653     class(x) <- "string"\r
654     return(x)\r
655 }\r
656 \r
657 as.string <- function(x, file = "", append = FALSE) {\r
658     if (is.null(attr(x, "class", exact = TRUE)))\r
659         switch(data.class(x),\r
660                character = return(string(x, file, append)),\r
661                numeric = return(string(as.character(x), file, append)),\r
662                stop("Cannot coerse argument to a string"))\r
663     if (class(x) == "string")\r
664         return(x)\r
665     stop("Cannot coerse argument to a string")\r
666 }\r
667 \r
668 is.string <- function(x) {\r
669     return(class(x) == "string")\r
670 }\r
671 \r