1toLatex.data.frame <- function(object,
2                           digits=getOption("digits"),
3                           format="f",
4                           useDcolumn=getOption("useDcolumn",TRUE),
5                           numeric.colspec=if(useDcolumn)
6                                               paste("D{.}{",LaTeXdec,"}{",ddigits,"}",sep="")
7                                           else "r",
8                           factor.colspec="l",
9                           LaTeXdec=".",
10                           ddigits=digits,
11                           useBooktabs=getOption("useBooktabs",TRUE),
12                           toprule=if(useBooktabs) "\\toprule" else "\\hline\\hline",
13                           midrule=if(useBooktabs) "\\midrule" else "\\hline",
14                           cmidrule=if(useBooktabs) "\\cmidrule" else "\\cline",
15                           bottomrule=if(useBooktabs) "\\bottomrule" else "\\hline\\hline",
16                           row.names=is.character(attr(object,"row.names")),
17                           NAas="",
18                           toLatex.escape.tex=getOption("toLatex.escape.tex",FALSE),
19                           ...){
20  n <- nrow(object)
21  m <- ncol(object)
22  d <- digits
23  is.num <- sapply(object,is.numeric)
24  is.mat <- sapply(object,is.matrix)
25  m.num <- sum(is.num)
26  digits <- integer(m.num)
27  digits[] <- d
28  fdigits <- integer(m)
29  fdigits[is.num] <- digits
30  fo <- format
31  format <- character(m)
32  format[is.num] <- fo
33
34  body <- list()
35  for(i in 1:m) {
36      object.i <- object[,i]
37      is.na.object.i <- is.na(object.i)
38      if(is.numeric(object.i))
39          body.i <- formatC(object.i,digits=fdigits[i],format=format[i])
40      else
41        body.i <- as.character(object.i)
42      body.i[is.na.object.i] <- NAas
43      body[[i]] <- format(body.i,justify="right")
44  }
45  body <- do.call(cbind,body)
46  ans <- gsub("([eE])([-+]?[0-9]+)","\\\\textrm{\\1}\\2",body)
47
48  if(row.names){
49    ans <- cbind(format(rownames(object),justify="right"),ans)
50  }
51
52  colspan <- sapply(object,NCOL)
53  header <- paste0("\\multicolumn{",colspan,"}{c}{",colnames(object),"}")
54
55  header <- paste(header,collapse=" & ")
56  if(row.names) header <- paste("&",header)
57  header <- paste(header,"\\\\")
58
59  ans <- apply(ans,1,paste,collapse=" & ")
60  ans <- paste(ans,"\\\\")
61  ans <- c(
62      toprule,
63      header,
64      midrule,
65      ans,
66      bottomrule
67  )
68
69  if(toLatex.escape.tex)
70      ans <- LaTeXcape(ans)
71
72  body.spec <- rep(factor.colspec,m)
73  dd <- integer(m.num)
74  dd[] <- ddigits
75  ddigits <- dd
76  body.spec[is.num] <- numeric.colspec
77  body.spec <- rep(body.spec,colspan)
78
79  if(row.names)
80    tabspec <- c("l",body.spec)
81  else
82    tabspec <- body.spec
83  tabspec <- paste(tabspec,collapse="")
84  tabbegin <- paste("\\begin{tabular}{",tabspec,"}",sep="")
85  tabend <- "\\end{tabular}"
86  ans <- c(tabbegin,ans,tabend)
87  structure(ans,class="Latex")
88}
89
90toLatex.data.set <- function(object,...){
91  frame <- structure(object@.Data,row.names=object@row_names,names=object@names,class="data.frame")
92  for(i in 1:ncol(frame))
93    frame[[i]] <- format(frame[[i]])
94  toLatex(frame,...)
95}
96