This document fixes common knit-time errors you encountered: - there is no package called 'kableExtra' → tries to install/load kableExtra but falls back to plain tables if it isn’t available. - object of type 'closure' is not subsettable in chunks like [myBargraph] → uses safe column selection (strings) and avoids accidental name clashes with functions. - cannot coerce class "function" to a data.frame → ensures df is truly a data frame and not shadowed by a function name.
Replace this with your real data import if you have one. The goal here is to ensure we always have a proper data.frame.
# Example data; replace with your actual import, e.g. readr::read_csv("yourfile.csv")
df <- data.frame(
Gender = factor(sample(c("Female", "Male"), 100, replace = TRUE)),
Age = sample(18:65, 100, replace = TRUE),
Height = round(rnorm(100, mean = 170, sd = 10), 1),
stringsAsFactors = FALSE
)
# Ensure df is actually a data.frame (not a function or other object)
if (!is.data.frame(df)) {
df <- as.data.frame(df, stringsAsFactors = FALSE)
}
# Explicitly avoid clashing with any function named `df`
if (is.function(df)) {
stop("`df` is a function in your environment. Please rename your data frame to `data_df` or another name.")
}
# Preview (safe table)
tbl_fun <- function(x, caption = "") {
if (exists("kbl", where = asNamespace("kableExtra"), inherits = FALSE) && has_kx) {
kableExtra::kbl(x, caption = caption) |>
kableExtra::kable_styling(full_width = FALSE, bootstrap_options = c("striped", "hover"))
} else {
knitr::kable(x, caption = paste0(caption, if (!has_kx) " (plain table; install kableExtra for styling)"))
}
}
tbl_fun(utils::head(df), "Data preview")
| Gender | Age | Height |
|---|---|---|
| Male | 56 | 175.8 |
| Male | 18 | 149.1 |
| Female | 62 | 151.6 |
| Male | 35 | 167.0 |
| Female | 19 | 170.0 |
| Female | 55 | 180.1 |
# Which columns are factors?
sapply(df, is.factor)
## Gender Age Height
## TRUE FALSE FALSE
# Convert character columns to factors (if desired)
to_factor <- names(Filter(is.character, df))
if (length(to_factor)) {
df[to_factor] <- lapply(df[to_factor], factor)
}
sapply(df, is.factor)
## Gender Age Height
## TRUE FALSE FALSE
The errors you saw typically happen when a symbol like Age or df accidentally refers to a function or when a variable name isn’t passed as a string.
# Robust helper that accepts either a bare column name or a character string
my_bargraph <- function(data, var) {
stopifnot(is.data.frame(data))
# Convert bare symbols (e.g., my_bargraph(df, Age)) to a string
if (missing(var)) stop("Please supply the column to plot, e.g., my_bargraph(df, 'Age')")
var_quo <- substitute(var)
if (!is.character(var)) {
var <- deparse(var_quo)
}
# Guard: column must exist
if (!var %in% names(data)) {
stop("Column not found: ", var, ". Available columns are: ", paste(names(data), collapse = ", "))
}
# Prevent accidental function masking, e.g., if a function is also named like the column
if (is.function(get0(var, mode = "function", inherits = TRUE))) {
warning("There is a function named '", var, "' in scope. Proceeding with column named '", var, "'.")
}
# If the variable is numeric, we cut into bins; if factor/character, we use counts
library(ggplot2)
if (is.numeric(data[[var]])) {
ggplot(data, aes(x = .data[[var]])) +
geom_histogram(bins = 30) +
labs(x = var, y = "Count", title = paste("Histogram of", var))
} else {
data |>
dplyr::count(.data[[var]], name = "n") |>
ggplot(aes(x = .data[[var]], y = n)) +
geom_col() +
labs(x = var, y = "Count", title = paste("Bar chart of", var)) +
theme(axis.text.x = element_text(angle = 30, hjust = 1))
}
}
col Properly)In earlier versions, col <- "Age" was placed in a chunk with eval = FALSE, so it never ran. Here we ensure it runs.
col <- "Age"
col
## [1] "Age"
my_bargraph(df, col)
summary_tbl <- data.frame(
Variable = names(df),
Class = sapply(df, function(x) class(x)[1]),
Missing = sapply(df, function(x) sum(is.na(x))),
Example = sapply(df, function(x) paste(utils::head(unique(x), 3), collapse = ", ")),
stringsAsFactors = FALSE
)
tbl_fun(summary_tbl, "Descriptive summary")
| Variable | Class | Missing | Example | |
|---|---|---|---|---|
| Gender | Gender | factor | 0 | Male, Female |
| Age | Age | integer | 0 | 56, 18, 62 |
| Height | Height | numeric | 0 | 175.8, 149.1, 151.6 |
If you knit to PDF, ensure a LaTeX distribution (e.g., TinyTeX) is installed:
# install.packages("tinytex")
# tinytex::install_tinytex()For stability, this document defaults to HTML output with a graceful fallback for tables if kableExtra is missing.