Prints the names of an object to the console, with an option to sort them. Supports custom separators and handles various edge cases.
Arguments
- x
An R object with names attribute (e.g., vector, list, data frame).
- sort
Logical. Whether to sort names using mixed alphanumeric sorting. Defaults to
FALSE
.- sep
Character. Separate names in output. Defaults to newline (
"\n"
).- na_rm
Logical. Whether to removes
NA
values from names before printing. Defaults toTRUE
.- prefix
Character. string to prepend to each name. Defaults to
NULL
.- ...
Additional arguments passed to
base::cat()
.
Examples
# Basic usage
vec <- c(a1 = 1, b = 2, a2 = 3)
cat_names(vec)
#> a1
#> b
#> a2
# Sorted names
cat_names(vec, sort = TRUE)
#> a1
#> a2
#> b
# Custom separator and prefix
cat_names(vec, sep = ", ", prefix = "- ")
#> - a1, - b, - a2
# Handle NA names
vec_na <- c(a = 1, NA, c = 3)
cat_names(vec_na, na_rm = TRUE)
#> a
#>
#> c
cat_names(vec_na, na_rm = TRUE, sort = TRUE)
#>
#> a
#> c
# Example with data frames
cat_names(mtcars)
#> mpg
#> cyl
#> disp
#> hp
#> drat
#> wt
#> qsec
#> vs
#> am
#> gear
#> carb
cat_names(iris)
#> Sepal.Length
#> Sepal.Width
#> Petal.Length
#> Petal.Width
#> Species