1% Generated by roxygen2: do not edit by hand 2% Please edit documentation in R/char.R 3\name{char} 4\alias{char} 5\alias{set_char_opts} 6\title{Format a character vector in a tibble} 7\usage{ 8char( 9 x, 10 ..., 11 min_chars = NULL, 12 shorten = c("back", "front", "mid", "abbreviate") 13) 14 15set_char_opts( 16 x, 17 ..., 18 min_chars = NULL, 19 shorten = c("back", "front", "mid", "abbreviate") 20) 21} 22\arguments{ 23\item{x}{A character vector.} 24 25\item{...}{These dots are for future extensions and must be empty.} 26 27\item{min_chars}{The minimum width to allocate to this column, defaults to 15. 28The \code{"pillar.min_chars"} option is not consulted.} 29 30\item{shorten}{How to abbreviate the data if necessary: 31\itemize{ 32\item \code{"back"} (default): add an ellipsis at the end 33\item \code{"front"}: add an ellipsis at the front 34\item \code{"mid"}: add an ellipsis in the middle 35\item \code{"abbreviate"}: use \code{\link[=abbreviate]{abbreviate()}} 36}} 37} 38\description{ 39\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}} 40 41Constructs a character vector that can be formatted with predefined minimum width 42or without width restrictions, and where the abbreviation style can be configured. 43 44The formatting is applied when the vector is printed or formatted, 45and also in a tibble column. 46 47\code{set_char_opts()} adds formatting options to an arbitrary character vector, 48useful for composing with other types. 49} 50\examples{ 51# Display as a vector: 52char(letters[1:3]) 53\dontshow{if ({ set.seed(20210331); rlang::is_installed("stringi") }) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} 54# Space constraints: 55rand_strings <- stringi::stri_rand_strings(10, seq(40, 22, by = -2)) 56 57# Plain character vectors get truncated if space is limited: 58data_with_id <- function(id) { 59 tibble::tibble( 60 id, 61 some_number_1 = 1, some_number_2 = 2, some_number_3 = 3, 62 some_number_4 = 4, some_number_5 = 5, some_number_6 = 6, 63 some_number_7 = 7, some_number_8 = 8, some_number_9 = 9 64 ) 65} 66data_with_id(rand_strings) 67 68# Use char() to avoid or control truncation 69data_with_id(char(rand_strings, min_chars = 24)) 70data_with_id(char(rand_strings, min_chars = Inf)) 71data_with_id(char(rand_strings, min_chars = 24, shorten = "mid")) 72 73# Lorem Ipsum, one sentence per row. 74lipsum <- unlist(strsplit(stringi::stri_rand_lipsum(1), "(?<=[.]) +", perl = TRUE)) 75tibble::tibble( 76 back = char(lipsum, shorten = "back"), 77 front = char(lipsum, shorten = "front"), 78 mid = char(lipsum, shorten = "mid") 79) 80tibble::tibble(abbr = char(lipsum, shorten = "abbreviate")) 81\dontshow{\}) # examplesIf} 82} 83\seealso{ 84Other vector classes: 85\code{\link{num}()} 86} 87\concept{vector classes} 88