R:为什么kable不能在for循环中打印?

时间:2021-09-04 12:05:12

I'm working a report with rmarkdown and latex. I need to print a group of tables using knitr::kable, but the don't print when inside a for loop.

我正在用rmarkdown和latex工作。我需要使用knitr :: kable打印一组表,但是在for循环中不要打印。

This is my code:

这是我的代码:

---
title: "project title"
author: "Mr. Author"
date: "2016-08-30"
output: 
  pdf_document: 
    latex_engine: xelatex
bibliography: biblio.bib
header-includes:
   - \usepackage{tcolorbox}
---

Text and chunks that run ok.

```{r loadLibraries}
require(data.table)
require(knitr)
```

## Try to print a group of tables from split

```{r results = "asis"}
t1 <- data.table(a = sample(letters, 10, T), b = sample(LETTERS[1:3], 10, T))
t2 <- split(t1, t1$b)

for (i in 1:length(t2)){
    kable(t2[[i]], col.names = c("A", "B"))
}
```

It doesn't matter if I use results = "asis" or if I omit it altogether, nothing prints to the document.

如果我使用results =“asis”无关紧要,或者如果我完全省略它,则不会打印任何内容。

I've tried enclosing the kable call within a print call (print(kable(t2[[i]]...), and it successfully prints the output to the document, but the format is the same format as a standard R prompt (preceded by ##, for example), which is rather ugly.

我已经尝试在打印调用中打包kable调用(print(kable(t2 [[i]] ...),并且它成功地将输出打印到文档,但格式与标准R提示符的格式相同(例如,在##之前),这是相当丑陋的。

How can I display the tables, other than manually?

如何手动显示表格?

### EDIT ###

### EDIT ###

Some answerers have redirected me to R knitr print in a loop as a duplicate answer. It's not, because as I stated in the previous paragraph, this effectively prints the table, but the format is not the expected one. The accepted answer (and related github thread) really solved the problem.

一些回答者将我重新定向到循环中的R knitr打印作为重复答案。它不是,因为正如我在前一段中所述,这有效地打印了表格,但格式不是预期的格式。接受的答案(和相关的github线程)真的解决了这个问题。

1 个解决方案

#1


5  

This question is addressed here: https://github.com/yihui/knitr/issues/886

这个问题在这里解决:https://github.com/yihui/knitr/issues/886

All you need is a line break after each print call

您需要的是每次打印电话后的换行符

---
title: "project title"
author: "Mr. Author"
date: "2016-08-30"
output: 
  pdf_document: 
    latex_engine: xelatex
    bibliography: biblio.bib
    header-includes:
       - \usepackage{tcolorbox}
---

Text and chunks that run ok.

```{r loadLibraries}
require(data.table)
require(knitr)
```

```{r results = "asis"}
t1 <- data.table(a = sample(letters, 10, T), b = sample(LETTERS[1:3], 10, T))
t2 <- split(t1, t1$b)

for (i in 1:length(t2)){
    print(kable(t2[[i]], col.names = c("A", "B")))
    cat("\n")
}
```

#1


5  

This question is addressed here: https://github.com/yihui/knitr/issues/886

这个问题在这里解决:https://github.com/yihui/knitr/issues/886

All you need is a line break after each print call

您需要的是每次打印电话后的换行符

---
title: "project title"
author: "Mr. Author"
date: "2016-08-30"
output: 
  pdf_document: 
    latex_engine: xelatex
    bibliography: biblio.bib
    header-includes:
       - \usepackage{tcolorbox}
---

Text and chunks that run ok.

```{r loadLibraries}
require(data.table)
require(knitr)
```

```{r results = "asis"}
t1 <- data.table(a = sample(letters, 10, T), b = sample(LETTERS[1:3], 10, T))
t2 <- split(t1, t1$b)

for (i in 1:length(t2)){
    print(kable(t2[[i]], col.names = c("A", "B")))
    cat("\n")
}
```