knitr::opts_chunk$set( collapse = TRUE, message = FALSE, warning = FALSE, comment = "#" ) options( knitr.kable.NA = "", width = 60 ) if (!requireNamespace("dplyr", quietly = TRUE)) { knitr::opts_chunk$set(eval = FALSE) }
First, load the {report}
package:
library(report)
Let's start by demonstrating some features with simple tests. The function report_table()
can be used to create a table for many R objects.
results <- cor.test(mtcars$mpg, mtcars$wt) report_table(results)
We can also obtain a shorter version by running summary()
on the output (that we are going to store in a variable called t
- like table)
t <- summary(report_table(results)) t
In the example above, running just t
ran print(t)
under the hood, which prints the table inside the console. However, one can nicely display that table in markdown documents using display()
.
display(t)
We can further customize this table, by adding significance stars,
display(t, stars = TRUE, title = "Table 1", footer = "Correlation in the mtcars (n = 32) dataset.\n*** p < .001" )
It works similarly for t-tests.
results <- t.test(mtcars$mpg ~ mtcars$am) t <- summary(report_table(results)) t
Note that, by default, report_table()
prettifies the printing: that means that the column names and its content is, underneath, not necessarily what is printed, which can be a bit confusing. For instance, while the confidence interval CI
appears as one column, it this actually made of three columns! One can access this raw table as a dataframe:
as.data.frame(t)
In fact, the function used to prettify the output is called insight::format_table()
and is accessible to you too, so that you can prettify the output while keeping it as a data frame.
insight::format_table(as.data.frame(t), stars = TRUE)
Also, you can join the results of multiple tables:
results1 <- t.test(mtcars$mpg ~ mtcars$am) results2 <- t.test(mtcars$wt ~ mtcars$am) results3 <- t.test(mtcars$qsec ~ mtcars$am) results <- c( report_table(results1), report_table(results2), report_table(results3) ) display(results)
model <- lm(Petal.Length ~ Species * Petal.Width, data = iris) report_table(model)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.