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