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