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