Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behavior of str_remove when called with NA_character_ pattern #546

Closed
davidciani opened this issue Apr 11, 2024 · 1 comment · Fixed by #566
Closed

Behavior of str_remove when called with NA_character_ pattern #546

davidciani opened this issue Apr 11, 2024 · 1 comment · Fixed by #566
Labels
bug an unexpected problem or unintended behavior tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day

Comments

@davidciani
Copy link

When called with NA as a pattern, str_remove fails loudly about a type mismatch. When called with NA_character_ as a pattern, it silently returns NA. Is this the expected behavior? If so it doesn't seem to be documented anywhere. Personally, I was expecting that str_remove would have returned the input string unmodified in this scenario (as there is nothing specified to remove), or alternatively failed with an error message.

library(stringr)

str_remove("test", NA)
#> Error in `str_replace()`:
#> ! `pattern` must be a string, not `NA`.

str_remove("test", NA_character_)
#> [1] NA

# What lead to discovery of this, though str_c is behaving as expected returning NA_character_

string_to_remove = NA # a blank cell in a data frame

str_remove_all("test", str_c("\\b",string_to_remove,"\\b"))
#> [1] NA

Created on 2024-04-11 with reprex v2.1.0

@hadley
Copy link
Member

hadley commented Jul 15, 2024

Hmmm, looks like type.character() should probably error on this. But we'd need to double check what the docs for stringr says just to make sure there's no reason for anyone to be relying on this behavkour.

@hadley hadley added bug an unexpected problem or unintended behavior tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day labels Jul 15, 2024
nash-delcamp-slp added a commit to nash-delcamp-slp/stringr that referenced this issue Aug 15, 2024
@hadley hadley closed this as completed in b511603 Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants