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 should ignore NA when using function as replacement #164

Merged
merged 11 commits into from Dec 28, 2017

Conversation

@yutannihilation
Copy link
Member

commented May 12, 2017

I like str_replace() with function replacement. It's a very cool feature.

One thing, currently, the code bellow returns a vector contains NA:

text <- c("A1", "B1", "CC")
pattern <- "[A-Z][0-9]"

stringr::str_replace(text, pattern, tolower)
#> [1] "a1" "b1" NA

But, I feel this is a bit counter-intuitive and not consistent with the results of similar functions:

stringr::str_replace_all(text, pattern, tolower)
#> [1] "a1" "b1" "CC"

stringr::str_replace(text, pattern, "x")
#> [1] "x"  "x"  "CC"

This PR will

  1. add omit_na option to str_sub(), which stri_sub<-() got in response to gagolews/stringi#199 and I'm trying to extend in gagolews/stringi#268.
  2. use it in str_replace() to ignore NA and keep the original.

Note that, test will pass only after gagolews/stringi#268 will be merged.

@yutannihilation yutannihilation changed the title str_replace should ignore NA when using function as replatement str_replace should ignore NA when using function as replacement May 12, 2017

@hadley

This comment has been minimized.

Copy link
Member

commented Nov 14, 2017

Is this now in CRAN stringi?

@gagolews

This comment has been minimized.

Copy link
Contributor

commented Nov 14, 2017

Should be anytime soon, the new release of stringi got stuck in CRAN/incoming since Friday

@hadley

This comment has been minimized.

Copy link
Member

commented Nov 14, 2017

Great - @yutannihilation can you please include the stringi version requirement in the DESCRIPTION?

@yutannihilation yutannihilation force-pushed the yutannihilation:omit-na branch from 3a688b4 to d0c8abf Nov 14, 2017

@yutannihilation

This comment has been minimized.

Copy link
Member Author

commented Nov 14, 2017

Thanks. I updated the version requirement to 1.1.6.

https://github.com/gagolews/stringi/releases/tag/v1.1.6.

R/sub.r Outdated
@@ -15,6 +15,8 @@
#' matrix to `start`.
#'
#' Negative values count backwards from the last character.
#' @param omit_na single logical value; if \code{TRUE}, missing values in any of the arguments

This comment has been minimized.

Copy link
@hadley

hadley Nov 14, 2017

Member

Can you please switch to markdown formatting?

R/sub.r Outdated
@@ -15,6 +15,8 @@
#' matrix to `start`.
#'
#' Negative values count backwards from the last character.
#' @param omit_na single logical value; if \code{TRUE}, missing values in any of the arguments
#' provided will result in an unchanged input; replacement function only

This comment has been minimized.

Copy link
@hadley

hadley Nov 14, 2017

Member

Missing indent here

This comment has been minimized.

Copy link
@hadley

hadley Nov 14, 2017

Member

I think you can remove the bit about replacement function only. It would be useful to have a bit more motivation about when this is useful

@yutannihilation

This comment has been minimized.

Copy link
Member Author

commented Nov 14, 2017

Should I also add stringi to Remotes? Or can I just wait the release of 1.1.6?

@hadley

This comment has been minimized.

Copy link
Member

commented Nov 15, 2017

Add to remotes just so we can test it, and I'll remove before releasing stringr.

@yutannihilation

This comment has been minimized.

Copy link
Member Author

commented Nov 15, 2017

Thanks, I've added Remotes. Travis passes but AppVeyor fails to install stringi by this error. Needs to investigate later.

/bin/sh: c:/Rtools/mingw_32/bin/g++: No such file or directory
(https://ci.appveyor.com/project/tidyverse/stringr/build/1.0.22#L166)

@hadley

This comment has been minimized.

Copy link
Member

commented Nov 15, 2017

Looks good. Can you please add a bullet to NEWS? It should briefly describe the change (starting with name of the function), and crediting yourself with (@yourname, #issuenumber).

@yutannihilation

This comment has been minimized.

Copy link
Member Author

commented Nov 15, 2017

Thanks! I've done. I think this is ready to merge now.

@yutannihilation

This comment has been minimized.

Copy link
Member Author

commented Nov 18, 2017

Congratulations for the release of stringi 1.1.6! 🎉

@yutannihilation

This comment has been minimized.

Copy link
Member Author

commented Nov 23, 2017

@hadley Hi, do I need to make further improvements on this PR? (Just curious, I know it's a bit too late since the next version is already submitted to CRAN)

@hadley

This comment has been minimized.

Copy link
Member

commented Nov 28, 2017

Unless it gets rejected by CRAN, it'll need to go in the next version. Sorry for forgetting about it 😢

@yutannihilation

This comment has been minimized.

Copy link
Member Author

commented Nov 28, 2017

No problem. Sorry for taking some time to get this PR ready. I will try to do better next time 😉

@hadley

This comment has been minimized.

Copy link
Member

commented Dec 28, 2017

Luckily for you, stringr did get rejected, so I can merge this 😄

@hadley hadley merged commit d6ef15d into tidyverse:master Dec 28, 2017

4 checks passed

codecov/patch 100% of diff hit (target 100%)
Details
codecov/project 100% (+0%) compared to aac9f53
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@yutannihilation

This comment has been minimized.

Copy link
Member Author

commented Dec 28, 2017

Wow, I appreciate the luck. Thanks!

@yutannihilation yutannihilation deleted the yutannihilation:omit-na branch Dec 28, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.