2017-03-18 23 views
2

我生產中使用R. Sweave自動報告toprule或midrule我想:xtable在Sweave:指定在表中+ alignement

  1. 在位置(nrow -1)我想有一個toprule而不是midrule
  2. 將第一行對齊居中,然後我想將數字對齊以便對齊數字。

這裏是我的代碼:

\documentclass[a4paper,18pt]{article} 
\usepackage[top=2.5cm, left=2.5cm, right=2.5cm,bottom=2.5cm]{geometry} 
\usepackage{Sweave} 
\usepackage{booktabs} 
\makeatletter 
    \def\hrulefill#1{\leavevmode\leaders\hrule\@height#1\hfill \kern\[email protected]} 
\makeatother 
\begin{document} 
\SweaveOpts{concordance=TRUE} 

<<xtable1, results=tex,echo=FALSE>>= 
library(xtable) 

> data 
      l w a m 
x  NA 515.0 0.22 127.83 
y  NA 75.0 0.45 33.75 
z  NA 85.0 0.45 38.25 


xtab <- xtable(data) 
hline <- c(-1,0,nrow(xtab)-1,nrow(xtab)) 
align(xtab) <- "lcccc" 
print(xtab,booktabs=T,hline.after=hline) 
@ 


\end{document} 

回答

2

documentation,結合使用add.to.rowhline.after=NULL控制你從hline向量添加到行什麼。

xtab <- xtable(data) 

hline <- c(-1,0,nrow(xtab)-1,nrow(xtab)) 
htype <- c("\\toprule ", "\\midrule ", "\\toprule ","\\bottomrule ") 

print(xtab,add.to.row = list(pos = as.list(hline), 
          command = htype), 
     hline.after = NULL)