Reputation: 1723
I was trying to do the following using example , but i want to autogenerate the tab with summary table of lm()
I created first the list with all summary tables: list_lm
---
title:
author:
date:
output:
html_document
---
# {.tabset}
```{r}
list_lm=list()
for(i in 1:10){
list_lm[[i]]= lm(dist ~ speed, data=cars)
}
```
```{r,results='asis', echo=FALSE}
for(i in 1:10){
cat('##',i,' \n')
print(list_lm[[i]] )
}
```
but it does not seem to produce a nice output when I do print(list_lm[[i]] )
Upvotes: 3
Views: 1209
Reputation: 41260
You could use knitr::kable
to better format the output:
---
output:
html_document
---
# {.tabset}
```{r}
list_lm=list()
for(i in 1:10){
list_lm[[i]]= lm(dist ~ speed, data=cars)
}
```
```{r,results='asis', echo=FALSE}
for(i in 1:10){
cat('##',i,' \n')
cat("Coefficients: \n")
print(knitr::kable(list_lm[[i]]$coefficients))
cat("\n")
cat("Summary: \n")
s <- summary(list_lm[[i]])
print(knitr::kable(data.frame(sigma = s$sigma,r.squared=s$r.squared)) )
cat('\n')
}
```
Another option is to use broom
package :
---
output:
html_document
---
`r knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, cache = F)`
# {.tabset}
```{r, ECHO = F, MESSAGE = F}
library(dplyr)
library(broom)
list_lm=list()
for(i in 1:10){
list_lm[[i]]= lm(dist ~ speed, data=cars)
}
```
```{r,results='asis', echo=FALSE}
for(i in 1:10){
cat('##',i,' \n')
list_lm[[i]] %>% tidy() %>% knitr::kable() %>% print
cat('\n')
}
```
Upvotes: 2