Level 1 Heading

Level 2 Heading

Level 3 Heading

Level 4 Heading

Formatiing

italic

bold

code

superscript2

–strikethrough–

Text link

Horizontal line:


block quote

block quote with bold and italic text

  • list
  • list
  • list
  • list
  1. list

  2. list

    1. list

    2. list

Equation using LaTeX formatting:

\(\pi*r^{2}\)


Code and Code Chunks

Insert a code chunk with

  • Windows: CTRL + ALT + i
  • Mac: CMD + Option + i

Code Chunk Tools

  • Gear icon - settings for chunk output (see below)
  • Upside down triangle (run all chunks above this one)
  • Play button (run code in this chunk)

Default Code Output Options

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.0     v dplyr   1.0.5
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 4.0.3
## Warning: package 'tibble' was built under R version 4.0.4
## Warning: package 'dplyr' was built under R version 4.0.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x ggplot2::%+%()   masks psych::%+%()
## x ggplot2::alpha() masks psych::alpha()
## x purrr::compose() masks flextable::compose()
## x dplyr::filter()  masks stats::filter()
## x dplyr::lag()     masks stats::lag()
penguins %>%
  group_by(sex) %>%
  drop_na(sex) %>%
  summarize(mean = mean(body_mass_g))
## # A tibble: 2 x 2
##   sex     mean
##   <fct>  <dbl>
## 1 female 3862.
## 2 male   4546.
detach("package:tidyverse", unload = TRUE) #unloading for purposes of next chunk

Use warning = FALSE

library(tidyverse)
penguins %>%
  group_by(sex) %>%
  drop_na(sex) %>%
  summarize(mean = mean(body_mass_g))
## # A tibble: 2 x 2
##   sex     mean
##   <fct>  <dbl>
## 1 female 3862.
## 2 male   4546.

Use message = FALSE

penguins %>%
  group_by(sex) %>%
  drop_na(sex) %>%
  summarize(mean = mean(body_mass_g))
## # A tibble: 2 x 2
##   sex     mean
##   <fct>  <dbl>
## 1 female 3862.
## 2 male   4546.

Use include = FALSE

Use eval = FALSE

penguins %>%
  group_by(sex) %>%
  drop_na(sex) %>%
  summarize(mean = mean(body_mass_g))

Typical Usage

## # A tibble: 2 x 2
##   sex     mean
##   <fct>  <dbl>
## 1 female 3862.
## 2 male   4546.

With a Plot

With a table

With an interactive table (HTML only)

More about tables

There are a number of packages that are useful for making tables, especially from statistical results. Let’s run a few analyses and see.

T-Test

The default t-test output is a mess

## 
##  Two Sample t-test
## 
## data:  body_mass_g by sex
## t = -8.5417, df = 331, p-value = 4.897e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -840.8014 -526.0222
## sample estimates:
## mean in group female   mean in group male 
##             3862.273             4545.685

We can use the broom::tidy package to get the model estimates and with a little renaming make a nice table:

Regression

There are a number of ways to make a regression table, including the use of broom::tidy. The modelsummary package makes it even easier! The result below is just the basic output. There are a lot of customization options, includng which model statistics are or are not included.

This is output as a flextable so it can be knitted in Word.

EFA

This example extracts eigenvalues from a simple psych and makes it into a table.

The psych output for an EFA can get quite messy and be hard to work with. I wrote the function fa_table to easily display the results and remove loadings under a specified cut value.

SEM estimates can be displayed like any other dataframe. You can use semPlot or lavaanPlot to easily display path diagrams, too.

SEM

Referencing

It’s easy to include references in a R Markdown document. You will need to set a citation style (and include a CSL file) and a bibliography file (you can export these from Zotero, Mendely, etc.) in the YAML. It looks like this:

bibliography: example.bib
csl: apa.csl

Then you can easily write references using square brackets and the @ symbol. There is also an RStudio addin called citr that allows you do search your .bib file and insert citations via point-and-click.

Blah blah (see Rocconi et al., 2020, pp. 33–35; also Morrow & Ackermann, 2012, ch. 1).

Blah blah (Rocconi et al., 2020, pp. 33–35, 38–39).

Blah blah (Morrow & Ackermann, 2012; Rocconi et al., 2020).

Blah blah (see Rocconi et al., 2020, pp. 33–35; also Morrow & Ackermann, 2012, ch. 1).

Blah blah (Morrow & Ackermann, 2012, pp. 33–35, 38–39).

Blah blah (Morrow & Ackermann, 2012; Rocconi et al., 2020).

Reference List

The reference list will appear at the end by default or you can place it manuualy using <div id="refs"></div>.

Morrow, J., & Ackermann, M. (2012). Intention to persist and retention of first-year students: The importance of motivation and sense of belonging. College Student Journal, 46(3), 483–491.

Rocconi, L. M., Liu, X., & Pike, G. R. (2020). The impact of person-environment fit on grades, perceived gains, and satisfaction: An application of holland’s theory. Higher Education, 1–18.

Better Output Options

  • Publishing to a website (via Rpubs)
  • Making an awesome Word Document

What else can you do with R Markdown?

Some Resources