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

str_replace() doesn't accept a named vector pattern #499

Closed
moodymudskipper opened this issue Feb 23, 2023 · 2 comments
Closed

str_replace() doesn't accept a named vector pattern #499

moodymudskipper opened this issue Feb 23, 2023 · 2 comments

Comments

@moodymudskipper
Copy link

str_replace("abc", c(a="d", c = "e"))
#> Error in check_lengths(string, pattern, replacement) : 
#>  argument "replacement" is missing, with no default

I can understand why, since it should replace only one time, but I expected it would do the first possible replacement (either first in the pattern vector or first in position, I feel the common use case would be that only one would match, at least it was mine).

The doc mentions in the pattern arg description:

To perform multiple replacements in each element of string, pass supply a named vector (c(pattern1 = replacement1)).

But it doesn't mention that it applies only to str_replace_all()

I would expect the doc to be more precise and the output to be:

str_replace("abc", c(a="d", c = "e"))
#> [1] "dbc"

# or something like
str_replace("abc", c(a="d", c = "e"))
#> Error in str_replace(string, pattern, replacement) : 
#> x `pattern` must be a string
#> i you provided a named vector, do you need `str_replace_all()` ?
@twest820
Copy link

+1, just hit this too. Also, there's a typo in the docs ("pass supply a named vector").

@hadley
Copy link
Member

hadley commented Aug 4, 2023

I don't plan to add support for this, but I can at least make it more clear in the docs.

@hadley hadley closed this as completed in 16f14c7 Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants