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