【发布时间】:2019-10-01 17:34:45
【问题描述】:
这是this one 的后续问题。
在上述问题中,我可以很好地实现标题创建和循环打印 ggplot2 对象。
现在我有一个新问题:我还需要在循环中打印模型的摘要。问题是如果我有asis 选项,它就不起作用。
有什么想法吗?
[重复:]
---
title: "Untitled"
output:
html_document:
theme: united
highlight: tango
toc: true
toc_float:
collapsed: false
smooth_scroll: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
```
```{r}
library(dplyr)
library(ggplot2)
df <- datasets::iris %>%
dplyr::as_tibble()
```
```{r species_loop, results='asis'}
for(i in c("setosa", "versicolor", "virginica")) {
cat(paste0("\n\n## ", i, "\n"))
df_filtered <- df %>%
dplyr::filter(Species == i)
p <- df_filtered %>%
ggplot2::ggplot(ggplot2::aes(Sepal.Length, Petal.Length)) +
ggplot2::geom_point()
print(p)
my_model <- lm(Sepal.Length ~ Petal.Length, data = df_filtered)
summary(my_model) %>%
print()
}
```
## I need the printing output of the model to look like this:
```{r}
df_filtered <- df %>%
dplyr::filter(Species == "setosa")
my_model <- lm(Sepal.Length ~ Petal.Length, data = df_filtered)
summary(my_model) %>%
print()
```
【问题讨论】:
-
我不确定你的意思,因为它是一个“第 2 部分”问题,但是当我用
broom::glance(my_model)替换 print() 时,你可以创建一个整洁的结果表和管道它变成一个 kable() -
是的我知道,但在我的情况下我无法整理它..我需要使用对象的打印方法
-
this issue 中的讨论虽然相当陈旧,但似乎表明 asis 加逐字文本输出将会很困难。
标签: r r-markdown