当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R tibble char 设置字符向量格式


[Experimental]

构造一个字符向量,可以使用预定义的最小宽度或没有宽度限制进行格式化,并且可以在其中配置缩写样式。

当打印或格式化矢量时,以及在 tibble 列中,会应用该格式。

set_char_opts() 将格式化选项添加到任意字符向量,对于与其他类型组合非常有用。

用法

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

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

参数

x

字符向量。

...

这些点用于将来的扩展,并且必须为空。

min_chars

分配给该列的最小宽度,默认为 15。不参考"pillar.min_chars" option

shorten

必要时如何缩写数据:

  • "back"(默认):在末尾添加省略号

  • "front" : 在前面添加省略号

  • "mid" : 在中间添加省略号

  • "abbreviate" :使用abbreviate()

也可以看看

其他向量类:num()

例子

# 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(
    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 × 10
#>    id      some_…¹ some_…² some_…³ some_…⁴ some_…⁵ some_…⁶ some_…⁷ some_…⁸
#>    <chr>     <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1 WJSsxf…       1       2       3       4       5       6       7       8
#>  2 HQOlqj…       1       2       3       4       5       6       7       8
#>  3 GR8hYm…       1       2       3       4       5       6       7       8
#>  4 dO0k5C…       1       2       3       4       5       6       7       8
#>  5 3yKhjZ…       1       2       3       4       5       6       7       8
#>  6 uyWa9T…       1       2       3       4       5       6       7       8
#>  7 UaiZMc…       1       2       3       4       5       6       7       8
#>  8 z2qYwl…       1       2       3       4       5       6       7       8
#>  9 ek4O7h…       1       2       3       4       5       6       7       8
#> 10 YCRHBt…       1       2       3       4       5       6       7       8
#> # … with 1 more variable: some_number_9 <dbl>, and abbreviated variable
#> #   names ¹some_number_1, ²some_number_2, ³some_number_3, ⁴some_number_4,
#> #   ⁵some_number_5, ⁶some_number_6, ⁷some_number_7, ⁸some_number_8

# Use char() to avoid or control truncation
data_with_id(char(rand_strings, min_chars = 24))
#> # A tibble: 10 × 10
#>    id                              some_…¹ some_…² some_…³ some_…⁴ some_…⁵
#>    <char>                            <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1 WJSsxf5RPiDjFRy7iVoadHgnz4uveI…       1       2       3       4       5
#>  2 HQOlqjZavreUgr3GXLoDX6mpkxihE2…       1       2       3       4       5
#>  3 GR8hYmPYrwJG9UsJm9A47KvwLTo2FQ…       1       2       3       4       5
#>  4 dO0k5CLEp9McpsCkmBt5wkYOioyg9C…       1       2       3       4       5
#>  5 3yKhjZZbegjEYOJm1HIxNkWvYx8Ozl…       1       2       3       4       5
#>  6 uyWa9TGFPzGkVAZsJlCaIHvalJsvMC        1       2       3       4       5
#>  7 UaiZMcOCjm7Lh2Gthq6NgOGsJbR2          1       2       3       4       5
#>  8 z2qYwlo7fldemmrE772uCYLrI6            1       2       3       4       5
#>  9 ek4O7hFxIud6K9YsvhQTB6Oe              1       2       3       4       5
#> 10 YCRHBt1kj2i9JnNgyaa5MB                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>, and abbreviated variable
#> #   names ¹some_number_1, ²some_number_2, ³some_number_3, ⁴some_number_4,
#> #   ⁵some_number_5
data_with_id(char(rand_strings, min_chars = Inf))
#> # A tibble: 10 × 10
#>    id                                       some_number_1 some_n…¹ some_…²
#>    <char>                                           <dbl>    <dbl>   <dbl>
#>  1 WJSsxf5RPiDjFRy7iVoadHgnz4uveIpMlA7Qaqjz             1        2       3
#>  2 HQOlqjZavreUgr3GXLoDX6mpkxihE20qyTAB45               1        2       3
#>  3 GR8hYmPYrwJG9UsJm9A47KvwLTo2FQEkuqMT                 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>, and abbreviated variable names ¹some_number_2,
#> #   ²some_number_3
data_with_id(char(rand_strings, min_chars = 24, shorten = "mid"))
#> # A tibble: 10 × 10
#>    id                              some_…¹ some_…² some_…³ some_…⁴ some_…⁵
#>    <char>                            <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1 WJSsxf5RPiDjFRy…4uveIpMlA7Qaqjz       1       2       3       4       5
#>  2 HQOlqjZavreUgr3…pkxihE20qyTAB45       1       2       3       4       5
#>  3 GR8hYmPYrwJG9Us…KvwLTo2FQEkuqMT       1       2       3       4       5
#>  4 dO0k5CLEp9McpsC…5wkYOioyg9COiuR       1       2       3       4       5
#>  5 3yKhjZZbegjEYOJ…HIxNkWvYx8Ozl85       1       2       3       4       5
#>  6 uyWa9TGFPzGkVAZsJlCaIHvalJsvMC        1       2       3       4       5
#>  7 UaiZMcOCjm7Lh2Gthq6NgOGsJbR2          1       2       3       4       5
#>  8 z2qYwlo7fldemmrE772uCYLrI6            1       2       3       4       5
#>  9 ek4O7hFxIud6K9YsvhQTB6Oe              1       2       3       4       5
#> 10 YCRHBt1kj2i9JnNgyaa5MB                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>, and abbreviated variable
#> #   names ¹some_number_1, ²some_number_2, ³some_number_3, ⁴some_number_4,
#> #   ⁵some_number_5

# Lorem Ipsum, one sentence per row.
lipsum <- unlist(strsplit(stringi::stri_rand_lipsum(1), "(?<=[.]) +", perl = TRUE))
tibble(
  back = char(lipsum, shorten = "back"),
  front = char(lipsum, shorten = "front"),
  mid = char(lipsum, shorten = "mid")
)
#> # A tibble: 10 × 3
#>    back                                                        front mid  
#>    <char>                                                      <cha> <cha>
#>  1 Lorem ipsum dolor sit amet, sit et auctor lacinia etiam co… …iis. Lo…s.
#>  2 Laoreet in nec diam ut suspendisse adipiscing suspendisse … …pis. La…s.
#>  3 Eleifend eleifend, metus nunc turpis.                       …pis. El…s.
#>  4 Augue massa lobortis faucibus velit efficitur ac faucibus.  …bus. Au…s.
#>  5 Elit egestas egestas sit amet ut conubia faucibus facilisi. …isi. El…i.
#>  6 Tempor urna nisi feugiat senectus.                          …tus. Te…s.
#>  7 Nunc in pretium dui ac.                                     … ac. Nu…c.
#>  8 Elit semper eu.                                             … eu. El…u.
#>  9 Sapien congue, dictum massa sed posuere sagittis.           …tis. Sa…s.
#> 10 Interdum ante aenean, nisi dapibus lacinia nulla laoreet, … …bus. In…s.
tibble(abbr = char(lipsum, shorten = "abbreviate"))
#> # A tibble: 10 × 1
#>    abbr                                                                   
#>    <char>                                                                 
#>  1 Loremipsumdolorsitamet,sitetauctorlacinetiamcommodo!Tincidntd,nslvtscs.
#>  2 Laoreetinnecdiamutsuspendissadipiscingsuspendissconubtortorrissdnctrps.
#>  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,nisidapibuslacinianulllaoreet,felisultriciesdapibus.
源代码:R/pillar.R

相关用法


注:本文由纯净天空筛选整理自Kirill Müller等大神的英文原创作品 Format a character vector。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。