日々のつれづれ

不惑をむかえ戸惑いを隠せない男性の独り言

Sweave関数について少し...・その3

その続き…
最近、Sweave関数もLaTexも使って無いので、動かないかもしれませんが…

で、次はxtableを複数のページにまたがって出力したくなって、supertabular環境とlongtable環境を持ってこようとしました。

  • supertablular環境
> library(xtable)
> dat <- data.frame(matrix(rnorm(12),ncol=3,dimnames=list(1:4,LETTERS[1:3])))
> 
> xtable.super <- function(dat,...){
+ library(xtable)
+ rownames(dat) <- gsub("_", "\\\\_", rownames(dat))
+ if(is.matrix(dat)) dat <- as.data.frame(dat)
+ tex <- xtable.col(dat,out=FALSE,...)
+ write(tex,"xtable.tmp")
+ tex <- readLines("xtable.tmp")
+ file.remove("xtable.tmp")
+ 
+ tex <- sub("tabular","supertabular",tex)
+ del <- grep(paste(colnames(dat),collapse=" & ",sep=""), gsub("\\\\","",tex))
+ tex1 <- tex[del]
+ tex <- tex[-del+0:1]
+ # header指定
+ cat("\\tablefirsthead{\\hline", tex1, "\ \ \\hline}\n")
+ # tail指定
+ cat("\\tabletail{\\hline}\n")
+ # 次ページの表の最初につく行の指定
+ cat("\\tablehead{\\multicolumn{", ncol(dat)+1, "}{l}{\\small\\sl continued on next page} \\\\ \\hline\n", tex1, "\ \ \\hline}\n")
+ cat(sub("caption", "topcaption", tex[grep("caption\\{(.*)\\}", tex)]), "\n")
+   
+ if(sum(grep("caption\\{(.*)\\}", tex))>0) tex <- tex[- grep("caption\\{(.*)\\}", tex)]
+ tex <- tex[nchar(tex)>0]
+ tex <- paste(tex, "\n", collapse="")
+ cat(tex)
+ }
> 
> xtable.super(dat)
% delete line start
% latex table generated in R 2.14.0 by xtable 1.6-0 package
% Sat Dec 17 19:38:26 2011
\begin{tabular}{rrrr}
\hline
& A & B & C \\ 
\hline
1 & -0.36 & -2.06 & -1.44 \\ 
2 & 1.01 & -0.01 & -0.90 \\ 
3 & 1.48 & 0.19 & 1.43 \\ 
4 & 0.58 & -0.58 & 2.27 \\ 
\hline
\end{tabular}
% delete line end
\tablefirsthead{\hline & A & B & C \\   \hline}
\tabletail{\hline}
\tablehead{\multicolumn{ 4 }{l}{\small\sl continued on next page} \\ \hline
& A & B & C \\   \hline}
\begin{supertabular}{rrrr} 
\hline 
1 & -0.36 & -2.06 & -1.44 \\ 
2 & 1.01 & -0.01 & -0.90 \\ 
3 & 1.48 & 0.19 & 1.43 \\ 
4 & 0.58 & -0.58 & 2.27 \\ 
\hline 
\end{supertabular} 
||
で、出力したsnwファイルから、<span class="deco" style="font-weight:bold;">% delete line start ~ % delete line end</span>、までをrubyで整形してplatex -> dvipdfmxしてました。

- longtable環境
>|r|
> xtable.long <- function(dat,...){
+ library(xtable)
+ rownames(dat) <- gsub("_", "\\\\_", rownames(dat))
+ if(is.matrix(dat)) dat <- as.data.frame(dat)
+ tex <- xtable.col(dat,out=FALSE,...)
+ write(tex,"xtable.tmp")
+ tex <- readLines("xtable.tmp")
+ file.remove("xtable.tmp")
+   
+ tex <- sub("\\{tabular\\}","{longtable}",tex)
+ del <- grep(paste(colnames(dat),collapse=" & "), gsub("\\\\","",tex))
+ # header,footer指定
+ y <- c("\\endfirsthead"
+ ,paste("\\multicolumn{",ncol(dat)+1,"}{l}{\\small\\sl continued from previous page} \\\\",sep="")
+ ,"\\hline"
+ ,tex[del]
+ ,"\\hline"
+ ,"\\endhead"
+ ,"\\hline"
+ ,paste("\\multicolumn{",ncol(dat)+1,"}{r}{\\small\\sl table continued on next page} \\\\",sep="")
+ ,"\\endfoot"
+ ,"\\endlastfoot"
+ )
+ tex <- c(tex[seq(del-1)],y,tex[seq(del+2,length(tex))])
+ tex <- tex[nchar(tex)>0]
+ tex <- paste(tex, "\n", collapse="")
+ cat(tex)
+ }
> 
> xtable.long(dat)
% delete line start
% latex table generated in R 2.14.0 by xtable 1.6-0 package
% Sat Dec 17 19:39:40 2011
\begin{tabular}{rrrr}
\hline
& A & B & C \\ 
\hline
1 & -0.36 & -2.06 & -1.44 \\ 
2 & 1.01 & -0.01 & -0.90 \\ 
3 & 1.48 & 0.19 & 1.43 \\ 
4 & 0.58 & -0.58 & 2.27 \\ 
\hline
\end{tabular}
% delete line end
\begin{longtable}{rrrr} 
\hline 
\endfirsthead 
\multicolumn{4}{l}{\small\sl continued from previous page} \\ 
\hline 
& A & B & C \\ 
\hline 
\endhead 
\hline 
\multicolumn{4}{r}{\small\sl table continued on next page} \\ 
\endfoot 
\endlastfoot 
1 & -0.36 & -2.06 & -1.44 \\ 
2 & 1.01 & -0.01 & -0.90 \\ 
3 & 1.48 & 0.19 & 1.43 \\ 
4 & 0.58 & -0.58 & 2.27 \\ 
\hline 
\end{longtable} 

で、出力したsnwファイルから、% delete line start ~ % delete line end、までをrubyで整形してplatex -> dvipdfmxしてました。

当時は、xtable関数そのものを書き換える力もなく、こんなスパゲティーコードでした。
恥ずかしい限りです…