简体   繁体   English

在 R Markdown 中创建 Kable

[英]creating Kable in R Markdown

how can I put the following model construction into a kable in R markdown.如何将以下模型构造放入 R markdown 中的 kable。

>modelTT <- glm(formula
           ,family=gaussian(link = "identity"), data=dataL_TT)

>regr_tab_NM(modelTT)

                                      Estimate Pr(>|t|)
        (Intercept)                    -2.6077  < 0.001
        Days_diff_Eff_Subm_2           -0.0114  < 0.001
        TR_BS_BROKER_ID_360_2           0.0344  < 0.001
        TR_BS_BROKER_ID_360_M           0.8551  < 0.001
        RURALPOP_P_CWR_2               -0.0083  < 0.001
        RURALPOP_P_CWR_M               -0.7106  < 0.001
        TR_B_BROKER_ID_360              0.0241  < 0.001
        TR_SCW_BROKER_ID_360           -0.0005  < 0.001
        PIP_Flag                        3.5838  < 0.001
        TR_BS_BROKER_INDIVIDUAL_720_2   0.0357  < 0.001
        TR_BS_BROKER_INDIVIDUAL_720_M  -0.0780  < 0.001
        Resolved_Conflictless5m         1.1547  < 0.001
        Resolved_Conflict5mTo1d         1.5352  < 0.001
        Resolved_Conflictafter1d        2.1279  < 0.001
        Priority_2Other                -1.1499  < 0.001

So first I started with library(knitr) then kable(modelTT, .....) I'm not sure if this is correct.所以首先我从 library(knitr) 开始,然后是 kable(modelTT, .....) 我不确定这是否正确。

One can print tabular content from R functions directly in R Markdown with kable() by saving the object to be printed, and then using an inline R function (vs. a block) to render the table.通过保存要打印的对象,然后使用内联 R 函数(相对于块)来呈现表格,可以使用kable()直接在 R Markdown 中打印来自 R 函数的表格内容。

To print the regression coefficients from a model, one needs to identify the specific object that contains the coefficients and print it, rather than the entire summary() object.要从模型打印回归系数,需要识别包含系数的特定对象并打印它,而不是整个summary()对象。 We'll illustrate with a simple example using lm() .我们将通过一个使用lm()的简单示例进行说明。

The output object from summary(lm(...)) is a list of 11 objects, one of which is called coefficients . summary(lm(...))的输出对象是一个包含 11 个对象的列表,其中之一称为coefficients The coefficients object contains the regression slopes, standard errors, t and probability values. coefficients对象包含回归斜率、标准误差、t 和概率值。

The following code can be saved as an Rmd file and knit with knitr to print these items as a kable() table.以下代码可以保存为 Rmd 文件,并使用knitr将这些项目打印为kable()表。

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Stackoverflow question [48804938](https://stackoverflow.com/questions/48804938/creating-kable-in-r-markdown) asked how to take output from a linear model and render it with kable in R Markdown. We'll run a `lm()` with the `mtcars` data frame and print the coefficients table with kable. 

```{r regression, echo = TRUE, warning = FALSE, eval = TRUE}
 library(knitr)
 fit <- lm(mpg ~ am + disp + wt, data = mtcars)
 theStats <- summary(fit)


```

At this point we can print the coefficients table with `theStats$coefficients` as the argument to the `kable()` function.

`r kable(theStats$coefficients)`

When we knit the document to HTML, it produces the following document.当我们将文档编织为 HTML 时,它会生成以下文档。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM