R
Beginner
2 min read
Tables, Figures, and Cross-References
Example
# --- knitr::kable example chunk ---
# ```{r summary-table}
# library(knitr)
# library(kableExtra)
# library(dplyr)
# data(mtcars)
#
# summary_tbl <- mtcars |>
# group_by(cyl) |>
# summarise(
# n = n(),
# avg_mpg = round(mean(mpg), 1),
# avg_hp = round(mean(hp), 1),
# avg_wt = round(mean(wt), 2)
# )
#
# kable(summary_tbl,
# caption = "Summary statistics by cylinder count",
# col.names = c("Cylinders","Count","Avg MPG","Avg HP","Avg Weight")) |>
# kable_styling(bootstrap_options = c("striped", "hover"),
# full_width = FALSE) |>
# column_spec(1, bold = TRUE) |>
# footnote(general = "Weight in thousands of pounds.")
# ```
# --- Figure with caption and bookdown cross-reference ---
# ```{r fig-mpg-wt, fig.cap="MPG vs Weight", fig.width=6, fig.height=4}
# library(ggplot2)
# ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(cyl))) +
# geom_point(size = 3) +
# geom_smooth(method = "lm", se = FALSE) +
# labs(x = "Weight (1000 lbs)", y = "Miles per gallon",
# colour = "Cylinders") +
# theme_minimal()
# ```
# As shown in Figure \@ref(fig:fig-mpg-wt), heavier cars have lower MPG.
# --- Equation (LaTeX math in text) ---
# The linear model is:
# $$\hat{y} = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \varepsilon$$
# --- Inline code ---
# The dataset contains `r nrow(mtcars)` observations
# across `r ncol(mtcars)` variables.
# The heaviest car weighs `r max(mtcars$wt)` thousand pounds.