r - 使用 longtable 自定义的 Xtable 两列

标签 r knitr xtable longtable

这是 question 的延续我之前发过。我在 RStudio 中有一个 code.Rnw 文件,我使用 knit("code.Rnw") 命令编织成一个 code.tex 文件。

我有一个正在使用 xtable 命令打印的数据框。在下面的示例中,它是 20 行。但是,为了节省空间,我将其打印为两列,每列 10 行。

下面是我的代码:

\documentclass[11pt, a4paper]{article}
\usepackage[margin=3cm]{geometry}
\usepackage{longtable}

\begin{document}

<<echo=FALSE,results='asis'>>=
library(xtable)
set.seed(1)

spaceCol =  rep("     ",10)
df1 = data.frame(student = letters[1:10], vals=runif(10, 1, 10))
df2 = data.frame(student = letters[11:20], vals=runif(10, 1, 10))
dfFull = data.frame(df1,spaceCol,df2)
names(dfFull) = c(" ","% Correct","     "," ", "% Correct")
row.names(dfFull) = NULL

x.big <- xtable(dfFull, label ='tabtwo',caption ='Caption for table with student scores')
print(x.big, tabular.environment ='longtable', floating = FALSE, include.rownames=FALSE)
@

\end{document}

输出结果如下:

Current output

我喜欢这种输出的美感,尤其是因为在 longtable 格式中,这种输出会在需要时自动分页。但是,我正在努力改进的是,让这个输出实际上是两个不同的列变得更容易可视化。

为此,我想在两列之间添加一个空格,这样输出看起来更像下面这样:

Desired output

但是,如果这被证明是不可能的,那么我可以考虑添加一条垂直线来区分两列,如下所示:

Alternative desired output

鉴于我在使用 xtable 方面的限制,这怎么可能?

最佳答案

\documentclass[11pt, a4paper]{article}

\usepackage{subfig}

\begin{document}

<<echo = FALSE>>=
library(xtable)

opts_chunk$set(
  echo = FALSE,
  results = 'asis'
)

set.seed(1)

mynames <- c("", "% Correct")

df1 = data.frame(letters[1:10], runif(10, 1, 10))
df2 = data.frame(student = letters[11:20], vals=runif(10, 1, 10))

colnames(df1) <- mynames
colnames(df2) <- mynames
@

\begin{table}\centering
\subfloat{
<<>>=
print(xtable(df1), floating = FALSE, include.rownames = FALSE)
@
} \hspace{2cm}
\subfloat{
<<>>=
print(xtable(df2), floating = FALSE, include.rownames = FALSE)
@
}
\caption{Caption for table with student scores} \label{tabtwo}
\end{table}
\end{document}

Result

唯一的缺点是,您不能通过这种方法使用 longtable

更新:这是一个使用longtable 的替代方法。诀窍是使用 xtable 仅用于表格的 contents 并手动构建标题,因此您可以完全控制所有行等。我决定使用一个空的列的空间,因为使第 2 列变宽会使水平线看起来很难看。

\documentclass{article}

\usepackage{longtable}

\begin{document}
\thispagestyle{empty}

<<echo = FALSE>>=
library(xtable)

opts_chunk$set(
  echo = FALSE,
  results = 'asis'
)

set.seed(1)

df1 = data.frame(letters[1:10], runif(10, 1, 10))
df2 = data.frame(student = letters[11:20], vals=runif(10, 1, 10))

dfFull <- cbind(df1, NA, df2)
@

\begin{longtable}{lrl@{\hskip 2cm}lr} \cline{1-2} \cline{4-5}
 & \% Correct & & & \% Correct \\ \cline{1-2} \cline{4-5}
<<>>=
print(xtable(dfFull), only.contents = TRUE, include.rownames = FALSE, include.colnames = FALSE, hline.after = NULL)
@
\cline{1-2} \cline{4-5}
\caption{Caption for table with studen scores} \label{tabtwo}
\end{longtable}
\end{document}

Result

UPDATE2:最后,一个使用 longtable 且不涉及“手动”创建表的一半的解决方案。诀窍是删除所有水平线 (hline.after = NULL) 并在需要时使用 add.to.row 添加 \clines (灵感来自 this question )。

\documentclass{article}

\usepackage{longtable}

\begin{document}
\thispagestyle{empty}

<<echo = FALSE, results = 'asis'>>=
library(xtable)

set.seed(1)


df1 = data.frame(letters[1:10], runif(10, 1, 10))
df2 = data.frame(letters[11:20], runif(10, 1, 10))

dfFull <- cbind(df1, NA, df2)

# To test "longtable", rbind data several times:
multiply <- 5
dfFull <- do.call("rbind", replicate(multiply, dfFull, simplify = FALSE))

colnames(dfFull) <- c("", "% Correct", "", "", "% Correct")

print(xtable(dfFull,
             caption = "Caption for table with student scores",
             label = "tabtwo",
             align = c("l",  # ignored (would apply to colnames)
                       "l", "r",
                       "l@{\\hskip 2cm}", # space between blocks
                       "l", "r")),
      include.rownames = FALSE,
      include.colnames = TRUE,
      hline.after = NULL, # Remove all default lines. A line after the very last row remains, which is automatically added when using "longtable".
      tabular.environment = "longtable",
      floating = FALSE,
      add.to.row = list(
        pos = list(-1, 0),
        command = rep("\\cline{1-2} \\cline{4-5}", 2))
      )
@
\end{document}

关于r - 使用 longtable 自定义的 Xtable 两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31247757/

相关文章:

r - error "running ' zip' failed"in compile R package

r - Shiny :runExample 给出错误(不能从 `runApp()` 内调用 `runApp()` )

r - 添加脚注/感谢 Rmarkdown 标题幻灯片

r - knitr:如何在不使用 setwd() 的情况下在 knit2html 中设置图形路径?

r - 你能在 R 的 xtable 中合并单元格吗

r - sparkr 数据 block 错误 : too many open devices

r - 在特定条件下创建二进制矩阵

html - R:输出带有小计的类似数据透视表

r - 如何告诉 R 在 Windows 中使用代理自动配置脚本 (PAC)

r - 使用 xtable 函数更改从 lm 回归中获得的表的行名称