From jmc at research.bell-labs.com  Thu Jul 15 23:43:21 2004
From: jmc at research.bell-labs.com (John Chambers)
Date: Thu Jul 15 23:43:41 2004
Subject: [Rd] tracing something in a namespace (PR#7091)
References: <20040714193307.15F58108C7@slim.kubism.ku.dk>
Message-ID: <40F6FA79.596C87E7@research.bell-labs.com>

rmh@temple.edu wrote:
> 
> # Your mailer is set to "none" (default on Windows),
> # hence we cannot send the bug report directly from R.
> # Please copy the bug report (after finishing it) to
> # your favorite email program and send it to
> #
> #       r-bugs@r-project.org
> #
> ######################################################
> 
> > x <- rnorm(10)
> > y <- 1:10
> > xyplot(y ~ x)
> > trace(lattice:::print.trellis, exit=recover)
> [1] "print.trellis"
> Warning message:
> Assigning over the binding of symbol "print.trellis" in environment/package "lattice"
> in: .assignOverBinding(what, newFun, whereF)
> > xyplot(y ~ x)
> > untrace(lattice:::print.trellis)
> Error in untrace(lattice:::print.trellis) :
>         Argument what should be the name of a function
> >
> 
> Something isn't right.  I see three possibilities.
> 
> a. tracing something in a namespace is prohibited and I didn't get an error,
> instead I got a warning.

No, it's not prohibited, though it's not encouraged.

Not to be counted on as being guaranteed to exist forever.

Use the alternative under b.

> 
> b. tracing in a namespace is acceptable, but the trace didn't work.

I believe your problem has to do with S3 methods in namespaces. 
Effectively, the first dispatch of an S3 method in a namespace caches
the method found (at least that is my impression).

If I take your example and move the trace() call before the first call
to xyplot() (and therefore before the first dispatch of print.trellis)
it works for me.

But, then you cannot redefine the method, so untrace() wouldn't work in
any case.

DON'T USE trace() ON S3 METHODS IN NAMESPACES.

Instead, a mechanism that is slightly more cumbersome but perfectly
legal (and no warnings) is to copy the S3 method to the global
environment and trace it there.

As long as the method is being dispatched, not called directly inside
the namespace, then the global copy will be used.

For your example:

R> print.trellis <- lattice:::print.trellis
R> trace(print.trellis, exit = recover)
R> x <- rnorm(10)
R> y <- 1:10
R> xyplot(y ~ x)
Tracing print.trellis(yy$value) on exit 

Enter a frame number, or 0 to exit   
1:source("~/testR/latticeTrace.R", echo = T) 
2:print(yy$value) 
3:print.trellis(yy$value) 
Selection: 0
R> untrace(print.trellis)
R> xyplot(y ~ x)
R> 

> 
> c. untrace doesn't recognize lattice:::print.trellis as the name of a function,
> even though trace did.

True, more or less.  We'll likely fix that for the next release, but the
recommended technique is as above for S3 methods.


> 
> --please do not edit the information below--
> 
> Version:
>  platform = i386-pc-mingw32
>  arch = i386
>  os = mingw32
>  system = i386, mingw32
>  status =
>  major = 1
>  minor = 9.1
>  year = 2004
>  month = 06
>  day = 21
>  language = R
> 
> Windows XP Home Edition (build 2600) Service Pack 1.0
> 
> Search Path:
>  .GlobalEnv, file:c:/HOME/rmh/hh/splus.library/HH/.RData, package:methods, package:stats,
> package:utils, package:multcomp, package:mvtnorm, package:abind, package:graphics,
> package:lattice, package:grid, Autoloads, package:base
> 
> ______________________________________________
> R-devel@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-devel

-- 
John M. Chambers                  jmc@bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc


[Rd] tracing something in a namespace (PR#7091)
John Chambers