modify the imported version of a function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

modify the imported version of a function

Benjamin Tyner
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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: modify the imported version of a function

Benjamin Tyner
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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.