tidyverse / magrittr

Improve the readability of R code with the pipe
https://magrittr.tidyverse.org
Other
961 stars 157 forks source link

Feature request: regex operator aliases #154

Closed rossholmberg closed 6 years ago

rossholmberg commented 7 years ago

Regex operators don't play nice with pipes, so I end up doing things like this a lot:

strings %>% sub( "\\.csv", "", . )

Using the . to specify the incoming object.

I think it would be nice to wrap a few of these functions, namely sub, gsub, grep and grepl. I think prefixing each with str_ would work well to differentiate. I'd also personally set the default replacement parameter in sub and gsub to "", since I think string removal is a common use for those functions. That turns the above into:

strings %>% str_sub( "\\.csv" )

Here are the wrapper functions I'm suggesting. I'd be happy to make a pull request if you like the idea, just give me the go-ahead if so:

str_sub <- function( x,
                     pattern,
                     replacement = "",
                     ignore.case = FALSE,
                     perl = FALSE,
                     fixed = FALSE,
                     useBytes = FALSE ) {
  sub( pattern = pattern,
       replacement = replacement,
       x = x,
       ignore.case = ignore.case,
       perl = perl,
       fixed = fixed,
       useBytes = useBytes )
}

str_gsub <- function( x,
                      pattern,
                      replacement = "",
                      ignore.case = FALSE,
                      perl = FALSE,
                      fixed = FALSE,
                      useBytes = FALSE ) {
  gsub( pattern = pattern,
        replacement = replacement,
        x = x,
        ignore.case = ignore.case,
        perl = perl,
        fixed = fixed,
        useBytes = useBytes )
}

str_grepl <- function( x,
                       pattern,
                       ignore.case = FALSE,
                       perl = FALSE,
                       fixed = FALSE,
                       useBytes = FALSE ) {
  grepl( pattern = pattern,
         x = x,
         ignore.case = ignore.case,
         perl = perl,
         fixed = fixed,
         useBytes = useBytes )
}

str_grep <- function( x,
                      pattern,
                      ignore.case = FALSE,
                      perl = FALSE,
                      fixed = FALSE,
                      useBytes = FALSE ) {
  grep( pattern = pattern,
        x = x,
        ignore.case = ignore.case,
        perl = perl,
        fixed = fixed,
        useBytes = useBytes )
}
stillmatic commented 7 years ago

The stringr package is an alternative to the base regex functions, and plays nice with magrittr piping.

rossholmberg commented 7 years ago

@stillmatic true. stringr is a very specialist package though, going beyond base functionality, and therefore deserving of its own package. I'm just hoping for some basic base function wrappers here, rather than redesigning the functions, as stringr does.

hadley commented 6 years ago

This is a great idea for a new package, but I don't think these functions belong in magrittr.