[R] changing (core) function argument defaults?
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Thu Nov 18 11:28:40 CET 2004
Berton Gunter <gunter.berton at gene.com> writes:
> Yes, I think for all practical purposes it (usually?) is. Here's an example.
> Suppose I wish to change the default "constant" argument of mad from 1.48 to
> 2. Then
>
> > z<-formals(mad)
> > z$constant<-2
> > mad<-as.function(c(z,body(mad)))
Actually, formals(mad)$constant<-2 will do the same. In either case,
you need to be careful to note that the environment of the function is
changed to the current frame. So if you redefine median(), this will
be picked up by your mad(), but not by stats::mad().
> If you now attach the workspace/environment containing this newly defined
> mad function to the search list before the stats package (which contains the
> original mad()) you have effectively changed the default argument without
> changing the function.
>
> I hope experts will let us know when this can't be done (perhaps with
> .internal functions or non-exported functions in namespaces, though it isn't
> clear to me that one couldn't manually export them and do this here, too).
I'd expect that it works whenever the function has default arguments
to modify (.Primitive functions do not). The namespace mechanism only
ensures that you don't overwrite the original, and that packages
expecting to use the original can continue to do so.
> Of course, all the usual warnings about masking existing functions apply.
Yes, R is not preventing users from shooting themselves in the foot,
nor preventing package writers from shooting users in the foot (as a
recent query involving [.factor showed).
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list