From 3b87fc485877b5e609ba595102ebaae7deb98122 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Sat, 13 May 2017 01:12:20 +0900 Subject: [PATCH 1/8] enable to set omit_na=TRUE on stri_sub<- --- R/replace.r | 2 +- R/sub.r | 17 ++++++++++++++--- man/str_sub.Rd | 14 +++++++++++++- tests/testthat/test-replace.r | 4 ++++ tests/testthat/test-sub.r | 9 +++++++++ 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/R/replace.r b/R/replace.r index 07da1192..29b35f01 100644 --- a/R/replace.r +++ b/R/replace.r @@ -168,7 +168,7 @@ str_replace_na <- function(string, replacement = "NA") { str_transform <- function(string, pattern, replacement) { loc <- str_locate(string, pattern) - str_sub(string, loc) <- replacement(str_sub(string, loc)) + str_sub(string, loc, omit_na = TRUE) <- replacement(str_sub(string, loc)) string } str_transform_all <- function(string, pattern, replacement) { diff --git a/R/sub.r b/R/sub.r index 598d6cfc..f4e030cb 100644 --- a/R/sub.r +++ b/R/sub.r @@ -15,6 +15,8 @@ #' matrix to \code{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 #' @param value replacement string #' @return A character vector of substring from \code{start} to \code{end} #' (inclusive). Will be length of longest input argument. @@ -50,6 +52,15 @@ #' str_sub(x, -1, -1) <- "K"; x #' str_sub(x, -2, -2) <- "GHIJ"; x #' str_sub(x, 2, -2) <- ""; x +#' +#' # If you want to keep the original if some argument is NA, +#' # use omit_na = TRUE +#' x1 <- x2 <- x3 <- x4 <- "AAA" +#' str_sub(x1, 1, NA) <- "B" +#' str_sub(x2, 1, 2) <- NA +#' str_sub(x3, 1, NA, omit_na = TRUE) <- "B" +#' str_sub(x4, 1, 2, omit_na = TRUE) <- NA +#' x1; x2; x3; x4 str_sub <- function(string, start = 1L, end = -1L) { if (is.matrix(start)) { stri_sub(string, from = start) @@ -61,11 +72,11 @@ str_sub <- function(string, start = 1L, end = -1L) { #' @export #' @rdname str_sub -"str_sub<-" <- function(string, start = 1L, end = -1L, value) { +"str_sub<-" <- function(string, start = 1L, end = -1L, omit_na = FALSE, value) { if (is.matrix(start)) { - stri_sub(string, from = start) <- value + stri_sub(string, from = start, omit_na = omit_na) <- value } else { - stri_sub(string, from = start, to = end) <- value + stri_sub(string, from = start, to = end, omit_na = omit_na) <- value } string } diff --git a/man/str_sub.Rd b/man/str_sub.Rd index ef488306..79800b9f 100644 --- a/man/str_sub.Rd +++ b/man/str_sub.Rd @@ -7,7 +7,7 @@ \usage{ str_sub(string, start = 1L, end = -1L) -str_sub(string, start = 1L, end = -1L) <- value +str_sub(string, start = 1L, end = -1L, omit_na = FALSE) <- value } \arguments{ \item{string}{input character vector.} @@ -19,6 +19,9 @@ str_sub(string, start = 1L, end = -1L) <- value Negative values count backwards from the last character.} +\item{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} + \item{value}{replacement string} } \value{ @@ -65,6 +68,15 @@ str_sub(x, 1, 1) <- "A"; x str_sub(x, -1, -1) <- "K"; x str_sub(x, -2, -2) <- "GHIJ"; x str_sub(x, 2, -2) <- ""; x + +# If you want to keep the original if some argument is NA, +# use omit_na = TRUE +x1 <- x2 <- x3 <- x4 <- "AAA" +str_sub(x1, 1, NA) <- "B" +str_sub(x2, 1, 2) <- NA +str_sub(x3, 1, NA, omit_na = TRUE) <- "B" +str_sub(x4, 1, 2, omit_na = TRUE) <- NA +x1; x2; x3; x4 } \seealso{ The underlying implementation in \code{\link[stringi]{stri_sub}} diff --git a/tests/testthat/test-replace.r b/tests/testthat/test-replace.r index 077c1400..3fdb328d 100644 --- a/tests/testthat/test-replace.r +++ b/tests/testthat/test-replace.r @@ -61,6 +61,10 @@ test_that("replacement can be different length", { expect_equal(str_replace_all("abc", "a|c", double), "aabcc") }) +test_that("replacement with NA works", { + expect_equal(str_replace("abc", "z", toupper), "abc") +}) + # fix_replacement --------------------------------------------------------- test_that("$ are escaped", { diff --git a/tests/testthat/test-sub.r b/tests/testthat/test-sub.r index 92c87338..110d7d74 100644 --- a/tests/testthat/test-sub.r +++ b/tests/testthat/test-sub.r @@ -69,6 +69,15 @@ test_that("replacement works", { str_sub(x, 2, -2) <- "" expect_equal(x, "AH") +}) +test_that("replacement with NA works", { + x <- "BBCDEF" + str_sub(x, NA) <- "A" + expect_equal(x, NA_character_) + x <- "BBCDEF" + str_sub(x, NA, omit_na = TRUE) <- "A" + str_sub(x, 1, 1, omit_na = TRUE) <- NA + expect_equal(x, "BBCDEF") }) From d0c8abf62e83eb5c160ac2135d6b9cc5aac0dcb6 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Wed, 15 Nov 2017 07:34:00 +0900 Subject: [PATCH 2/8] update the version of stringi to 1.1.6 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 91396f21..bfc2d974 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,7 +19,7 @@ Depends: Imports: glue, magrittr, - stringi (>= 0.4.1) + stringi (>= 1.1.6) Suggests: covr, htmltools, From abd94d02b7305fa225636b09c1c2cdd9357b465e Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Wed, 15 Nov 2017 08:19:47 +0900 Subject: [PATCH 3/8] improve description about omit_na --- R/sub.r | 4 ++-- man/str_sub.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/sub.r b/R/sub.r index 8098913c..d318d35e 100644 --- a/R/sub.r +++ b/R/sub.r @@ -15,8 +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 +#' @param omit_na Single logical value. If `TRUE`, missing values in any of the +#' arguments provided will result in an unchanged input. #' @param value replacement string #' @return A character vector of substring from `start` to `end` #' (inclusive). Will be length of longest input argument. diff --git a/man/str_sub.Rd b/man/str_sub.Rd index 0ecc0627..1a969aa1 100644 --- a/man/str_sub.Rd +++ b/man/str_sub.Rd @@ -19,8 +19,8 @@ matrix to \code{start}. Negative values count backwards from the last character.} -\item{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} +\item{omit_na}{Single logical value. If \code{TRUE}, missing values in any of the +arguments provided will result in an unchanged input.} \item{value}{replacement string} } From 186c46b4b91818f710cd8511de0a3ea0d1853830 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Wed, 15 Nov 2017 10:23:17 +0900 Subject: [PATCH 4/8] add stringi in Remotes for now --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index bfc2d974..f4baff4a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,3 +31,4 @@ VignetteBuilder: knitr LazyData: true Roxygen: list(markdown = TRUE) RoxygenNote: 6.0.1 +Remotes: gagolews/stringi From e31a3a8dbb8c44779ad022c4a269ca6b038a2d1e Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Wed, 15 Nov 2017 10:51:46 +0900 Subject: [PATCH 5/8] add USE_RTOOLS: true --- appveyor.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/appveyor.yml b/appveyor.yml index c6c14384..d6c328f8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -43,3 +43,7 @@ artifacts: - path: '\*_*.zip' name: Bits + +environment: + global: + USE_RTOOLS: true From 5404b6a8589a793fc5bd2c37aaaf9a6bc3e04b99 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Wed, 15 Nov 2017 23:36:14 +0900 Subject: [PATCH 6/8] update NEWS --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index efa4af9f..29863898 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,6 +16,10 @@ `str_replace()` and `str_replace_all()` to remove patterns from strings. (@Shians, #178) +* `str_sub()` gains `omit_na` argument for ignoring `NA`. Accordingly, + `str_replace()` now ignores `NA`s and keeps the original strings. + (@yutannihilation, #164) + # stringr 1.2.0 ## API changes From 546c1bc2c8177adcb2527c87d8116df0998bdfea Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Sat, 18 Nov 2017 10:12:40 +0900 Subject: [PATCH 7/8] Revert "add USE_RTOOLS: true" This reverts commit e31a3a8dbb8c44779ad022c4a269ca6b038a2d1e. --- appveyor.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index d6c328f8..c6c14384 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -43,7 +43,3 @@ artifacts: - path: '\*_*.zip' name: Bits - -environment: - global: - USE_RTOOLS: true From d80c640caa5e4f781149a002836bd801c15a2e37 Mon Sep 17 00:00:00 2001 From: Hiroaki Yutani Date: Sat, 18 Nov 2017 10:13:05 +0900 Subject: [PATCH 8/8] Revert "add stringi in Remotes for now" This reverts commit 186c46b4b91818f710cd8511de0a3ea0d1853830. --- DESCRIPTION | 1 - 1 file changed, 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index f4baff4a..bfc2d974 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,4 +31,3 @@ VignetteBuilder: knitr LazyData: true Roxygen: list(markdown = TRUE) RoxygenNote: 6.0.1 -Remotes: gagolews/stringi