[Experimental]

Constructs a character vector that can be formatted with predefined minimum width or without width restrictions, and where the abbreviation style can be configured.

The formatting is applied when the vector is printed or formatted, and also in a tibble column.

set_char_opts() adds formatting options to an arbitrary character vector, useful for composing with other types.

char(
  x,
  ...,
  min_chars = NULL,
  shorten = c("back", "front", "mid", "abbreviate")
)

set_char_opts(
  x,
  ...,
  min_chars = NULL,
  shorten = c("back", "front", "mid", "abbreviate")
)

Arguments

x

A character vector.

...

These dots are for future extensions and must be empty.

min_chars

The minimum width to allocate to this column, defaults to 15. The "pillar.min_chars" option is not consulted.

shorten

How to abbreviate the data if necessary:

  • "back" (default): add an ellipsis at the end

  • "front": add an ellipsis at the front

  • "mid": add an ellipsis in the middle

  • "abbreviate": use abbreviate()

See also

Other vector classes: num()

Examples

# Display as a vector:
char(letters[1:3])
#> <pillar_char[3]>
#> [1] a b c
# Space constraints:
rand_strings <- stringi::stri_rand_strings(10, seq(40, 22, by = -2))

# Plain character vectors get truncated if space is limited:
data_with_id <- function(id) {
  tibble::tibble(
    id,
    some_number_1 = 1, some_number_2 = 2, some_number_3 = 3,
    some_number_4 = 4, some_number_5 = 5, some_number_6 = 6,
    some_number_7 = 7, some_number_8 = 8, some_number_9 = 9
  )
}
data_with_id(rand_strings)
#> # A tibble: 10 x 10
#>    id      some_number_1 some_number_2 some_number_3 some_number_4 some_number_5
#>    <chr>           <dbl>         <dbl>         <dbl>         <dbl>         <dbl>
#>  1 WJSsxf…             1             2             3             4             5
#>  2 HQOlqj…             1             2             3             4             5
#>  3 GR8hYm…             1             2             3             4             5
#>  4 dO0k5C…             1             2             3             4             5
#>  5 3yKhjZ…             1             2             3             4             5
#>  6 uyWa9T…             1             2             3             4             5
#>  7 UaiZMc…             1             2             3             4             5
#>  8 z2qYwl…             1             2             3             4             5
#>  9 ek4O7h…             1             2             3             4             5
#> 10 YCRHBt…             1             2             3             4             5
#> # … with 4 more variables: some_number_6 <dbl>, some_number_7 <dbl>,
#> #   some_number_8 <dbl>, some_number_9 <dbl>

# Use char() to avoid or control truncation
data_with_id(char(rand_strings, min_chars = 24))
#> # A tibble: 10 x 10
#>    id                                  some_number_1 some_number_2 some_number_3
#>    <char>                                      <dbl>         <dbl>         <dbl>
#>  1 WJSsxf5RPiDjFRy7iVoadHgnz4uveIpMlA…             1             2             3
#>  2 HQOlqjZavreUgr3GXLoDX6mpkxihE20qyT…             1             2             3
#>  3 GR8hYmPYrwJG9UsJm9A47KvwLTo2FQEkuq…             1             2             3
#>  4 dO0k5CLEp9McpsCkmBt5wkYOioyg9COiuR              1             2             3
#>  5 3yKhjZZbegjEYOJm1HIxNkWvYx8Ozl85                1             2             3
#>  6 uyWa9TGFPzGkVAZsJlCaIHvalJsvMC                  1             2             3
#>  7 UaiZMcOCjm7Lh2Gthq6NgOGsJbR2                    1             2             3
#>  8 z2qYwlo7fldemmrE772uCYLrI6                      1             2             3
#>  9 ek4O7hFxIud6K9YsvhQTB6Oe                        1             2             3
#> 10 YCRHBt1kj2i9JnNgyaa5MB                          1             2             3
#> # … with 6 more variables: some_number_4 <dbl>, some_number_5 <dbl>,
#> #   some_number_6 <dbl>, some_number_7 <dbl>, some_number_8 <dbl>,
#> #   some_number_9 <dbl>
data_with_id(char(rand_strings, min_chars = Inf))
#> # A tibble: 10 x 10
#>    id                                       some_number_1 some_number_2
#>    <char>                                           <dbl>         <dbl>
#>  1 WJSsxf5RPiDjFRy7iVoadHgnz4uveIpMlA7Qaqjz             1             2
#>  2 HQOlqjZavreUgr3GXLoDX6mpkxihE20qyTAB45               1             2
#>  3 GR8hYmPYrwJG9UsJm9A47KvwLTo2FQEkuqMT                 1             2
#>  4 dO0k5CLEp9McpsCkmBt5wkYOioyg9COiuR                   1             2
#>  5 3yKhjZZbegjEYOJm1HIxNkWvYx8Ozl85                     1             2
#>  6 uyWa9TGFPzGkVAZsJlCaIHvalJsvMC                       1             2
#>  7 UaiZMcOCjm7Lh2Gthq6NgOGsJbR2                         1             2
#>  8 z2qYwlo7fldemmrE772uCYLrI6                           1             2
#>  9 ek4O7hFxIud6K9YsvhQTB6Oe                             1             2
#> 10 YCRHBt1kj2i9JnNgyaa5MB                               1             2
#> # … with 7 more variables: some_number_3 <dbl>, some_number_4 <dbl>,
#> #   some_number_5 <dbl>, some_number_6 <dbl>, some_number_7 <dbl>,
#> #   some_number_8 <dbl>, some_number_9 <dbl>
data_with_id(char(rand_strings, min_chars = 24, shorten = "mid"))
#> # A tibble: 10 x 10
#>    id                                  some_number_1 some_number_2 some_number_3
#>    <char>                                      <dbl>         <dbl>         <dbl>
#>  1 WJSsxf5RPiDjFRy7i…nz4uveIpMlA7Qaqjz             1             2             3
#>  2 HQOlqjZavreUgr3GX…6mpkxihE20qyTAB45             1             2             3
#>  3 GR8hYmPYrwJG9UsJm…47KvwLTo2FQEkuqMT             1             2             3
#>  4 dO0k5CLEp9McpsCkmBt5wkYOioyg9COiuR              1             2             3
#>  5 3yKhjZZbegjEYOJm1HIxNkWvYx8Ozl85                1             2             3
#>  6 uyWa9TGFPzGkVAZsJlCaIHvalJsvMC                  1             2             3
#>  7 UaiZMcOCjm7Lh2Gthq6NgOGsJbR2                    1             2             3
#>  8 z2qYwlo7fldemmrE772uCYLrI6                      1             2             3
#>  9 ek4O7hFxIud6K9YsvhQTB6Oe                        1             2             3
#> 10 YCRHBt1kj2i9JnNgyaa5MB                          1             2             3
#> # … with 6 more variables: some_number_4 <dbl>, some_number_5 <dbl>,
#> #   some_number_6 <dbl>, some_number_7 <dbl>, some_number_8 <dbl>,
#> #   some_number_9 <dbl>

# Lorem Ipsum, one sentence per row.
lipsum <- unlist(strsplit(stringi::stri_rand_lipsum(1), "(?<=[.]) +", perl = TRUE))
tibble::tibble(
  back = char(lipsum, shorten = "back"),
  front = char(lipsum, shorten = "front"),
  mid   = char(lipsum, shorten = "mid")
)
#> # A tibble: 10 x 3
#>    back                      front                     mid                      
#>    <char>                    <char>                    <char>                   
#>  1 Lorem ipsum dolor sit am… … dui, nisl vitae sociis. Lorem ipsum …itae sociis.
#>  2 Laoreet in nec diam ut s… …rtor risus donec turpis. Laoreet in n…onec turpis.
#>  3 Eleifend eleifend, metus… …fend, metus nunc turpis. Eleifend ele…nunc turpis.
#>  4 Augue massa lobortis fau… …t efficitur ac faucibus. Augue massa …ac faucibus.
#>  5 Elit egestas egestas sit… …nubia faucibus facilisi. Elit egestas…us facilisi.
#>  6 Tempor urna nisi feugiat… …a nisi feugiat senectus. Tempor urna …at senectus.
#>  7 Nunc in pretium dui ac.   Nunc in pretium dui ac.   Nunc in pretium dui ac.  
#>  8 Elit semper eu.           Elit semper eu.           Elit semper eu.          
#>  9 Sapien congue, dictum ma… …sa sed posuere sagittis. Sapien congu…re sagittis.
#> 10 Interdum ante aenean, ni… …felis ultricies dapibus. Interdum ant…ies dapibus.
tibble::tibble(abbr = char(lipsum, shorten = "abbreviate"))
#> # A tibble: 10 x 1
#>    abbr                                                                         
#>    <char>                                                                       
#>  1 Loremipsumdolorsitamet,sitetauctorlacinetiamcommodo!Tinciduntdui,nislvitsocs.
#>  2 Laoreetinnecdiamutsuspendisseadipiscingsuspendissconubtortorrisusdonecturpis.
#>  3 Eleifend eleifend, metus nunc turpis.                                        
#>  4 Augue massa lobortis faucibus velit efficitur ac faucibus.                   
#>  5 Elit egestas egestas sit amet ut conubia faucibus facilisi.                  
#>  6 Tempor urna nisi feugiat senectus.                                           
#>  7 Nunc in pretium dui ac.                                                      
#>  8 Elit semper eu.                                                              
#>  9 Sapien congue, dictum massa sed posuere sagittis.                            
#> 10 Interdumanteaenean,nisidapibuslacinianullalaoreet,felisultriciesdapibus.