2. You need to review how namespaces work. From the "Writing R
extensions" manual:
"The namespace controls the search strategy for variables used by
**functions in the package**. If not found locally, R searches the
package namespace first, then the imports, then the base namespace and
then the normal search path."
So if vectorize.args() is among the package functions, it will be
found by package functions but not by those you write unless
specifically qualified by :: or ::: depending on whether it is
exported.
Cheers,
Bert
Bert Gunter
"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Thu, Feb 2, 2017 at 6:30 AM, Marc Girondot via R-help
<r-help at r-project.org> wrote:
Dear experts,
In the package nlWaldTest, there is an hidden function : .smartsub
I can use it, for example:
getFromNamespace(".smartsub", ns="nlWaldTest")(pat="x", repl="b" ,
x="essai(b[1], b[2], x[1])")
[1] "essai(b[1], b[2], b[1])"
Now I try to create this function in my global environment:
smartsub <- getFromNamespace(".smartsub", ns="nlWaldTest")
It works also:
smartsub(pat="x", repl="b" , x="essai(b[1], b[2], x[1])")
[1] "essai(b[1], b[2], b[1])"
But if I create the function manually:
smartsub2 <- function (pat, repl, x)
{
args <- lapply(as.list(match.call())[-1L], eval, parent.frame())
names <- if (is.null(names(args)))
character(length(args))
else names(args)
dovec <- names %in% vectorize.args
do.call("mapply", c(FUN = FUN, args[dovec], MoreArgs =
list(args[!dovec]),
SIMPLIFY = SIMPLIFY, USE.NAMES = USE.NAMES))
}
smartsub2(pat="x", repl="b" , x="essai(b[1], b[2], x[1])")
Error in names %in% vectorize.args : objet 'vectorize.args' introuvable
It fails because vectorize.args is unknown
Indeed smartsub2 is different from smartsub.
identical(smartsub, smartsub2)
[1] FALSE
1/ Why are they different? They are just a copy of each other.
2/ Second question, vectorize.args is indeed not defined before to be used
in the function. Why no error is produced in original function?
Thanks a lot
Marc
--