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