简体   繁体   English

Sweave中的xtable:在表格中指定前导或中间规则+对齐

[英]xtable in Sweave: specify toprule or midrule in a table + alignement

I am producing an automatic report using Sweave in R. I would like: 我正在使用R中的Sweave生成自动报告。我想:

  1. In position (nrow -1) I would like to have a toprule instead of a midrule 在位置(第-1行),我希望有一个中规而不是中规
  2. Align the first row as being centered and then I would like to align number to the right in order to align digits. 将第一行居中对齐,然后将数字右对齐以对齐数字。

Here is my code: 这是我的代码:

\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\z@}
\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} 

From the documentation , use add.to.row in conjunction with hline.after=NULL to control what you add to the lines from the hline vector. 文档中 ,将add.to.rowhline.after=NULL结合使用,以控制您要添加到hline向量行中的hline

library(xtable)
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)

Data 数据

data <- structure(list(l = c(NA, NA, NA), w = c(515, 75, 85), a = c(0.22, 
0.45, 0.45), m = c(127.83, 33.75, 38.25)), class = "data.frame", row.names = c("x", "y", "z"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM