1 A main title starts with 1 by default (#)

This is a primer on how to create automatic sectioning and enumeration of tables and charts in an R Markdown document. In order to follow the hierarchy of sections used, the level (number of “#”) is displayed in each header.

1.1 Set up sample data (##)

1.1.1 Data used in this document (###)

First we load school sample data, calculate test score means by school and save them as data frame:

df = read.xlsx("//mcifs02/homes$/NiMevenkamp/MCI/Lehre/09-AdvancedStatistics/ihsm24/SchoolData.xlsx") 

1.1.2 Statistics used in this document (###)

As sample statistic we use average scores by school:

means_df = data.frame(
  by(df$test_score, df$school_long_name, mean)
  )
names(means_df) = c("Score")
means_df
##                                       Score
## Hillcrest High School              48.84623
## Oakwood High School                39.32777
## St Mary’s Church of England School 47.19057

1.2 How to make use of automatic sectioning (##)

1.2.1 Section numbers (###)

In order to enable automatic section numbering we have to declare number_sections: true in the YAML header of the document. Enumeration follows the hierarchy of hashtags “#” used to define section headers in your document.

1.2.2 Table of contents (###)

A table of contents (toc) will be created if we declare toc: true in the YAML header of the documents.

Unnumbered sections (###...{.unnumbered})

Unnumbered sections can be defined be adding {.unnumbered} after the section title, e.g.:

### Unnumbered sections {.unnumbered}

Alternatively you may use

### Unnumbered sections {-}

Unlisted sections (###...{.unnumbered .unlisted})

Unlisted sections do not show up in the table of contents. They can be used to structure your text without adding complexity to the overall structure of your document.

Good to know (####...{.unnumbered .unlisted})

Unlisted sections must be declared unnumbered, too.

How to use (####...{.unnumbered .unlisted})

Unlisted sections are often assigned to low level headers.

1.3 Table enumeration (##)

1.3.1 General (###)

The score means by school can be displayed as follows ():

kable_styling(
  kable(means_df,
        col.names = c("School","Average Test Score"),
        caption = "Average Test Score by School"
        ),
  full_width = F, font_size = 13, bootstrap_options = c("hover", "condensed")
)
Average Test Score by School
School Average Test Score
Hillcrest High School 48.84623
Oakwood High School 39.32777
St Mary’s Church of England School 47.19057

Table enumeration is not supported in basic R Markdown. You may use one of the more advanced output formats as defined by R Bookdown. That said, tables can be enumerated manually by simply adding the number to the table caption:

kable_styling(
  kable(means_df,
        col.names = c("School","Average Test Score"),
        caption = "Table 1: Average Test Score by School"
        ),
  full_width = F, font_size = 13, bootstrap_options = c("hover", "condensed")
)
Table 1: Average Test Score by School
School Average Test Score
Hillcrest High School 48.84623
Oakwood High School 39.32777
St Mary’s Church of England School 47.19057

1.3.2 Referencing (###)

Cross referencing of sections and tables is only supported in R Bookdown. In text references such as (see Tab. 1) must be written manually.