R markdown表循环NULL结果



我的代码在html输出的末尾产生了很多NULL

你能帮我预防一下吗?

---
title: "Test"
output: html_document
---
```{r warning=FALSE, message=FALSE, results = 'asis',echo=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
# nest all data except the cut column and create html tables
diamonds_tab <- diamonds %>%
nest(-cut) %>%
mutate(tab = map2(cut,data,function(cut,data){
writeLines(landscape(kable_styling(kable(as.data.frame(head(data)),
caption =cut,
format = "html",align = "c",row.names = FALSE),
latex_options = c("striped"), full_width = T)))
}))
# print tab column, which contains the html tables    
invisible(walk(diamonds_tab$tab, print))
```

不要使用invisible,而是用capture.output包装print命令。

---
title: "Test"
output: html_document
---
```{r warning=FALSE, message=FALSE, results = 'asis',echo=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)
# nest all data except the cut column and create html tables
diamonds_tab <- diamonds %>%
nest(-cut) %>%
mutate(tab = map2(cut,data,function(cut,data){
writeLines(landscape(kable_styling(kable(as.data.frame(head(data)),
caption =cut,
format = "html",align = "c",row.names = FALSE),
latex_options = c("striped"), full_width = T)))
}))
# print tab column, which contains the html tables    
walk(diamonds_tab$tab, ~ capture.output(print(.x)))
```

我发现,只需要删除行走就足够了。

相关内容

  • 没有找到相关文章

最新更新