Skip to content
Back to formatted view

Raw Message

Message-ID: <82f987ca-f11d-c6e1-db39-71a8415195e2@yahoo.fr>
Date: 2017-02-02T14:30:57Z
From: Marc Girondot
Subject: Function that works within a package and not when copied in global environment. Why?

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


-- 

__________________________________________________________
Marc Girondot, Pr

Laboratoire Ecologie, Syst?matique et Evolution
Equipe de Conservation des Populations et des Communaut?s
CNRS, AgroParisTech et Universit? Paris-Sud 11 , UMR 8079
B?timent 362
91405 Orsay Cedex, France

Tel:  33 1 (0)1.69.15.72.30   Fax: 33 1 (0)1.69.15.73.53
e-mail: marc.girondot at u-psud.fr
Web: http://www.ese.u-psud.fr/epc/conservation/Marc.html
Skype: girondot