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