(#)
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.
(##)
(###)
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")
(###)
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
(##)
(###)
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.
(###)
A table of contents (toc) will be created if we declare
toc: true
in the YAML header of the documents.
(###...{.unnumbered})
Unnumbered sections can be defined be adding {.unnumbered} after the section title, e.g.:
### Unnumbered sections {.unnumbered}
Alternatively you may use
### Unnumbered 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.
(####...{.unnumbered .unlisted})
Unlisted sections must be declared unnumbered, too.
(####...{.unnumbered .unlisted})
Unlisted sections are often assigned to low level headers.
(##)
(###)
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")
)
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")
)
School | Average Test Score |
---|---|
Hillcrest High School | 48.84623 |
Oakwood High School | 39.32777 |
St Mary’s Church of England School | 47.19057 |
(###)
Cross referencing of sections and tables is only supported in R Bookdown. In text references such as (see Tab. 1) must be written manually.