chr.trunc: Truncate a Character Vector to a Maximum Width

chr.truncR Documentation

Truncate a Character Vector to a Maximum Width

Description

This function truncates a character vector, so that the number of characters of each element of the character vector is always less than or equal to the width specified in the argument width.

Usage

chr.trunc(x, width, side = c("right", "left", "center"), ellipsis = "...",
          check = TRUE)

Arguments

x

a character vector or factor. Note that factors are converted into a character vector.

width

a numeric value indicating the maximum width of the character strings in the vector. Note that the default setting switches to ".." when width = 3, "." when width = 2, and "" when width = 1.

side

a character string indicating the location of the ellipsis, i.e. "right" (default) for the right side, "left" for the left side, and "center" for center of the character strings in the vector

ellipsis

a character string indicating the content of the ellipsis, i.e., "..." by default.

check

logical: if TRUE (default), argument specification is checked.

Value

Returns a truncated character vector.

Note

This function was adapted from the str_trunc() function in the stringr package by Hadley Wickham (2023).

Author(s)

Takuya Yanagida

References

Wickham H (2023). stringr: Simple, Consistent Wrappers for Common String Operations. R package version 1.5.1, https://CRAN.R-project.org/package=stringr

See Also

chr.color, chr.grep, chr.grepl, chr.gsub, chr.omit, chr.trim

Examples

# Example 1: Truncate at the right side with a max. of 10 characters
chr.trunc(row.names(mtcars), width = 10)

# Example 2: Truncate at the left side with a max. of 10 characters
chr.trunc(row.names(mtcars), width = 10, side = "left")

# Example 3: Truncate without ellipses
chr.trunc(row.names(mtcars), width = 10, ellipsis = "")

misty documentation built on April 4, 2025, 3:49 a.m.

Related to chr.trunc in misty...