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