Table 在 r 中有多个子行并写入 pdf

Table in r with multiple sub rows and write to pdf

是否可以在 R 中生成多行对应单行的 table?并将结果 table 写入 pdf。示例 table 如下。

是否可以按照图中显示的方式连接两个单独的 table。 下面给出了两个 table 的示例代码

tab1="Test Description
1 test1 description
2 test2 description"
table1 <-read.table(text = tab1,header = TRUE)

tab21="Cause Probability Feedback
1 cause1 .5 positive
2 Cause2 .2 negative
3 Cause3 .1 negative
4 Cause4 .2 negative"
table2 <-read.table(text = tab21,header = TRUE)

tab22="Cause Probability Feedback
1 cause1 .7 positive
2 Cause2 .2 negative
3 Cause3 .1 negative"
table3 <-read.table(text = tab22,header = TRUE)

这有点棘手,但我会利用 print.xtable 函数将具有 NA 的单元格打印为空的事实。单元格不是真正的 'merged',但当内容与顶部对齐时看起来像。

基本上步骤是:

  1. 在R
  2. 中生成一个suitabledata.frame
  3. 使用包 'xtable[= 中的 print.xtable 打印为 .tex 兼容 table 52=]'
  4. 使用Sweave/knitr/etc生成一个suitable.tex
  5. tools::texi2pdf 然后会将您的 .tex 转换为 suitable .pdf

这里是文件,你只需要在你的 R 终端中获取 RunSweave.R(并确保你已经安装了 LaTeX 以及所需的包,即 'xtable 并且有单个文件夹中的文件;这是 Windows 中的 运行。

文件 StackExampleCode.R:

# StackExampleCode.R
library(xtable)

# A work-around by setting rows in the multi-row to NA after the initial top-justified line
header <- data.frame(test = "Tests", det = "Details", cause = "Cause", prob = "Probability", fb = "Feedback")
# Filling the fields for these is something you'd probably want to do in R in a more sophisticated manner
test1 <- data.frame(
    test = c("Test 1", NA, NA, NA, NA),
    det = c("Description", NA, NA, NA, NA),
    cause = c("Cause 1", NA, paste("Cause", 2:4)),
    prob = c(".5", NA, ".2", ".1", ".2"),
    fb = c("positive", NA, "negative", "negative", "negative")
)
test2 <- data.frame(
    test = c("Test 2", NA, NA, NA),
    det = c("Description", NA, NA, NA),
    cause = c(paste("Cause", 1:3), NA),
    prob = c(".7", ".1", ".2", NA),
    fb = c("positive", "negative", "negative", NA)
)
# Bind it all together, you probably want something similar if it's automatic data you're generating
tab <- rbind(header, test1, test2)

文件StackExampleRnw.Rnw:

% StackExampleRnw.Rnw
% Note the different comment char, writing .tex here
\documentclass{article}

\begin{document}

<<echo=FALSE, results=tex>>=
# Printing the table
print(
    xtable(tab, 
        align = "|l|l|l|l|l|l|" # Create the desired vertical lines and text alignments ala LaTeX; left align with vertical lines in-between each column)
    ),
    add.to.row = list( # Add horizontal lines to correct spots, should be adjusted according to the desired data
        pos = list(-1, 1, 6, nrow(tab)), 
        command = c("\hline \n", "\hline \n", "\hline \n", "\hline \n") # Horizontal lines and a neater formatting of output using a linechange
    ),
    include.rownames = FALSE, # Don't include the rownames (which would be just numbers)
    include.colnames = FALSE, # Don't include the rownames, these were already included as if it was an ordinary table row
    hline.after = NULL # Suppress the empty horizontal line that is intended for an automated caption
)
@
\end{document}

文件RunSweave.R:

# RunSweave.R

# Run the code
source("StackExampleCode.R")
# Bundle R code with LaTeX
Sweave("StackExampleRnw.Rnw")
# .tex -> .pdf
tools::texi2pdf("StackExampleRnw.tex")

这是我在 StackExampleRnw.pdf:

中的样子

或者,您可以直接访问文件 StackExampleRnw.tex 中 .tex 中的 table,如果您愿意table,则可以进行一些额外的格式化。以上不需要在 .tex 中进行任何额外的修改,但您需要确保将水平线和 NA 放在正确的位置。

如果您不喜欢table .tex,print.xtable-函数有大量参数可用于进一步格式化。如果右边三列中的部分水平线对你来说真的很重要,我可能会把它分成两个 table,然后把它们水平地粘在一起,让右边的每一个都有一条水平线行。

我希望在 pixiedust 中通过合并一些单元格来完成此操作,但我在 pixiedust 中似乎有一个缺陷,它不允许在合并的行上使用垂直边框。解决方法使用 Teemu 的方法,将我们不想查看的单元格设置为 NA 并将它们打印为空字符。

library(dplyr)
library(pixiedust)

table2$Test <- "test1"
table3$Test <- "test2"

bind_rows(
  right_join(table1, table2),
  right_join(table1, table3)
) %>%
  mutate(Description = as.character(Description)) %>%
  group_by(Test) %>%
  mutate(Description = ifelse(duplicated(Description), NA, Description)) %>%
  ungroup() %>%
  mutate(Test = ifelse(duplicated(Test), NA, Test))%>%
  dust(float = FALSE) %>%
  sprinkle(cols = 1:2,
           rows = c(4, 7),
           border = "bottom") %>%
  sprinkle(cols = 1:2,
           rows = 1,
           border = "top") %>%
  sprinkle(cols = 1:2, 
           border = "left",
           na.string = "") %>%
  medley_all_borders(cols = 3:5) %>%
  medley_all_borders(part = "head") %>%
  sprinkle_print_method("latex")

一个完整的、有效的 RMD 文件应该是:

---
title: "Untitled"
output: pdf_document
header-includes: 
- \usepackage{amssymb} 
- \usepackage{arydshln} 
- \usepackage{caption} 
- \usepackage{graphicx} 
- \usepackage{hhline} 
- \usepackage{longtable} 
- \usepackage{multirow} 
- \usepackage[dvipsnames,table]{xcolor} 

---

```{r, echo = FALSE, message = FALSE, warning = FALSE}
library(dplyr)
library(pixiedust)

tab1="Test Description
1 test1 description
2 test2 description"
table1 <-read.table(text = tab1,header = TRUE)

tab21="Cause Probability Feedback
1 cause1 .5 positive
2 Cause2 .2 negative
3 Cause3 .1 negative
4 Cause4 .2 negative"
table2 <-read.table(text = tab21,header = TRUE)

tab22="Cause Probability Feedback
1 cause1 .7 positive
2 Cause2 .2 negative
3 Cause3 .1 negative"
table3 <-read.table(text = tab22,header = TRUE)
```

```{r, echo = FALSE, message = FALSE, warning = FALSE}
table2$Test <- "test1"
table3$Test <- "test2"

bind_rows(
  right_join(table1, table2),
  right_join(table1, table3)
) %>%
  mutate(Description = as.character(Description)) %>%
  group_by(Test) %>%
  mutate(Description = ifelse(duplicated(Description), NA, Description)) %>%
  ungroup() %>%
  mutate(Test = ifelse(duplicated(Test), NA, Test))%>%
  dust(float = FALSE) %>%
  sprinkle(cols = 1:2,
           rows = c(4, 7),
           border = "bottom") %>%
  sprinkle(cols = 1:2,
           rows = 1,
           border = "top") %>%
  sprinkle(cols = 1:2, 
           border = "left",
           na.string = "") %>%
  medley_all_borders(cols = 3:5) %>%
  medley_all_borders(part = "head")
```