---
title: "Automated Reporting: Getting Started"
output: 
  rmarkdown::html_vignette:
    toc: true
    fig_width: 10.08
    fig_height: 6
tags: [r, report]
vignette: >
  %\VignetteIndexEntry{Automated Reporting: Getting Started}
  \usepackage[utf8]{inputenc}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r, echo=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  warning = FALSE,
  message = FALSE,
  out.width = "100%",
  comment = "#"
)

options(
  knitr.kable.NA = "",
  width = 60
)

pkgs <- c("dplyr", "lme4")
successfully_loaded <- vapply(pkgs, requireNamespace, FUN.VALUE = logical(1L), quietly = TRUE)
can_evaluate <- all(successfully_loaded)

if (can_evaluate) {
  knitr::opts_chunk$set(eval = TRUE)
  vapply(pkgs, require, FUN.VALUE = logical(1L), quietly = TRUE, character.only = TRUE)
} else {
  knitr::opts_chunk$set(eval = FALSE)
}

library(report)
```

# Installation

First, install R and R studio. Then, copy and paste the following lines in the
console:

```{r eval=FALSE}
install.packages("remotes")
remotes::install_github("easystats/report") # You only need to do that once
```

```{r eval=FALSE}
library("report") # Load the package every time you start R
```

Great! The `report` package is now installed and loaded in your session.

# Supported Objects

The `report` package works in a two step fashion:
- First, you create a `report` object with the `report()` function.
- Second, this report object can be displayed either textually (the default
output) or as a table, using `as.data.frame()`. Moreover, you can also access a
more compact version of the report using `summary()` on the report object.

## Dataframes

If an entire dataframe is supplied, `report` will provide descriptive statistics
for all columns:

```{r}
report(iris)
```

## Grouped Dataframes

The dataframe can also be a *grouped* dataframe (from `{dplyr}` package), in which case
`report` would return a separate report for each level of the grouping variable.
Additionally, instead of textual summary, `report` also allows one to return a
tabular summary using the `report_table()` function:

```{r}
iris |>
  group_by(Species) |>
  report_table()
```

## Correlations, t-test, and Wilcox test

`report` can also be used to provide automated summaries for statistical model
objects from correlation, *t*-tests, Wilcoxon tests, etc.

```{r}
report(t.test(formula = mtcars$wt ~ mtcars$am))
```

```{r, eval=FALSE}
report(cor.test(mtcars$mpg, mtcars$wt))
```

## Regression models

### Linear regression (`lm`)

We will start out simple: a simple linear regression

```{r}
model <- lm(wt ~ am + mpg, data = mtcars)

report(model)
```

### anova (`aov`)

And its close cousin ANOVA is also covered by `report`:

```{r}
model <- aov(wt ~ am + mpg, data = mtcars)

report(model)
```

### General Linear Models (GLMs) (`glm`)

```{r}
model <- glm(vs ~ mpg + cyl, data = mtcars, family = "binomial")

report(model)
```

### Linear Mixed-Effects Models (`merMod`)

```{r}
library(lme4)

model <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy)

report(model)
```
