【发布时间】:2021-03-24 00:07:02
【问题描述】:
我正在尝试从循环中将表格打印为 pdf,但我一直没有运气。我最初使用的是 Flextable,但在网上看到 cmets 无法做到这一点是它的问题。所以,我改用 Kable 和 Kableextra。我仍然没有成功完成这项工作。那里有一些线索暗示了各种各样的事情。我已经尝试了所有这些,但似乎没有什么对我有用。因此,我创建了一个非常简单的示例来说明我的位置,我希望有人能够向我展示这是如何完成的。
这是 RMarkdown 代码。
---
title: "test_print"
output: pdf_document
---
knitr::opts_chunk$set(echo = FALSE)
library(kableExtra)
#create a list and put two kables in it
lst <- vector("list", 2)
lst[[1]] <- kbl(head(cars), booktabs = T)
lst[[2]] <- kbl(head(pressure), booktabs = T)
lst[[1]] #this code works fine producing a table in the pdf file
for (i in 1:2)
{
lst[[i]] #this code produces no output at all
}
#this produces a shiny tag list with the reports seemingly there but they do not output properly
for (i in 1:2)
{
print(lst[[i]])
}
网上有很多关于如何解决这个问题的建议,但我一直无法解决。例如,在几个地方它说你必须将 kable 包装在 print 中,因为副作用在循环中无法正常工作。
我是 R 和 RMarkdown 的新手,所以我可能在这里遗漏了一些简单的东西,但也有足够多的其他人遇到过这个问题(鉴于我找到的线程),所以也许它毕竟不是那么简单。
我将不胜感激并提供有关如何解决此问题的想法。
【问题讨论】:
-
你只需要在 RMarkdown 块中包含
results = 'asis'。详情请见this。 -
谢谢,我以前发现过这个,但不知怎的,我从来没有得到正确的组合——请参阅我对自己问题的回答。
标签: r for-loop pdf printing kable