This tutorial is based on R Markdown Cookbook by Xie et al. (2018). See the link

1 List of libraries used in this project

library(bookdown)
library(DiagrammeR)
library(equatiomatic)
library(kableExtra)
library(xtable)
library(pander)
library(corrplot)
## corrplot 0.92 loaded

2 Structure of the report

DiagrammeR::grViz("digraph {
  graph [layout = dot, rankdir = TB]
  
  node [shape = rectangle]        
  rec1 [label = '1. Introduction']
  rec2 [label = '2. Exploratory Analysis']
  rec3 [label = '3. Predictive Analysis']
  rec4 [label = '4. Recommendations/Conclusions']
  
  # edge definitions with the node IDs
  rec1 -> rec2 -> rec3 -> rec4
  }",
  height = 500)

3 Various Table outputs

3.1 A table using xtable package

print(xtable(head(iris), caption = "My table using xtable package", label = "xtabletab"), comment = FALSE,
            caption.placement = "top", type = "html")
My table using xtable package
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.10 3.50 1.40 0.20 setosa
2 4.90 3.00 1.40 0.20 setosa
3 4.70 3.20 1.30 0.20 setosa
4 4.60 3.10 1.50 0.20 setosa
5 5.00 3.60 1.40 0.20 setosa
6 5.40 3.90 1.70 0.40 setosa

3.2 A table using knitr::kable

dat <- head(iris)
knitr::kable(dat, caption = "My table using knitr::kable",
                         booktabs = T, label = "kabletable")
Table 3.1: My table using knitr::kable
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

3.3 A table using kableExtra package

dat <- head(iris)
kab <- knitr::kable(dat, caption = "My table using kable classic",
                         booktabs = T, label = "kabletable")
kable_classic_2(kab, full_width = F)
Table 3.1: My table using kable classic
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

4 A simple plot

plot(cars)
Distance vs. speed

Figure 4.1: Distance vs. speed

4.1 Simple linear regression

fit <- lm(mpg ~ hp, mtcars)

# Show the linear model
equatiomatic::extract_eq(fit)

\[ \operatorname{mpg} = \alpha + \beta_{1}(\operatorname{hp}) + \epsilon \]

# display the actual coefficients
equatiomatic::extract_eq(fit, use_coefs = TRUE)

\[ \operatorname{\widehat{mpg}} = 30.1 - 0.07(\operatorname{hp}) \]

plot(mpg ~ hp, mtcars)
MPG (miles per gallon) vs. HP (Horse Power

Figure 4.2: MPG (miles per gallon) vs. HP (Horse Power

5 Appendix: Extracting all R codes used in this report

library(bookdown)
library(DiagrammeR)
library(equatiomatic)
library(kableExtra)
library(xtable)
library(pander)
library(corrplot)

DiagrammeR::grViz("digraph {
  graph [layout = dot, rankdir = TB]
  
  node [shape = rectangle]        
  rec1 [label = '1. Introduction']
  rec2 [label = '2. Exploratory Analysis']
  rec3 [label = '3. Predictive Analysis']
  rec4 [label = '4. Recommendations/Conclusions']
  
  # edge definitions with the node IDs
  rec1 -> rec2 -> rec3 -> rec4
  }",
  height = 500)
print(xtable(head(iris), caption = "My table using xtable package", label = "xtabletab"), comment = FALSE,
            caption.placement = "top", type = "html")
dat <- head(iris)
knitr::kable(dat, caption = "My table using knitr::kable",
                         booktabs = T, label = "kabletable")

dat <- head(iris)
kab <- knitr::kable(dat, caption = "My table using kable classic",
                         booktabs = T, label = "kabletable")
kable_classic_2(kab, full_width = F)

plot(cars)
fit <- lm(mpg ~ hp, mtcars)

# Show the linear model
equatiomatic::extract_eq(fit)

# display the actual coefficients
equatiomatic::extract_eq(fit, use_coefs = TRUE)

plot(mpg ~ hp, mtcars)

  1. U of Windsor, ↩︎