Good practice for exporting regexp in package

regex

#1

Hi,

I have some regular expressions that i think maybe some other people would like to use (for research in bioinformatics), these regexp are very much linked to the package I'm writing but saving them as data (http://r-pkgs.had.co.nz/data.html) doesn't seem best, they would be hidden when you look at the code. I don't know what would be right thing to do or what would the good practice for exporting regexps.


#2

The variables storing the regular expressions can be created and loaded just like the functions in your package. For example, let's say you made a file named is_foobar.R and stored it in the package's R/ subdirectory:

# is_foobar.R

#' @export
my_regexes <- list(
  foo = "\\bfoo\\b",
  bar = "(bar){3}"
)

#' @export
is_foobar <- function(x) {
  grepl(my_regexes[["foo"]], x) & grepl(my_regexes[["bar"]], x)
}

Now both my_regexes and is_foobar are available if users load your package.

library(mypackage)

is_foobar(c("foo", "bar", "foo barbarbar"))
# [1] FALSE FALSE  TRUE
my_regexes
# $`foo`
# [1] "\\bfoo\\b"
#
# $bar
# [1] "(bar){3}"

If a user just wants the regular expressions but nothing else, they can use :: notation:

test::my_regexes
# $`foo`
# [1] "\\bfoo\\b"
#
# $bar
# [1] "(bar){3}"

#3

oh, yes, I see, thank you