[R] modify the imported version of a function
Benjamin Tyner
btyner at gmail.com
Mon Dec 19 23:54:04 CET 2016
Figured it out...in case it is useful to others:
> library(lint)
> lint.enclos <- parent.env(asNamespace("lint"))
> stopifnot(all(c("perl", "regex") %in% ls(lint.enclos)))
> lint.enclos$perl
function (pattern)
{
message("perl is deprecated. Please use regexp instead")
regex(pattern)
}
<environment: namespace:stringr>
> unlockBinding("perl", lint.enclos)
> lint.enclos$perl <- lint.enclos$regex
> lint.enclos$perl
function (pattern, ignore_case = FALSE, multiline = FALSE, comments
= FALSE,
dotall = FALSE, ...)
{
options <- stri_opts_regex(case_insensitive = ignore_case,
multiline = multiline, comments = comments, dotall = dotall,
...)
structure(pattern, options = options, class = c("regex",
"pattern", "character"))
}
<environment: namespace:stringr>
On 12/16/2016 06:06 PM, Benjamin Tyner wrote:
> Hi
>
> I saw on the assignInNamespace help page, that it changes "the copy in
> the namespace, but not any copies already exported from the namespace,
> in particular an object of that name in the package (if already
> attached) and any copies already imported into other namespaces."
>
> So now I'm wondering, whether there is a way to modify such copies
> already imported into other namespaces? For a concrete example,
> consider the lint package (now archived on CRAN) which includes a
> function check_pattern, which calls stringr:::perl, which has been
> deprecated in newer versions:
>
> > stringr:::perl
> function (pattern)
> {
> message("perl is deprecated. Please use regexp instead")
> regex(pattern)
> }
> <environment: namespace:stringr>
>
> so, what if I wanted to directly replace stringr:::perl with
> stringr:::regex, in such a way that lint::check_pattern sees the
> replacement? Is there a way, preferably without having to attach
> stringr to the search path?
>
> (I'm mostly just interested in learning about how namespace imports
> actually work; of course in this toy example one could use
> suppressMessages to hide that deprecation message).
>
> Regards
> Ben
More information about the R-help
mailing list