Skip to content

str_trunc can't handle NA's in input #162

@ClaytonJY

Description

@ClaytonJY

Passing a single NA into str_trunc leaves it untouched, as expected

NA_character_ %>% str_trunc(10)

But a vector with one or more NA's throws an error

c("foo bar baz", NA) %>% str_trunc(10)
c(NA_character_, NA_character_) %>% str_trunc(10)

Error in string[too_long] <- switch(side, right = str_c(str_sub(string[too_long], :
NAs are not allowed in subscripted assignments

which happens without piping, as well.

It seems like the expected behaviour would be to truncate the "real" strings, and leave the NA's untouched, rather than throw an error.

Session Info

> devtools::session_info()
Session info ----------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.0 (2017-04-21)
 system   x86_64, linux-gnu           
 ui       RStudio (1.0.143)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/New_York            
 date     2017-05-09                  

Packages --------------------------------------------------------------------------------------------------------------------------------
 package  * version date       source        
 devtools   1.12.0  2016-12-05 CRAN (R 3.4.0)
 digest     0.6.12  2017-01-27 CRAN (R 3.4.0)
 magrittr * 1.5     2014-11-22 CRAN (R 3.4.0)
 memoise    1.1.0   2017-04-21 CRAN (R 3.4.0)
 stringi    1.1.5   2017-04-07 CRAN (R 3.4.0)
 stringr  * 1.2.0   2017-02-18 CRAN (R 3.4.0)
 withr      1.0.2   2016-06-20 CRAN (R 3.4.0)

(Not the dev version, but git blame suggests this function hasn't changed in two years)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions