Publication-ready Tables"

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)

Getting started with correlations

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"
)

t-tests, ...

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)

ANOVAs

Linear Models

model <- lm(Petal.Length ~ Species * Petal.Width, data = iris)

report_table(model)


Try the report package in your browser

Any scripts or data that you put into this service are public.

report documentation built on Sept. 11, 2024, 8:47 p.m.