Code Styling

Syntax Highlighting

Code with syntax highlighting:

hello <- function(name) {
  cat(sprintf("Hello, %s!", name))
}

hello("world")
## Hello, world!

Verbatim Output

str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

Markdown

A list:

  1. One
  2. Two
  3. Three
    • hello
    • world

bold, italics, strikethrough.

A blockquote :

Hello! This is a blockquote.

A link or url: https://www.r-project.org/

Some math:

\[\begin{align*} y = X\beta + \epsilon \end{align*}\]

Now some headers:

Level 2

Level 3

Level 4

Level 5

Tabsets

The vmodern R markdown theme comes with five preset tab styles.

  • Traditional Tabs: {.tabset}
  • Traditional Pills: {.tabset .tabset-pills}
  • Modern Tabs: {.tabset .tabset-vmodern}
  • Modern Square Pills: {.tabset .tabset-pills .tabset-square}
  • Modern Circle Pills: {.tabset .tabset-pills .tabset-circle}

Traditional

Tab 1

Tab 2

Tab 3

Pills

Tab 1

Tab 2

Tab 3

vmodern Tabs

Tab 1

Tab 2

Tab 3

Grey Square Tabs

Tab 1

Tab 2

Tab 3

Grey Circle Tabs

Tab 1

Tab 2

Tab 3

Figures

A plot using the vmodern ggplot2 theme and a caption:

ggplot(iris) +
  aes(x = Sepal.Length, y = Sepal.Width, color = Species) +
  geom_point() +
  theme_vmodern() +
  scale_color_vmodern(discrete = TRUE)
A caption

Figure 1: A caption

A sequence of plots generated in a for loop using subchunkify():

for (feature in setdiff(colnames(iris), "Species")) {
  hist_plot <- ggplot(iris) +
    aes(x = .data[[feature]]) +
    geom_histogram(bins = 12, color = "grey98", fill = "#3B7EA1") +
    labs(y = "Frequency", title = feature) +
    theme_vmodern()
  subchunkify(hist_plot, i = sprintf("histogram-%s", feature), 
              fig_height = 4, fig_width = 8)
}

Tables

A table using pretty_kable():

iris %>%
  group_by(Species) %>%
  summarise(across(.fns = mean)) %>%
  pretty_kable(
    digits = 2, 
    sigfig = FALSE,
    caption = "Mean of each iris feature with the maximum value bolded in blue", 
    format = "html", 
    bold_margin = 2, 
    bold_function = ". == max(.)",
    bold_color = "red",
    bold_scheme = c(FALSE, TRUE, TRUE, TRUE, TRUE)
  )
Table 1: Mean of each iris feature with the maximum value bolded in blue
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
setosa 5.01 3.43 1.46 0.25
versicolor 5.94 2.77 4.26 1.33
virginica 6.59 2.97 5.55 2.03

A table using pretty_DT():

pretty_DT(iris, digits = 2, sigfig = FALSE, 
          caption = "Iris dataset", rownames = FALSE)

Note that both pretty_kable() and pretty_DT() enable convenient bolding options.

LS0tCnRpdGxlOiAidm1vZGVybiBUaGVtZSIKZGF0ZTogImByIGZvcm1hdChTeXMudGltZSgpLCAnJUIgJWQsICVZJylgIgpvdXRwdXQ6CiAgdnRoZW1lczo6dm1vZGVybjoKICAgIGNvZGVfZm9sZGluZzogc2hvdwogICAgbGlnaHRib3g6IHRydWUKIyBwa2dkb3duOgojICAgYXNfaXM6IHRydWUKIyB2aWduZXR0ZTogPgojICAgJVxWaWduZXR0ZUluZGV4RW50cnl7dm1vZGVybn0KIyAgICVcVmlnbmV0dGVFbmdpbmV7a25pdHI6OnJtYXJrZG93bn0KIyAgICVcVmlnbmV0dGVFbmNvZGluZ3tVVEYtOH0KLS0tCgoKYGBge3Igc2V0dXAsIGluY2x1ZGU9RkFMU0V9CmxpYnJhcnkoa25pdHIpCmxpYnJhcnkodnRoZW1lcykKbGlicmFyeShnZ3Bsb3QyKQpsaWJyYXJ5KGRwbHlyKQoKIyMgR2xvYmFsIG9wdGlvbnMKb3B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