stringr icon indicating copy to clipboard operation
stringr copied to clipboard

str_wrap() not splitting at specified width for long chemical name string

Open jzadra opened this issue 1 year ago • 0 comments

This is a weird string, but regardless str_wrap() is failing to split part of it at the specified width:

library(tidyverse)
str_wrap("2-Propenoic acid, 1,1'-[2-[[(1-oxo-2-propen-1-yl)oxy]methyl]-2-[[[[[[1,3,3-trimethyl-5-[[[3-[(1-oxo-2-propen-1-yl)oxy]-2,2-bis[[(1-oxo-2-propen-1-yl)oxy]methyl]propoxy]carbonyl]amino]cyclohexyl]methyl]amino]carbonyl]oxy]methyl]-1,3-propanediyl] ester", width = 50) %>% 
  enframe(name = NULL) %>% 
  separate_rows(value, sep = "\n") %>% 
  mutate(chars = nchar(value))
#> # A tibble: 5 × 2
#>   value                                                                    chars
#>   <chr>                                                                    <int>
#> 1 2-Propenoic acid, 1,1'-[2-[[(1-oxo-2-propen-1-                              46
#> 2 yl)oxy]methyl]-2-[[[[[[1,3,3-trimethyl-5-[[[3-[(1-                          50
#> 3 oxo-2-propen-1-yl)oxy]-2,2-bis[[(1-oxo-2-propen-1-                          50
#> 4 yl)oxy]methyl]propoxy]carbonyl]amino]cyclohexyl]methyl]amino]carbonyl]o…    86
#> 5 propanediyl] ester                                                          18

jzadra avatar Jul 15 '22 16:07 jzadra

You might want to use whitespace_only = FALSE

hadley avatar Aug 12 '22 11:08 hadley