[R] How to get the namespace of a function?

Gabor Grothendieck ggrothendieck at gmail.com
Thu Feb 2 22:05:18 CET 2006


It actually did find index -- in fact, the error message is coming from index.
I forgot that in this case index is a generic which in turn is calling
index.ts and that is what it can't find.  How about one of these:

> library(zoo)
> f <- function(x) eval(substitute(index(x), list(x = x)),
+ envir = as.environment("package:zoo"))
> environment(f) <- NULL
> x <- ts(1:4)
> f(x)
[1] 1 2 3 4

or

> library(zoo)
> f <- function(x) eval.parent(substitute(index(x)))
> environment(f) <- NULL
> x <- ts(1:4)
> f(x)
[1] 1 2 3 4




On 2/2/06, Fernando Saldanha <fsaldan1 at gmail.com> wrote:
> Thanks, Gabor. I tried this without success:
>
> > z <- ts(1:5)
> > f <- function(x) {f1 <- function() get("index", "package:zoo"); f1()(x)}
> > environment(f) <- NULL
> > f(z)
> Error in f1()(x) : no applicable method for "index"
>
> I also tried this, which seemed simpler, with the same outcome:
>
> > z <- ts(1:5)
> > f <- function(x) {f1 <- get("index", "package:zoo"); f1(x)}
> > environment(f) <- NULL
> > f(z)
> Error in f1(x) : no applicable method for "index"
>
> FS
>
>
>
> On 2/2/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> > Try:
> >
> > f <- function() get("index", "package:zoo")
> >
> >
> > On 2/2/06, Fernando Saldanha <fsaldan1 at gmail.com> wrote:
> > > I declared the environment of the function myfun to be NULL as follows:
> > >
> > > environment(myfun) <- NULL
> > >
> > > Later on I called that myfun and got an error message because the
> > > function index() in the zoo package was called inside myfun and was
> > > not visible:
> > >
> > > Error in myfun(args) : couldn't find function "index"
> > >
> > > I tried to use zoo::index() instead of index(), but that did not work.
> > > In fact, zoo::index does not work even in the command line:
> > >
> > > > z<-ts(1:5)
> > > > z
> > > Time Series:
> > > Start = 1
> > > End = 5
> > > Frequency = 1
> > > [1] 1 2 3 4 5
> > > > index(z)
> > > [1] 1 2 3 4 5
> > > > zoo::index(z)
> > > Error in loadNamespace(name) : package 'zoo' does not have a name space
> > >
> > > How can I qualify index() so that it is visible inside the body of myfun?
> > >
> > > Thanks for any suggestions,
> > >
> > > FS
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> > >
> >
>




More information about the R-help mailing list