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