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

Mark.Bravington at csiro.au Mark.Bravington at csiro.au
Fri Jul 16 04:37:07 CEST 2004


Richard: you might also try 'mtrace' in the 'debug' package, which works fine in the example you give, without any need to globalize 'print.trellis' or similar. You can't specify methods by package e.g. lattice:::print.trellis, but then again you don't need to:

library( lattice)
...
mtrace( print.trellis) # traces all copies, even ones that 'find' doesn't find
xyplot( y ~ x) # up comes the debug window
...
mtrace( print.trellis, F) # removes all traces
xyplot( y~x) # no debug window

It took me quite a while to figure out how to get this to work. Briefly, S3 methods in namespaces seem to exist in either two or three places, depending on whether they are explicitly exported. It is probably unhealthy for there to be any differences between the versions. 'mtrace' finds all copies and changes them all; see 'fun.locator' in 'mvbutils' for more details.

Hope this helps

Mark

BTW I devoutly hope that it never becomes impossible to change things inside namespaces or the "base" environment(e.g. via bindings that cannot be unlocked). It's probably good to make it hard for users to screw up (as is the case now), but not good to prevent interested and determined parties from changing code if they really want to. Losing the ability to trace in namespaces, or to try fixing code in namespaces, would be bad-- there are always going to be *some* bugs even in bits of "core" code. A huge part of the power of R lies in the ability to change bits of it, and locking more away than is absolutely necessary seems to me undesirable. Perhaps I'm over-interpreting John Chambers "don't count on it" comment here-- anyway, I hope so!

*******************************

Mark Bravington
CSIRO (CMIS)
PO Box 1538
Castray Esplanade
Hobart
TAS 7001

phone (61) 3 6232 5118
fax (61) 3 6232 5012
Mark.Bravington at csiro.au 

#-----Original Message-----
#From: r-devel-bounces at stat.math.ethz.ch
#[mailto:r-devel-bounces at stat.math.ethz.ch]On Behalf Of John Chambers
#Sent: Friday, 16 July 2004 7:43 AM
#To: rmh at temple.edu
#Cc: R-bugs at biostat.ku.dk; r-devel at stat.math.ethz.ch
#Subject: Re: [Rd] tracing something in a namespace (PR#7091)
#
#
#rmh at 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 at 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 at stat.math.ethz.ch mailing list
#> https://www.stat.math.ethz.ch/mailman/listinfo/r-devel
#
#-- 
#John M. Chambers                  jmc at 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
#
#______________________________________________
#R-devel at stat.math.ethz.ch mailing list
#https://www.stat.math.ethz.ch/mailman/listinfo/r-devel
#



More information about the R-devel mailing list