[R] Overwriting a procedure

Duncan Murdoch murdoch.duncan at gmail.com
Thu Sep 4 03:20:08 CEST 2014


On 03/09/2014, 8:58 PM, MacQueen, Don wrote:
> If you have more than one version of fixx(), then the one that is used is
> the one that comes first in the search path. The search path is revealed
> by the search() function. So if you can learn to control the search path,
> then you can control which version of fixx() is used. That would be my
> initial approach.

This is only partially correct information.  It only applies to uses of
fixx() from the console.  The original poster wanted to replace a
function in a package:  uses within the package will search the package
namespace first, regardless of the search path.

Duncan Murdoch

> 
> As an aside, you can define your first version of fixx() more simply as
> 
>   fixx <- function(x) list(x=x)
> 
> and the second more simply as
> 
> fixx <- function(x) {
>  x[,6]<-x[,5]^2/10
>  list(x=x)
> }
> 
> Using return() is completely unnecessary (but of course ok if preferred as
> a matter of programming style).
> 
> 
> Of course, this all assumes you truly need two different functions with
> the same name. I would think that is unlikely, but since there¹s no
> indication of what determines which one should be used, I can¹t say.
> However, there must be some criterion that determines that the second
> version should be used, so perhaps
> 
> 
> fixx <- function(x, criterion) {
>   ## criterion must be a logical value of length 1
>   if (criterion) x[,6]<-x[,5]^2/10
>   list(x=x)
> }
> 
> would work.
> 
>



More information about the R-help mailing list