gpt4 book ai didi

r - knitr:生成带有循环标签的汇总表?

转载 作者:行者123 更新时间:2023-12-04 09:25:57 25 4
gpt4 key购买 nike

我试图使用 example 执行以下操作,但我想自动生成带有 lm() 汇总表的选项卡
我首先创建了包含所有汇总表的列表: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]] )
}
```
但是当我做 print(list_lm[[i]] ) 时它似乎没有产生很好的输出
https://stackoverflow.com/questions/24342162/regression-tables-in-markdown-format-for-flexible-use-in-r-markdown-v2

最佳答案

您可以使用 knitr::kable更好地格式化输出:

---
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')
}
```
enter image description here
另一种选择是使用 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')
}
```
enter image description here

关于r - knitr:生成带有循环标签的汇总表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63008738/

25 4 0