[R] Overwriting a procedure

Steven Yen syen04 at gmail.com
Wed Sep 24 16:19:25 CEST 2014


Thank you all. Various ideas led to a simple solution.
I include an argument in the calling function, i.e., v.transform, 
default being FALSE. Function fixzx is removed entirely from the 
package, which is OK by default. The function is called only when 
v.transform=TRUE:

if (v.transform){
   v<-fixzx(z1,x1); z1<-v$z; x1<-v$x
}

Then, when I do need to transform variable(s), I include function 
fixx(x) in the main program (which I had meant to do anyway as 
transformations are problem specific):

fixzx <- function(z,x){
z[,4]=z[,3]^2/10; x[,4]=z[,4];
x[,18]<-x[,17]^2/10
result <- list(z=z,x=x)
return(result)
}

This works out nicely. The fact that a function fixx is not needed 
when v.transform=FALSE is very convenient. Other programming 
languages, e.g., Gauss, do not allow the function to be missing when 
v.transform=FALSE. Thank you all.

At 04:41 PM 9/2/2014, Greg Snow wrote:
>A perhaps better approach would be to have the functions that
>currently call fixx accept an argument of a function to use.  It could
>default to fixx, but if the caller passed in a new function it would
>use that function instead.
>
>If you really want to overwrite a function inside of a package
>namespace then look at the assignInNamespace function in the utils
>package (but note the warning in the description on the help page).
>
>On Tue, Sep 2, 2014 at 12:45 PM, Steven Yen <syen04 at gmail.com> wrote:
> > Is there a way to over-write a procedure (subroutine)?
> >
> > I include a default procedure fixx in a list of procedures which are
> > compiled into a package. By default, the procedure deliver the data matrix
> > x.
> >
> > fixx <- function(x){
> > result <- list(x=x)
> > return(result)
> > }
> >
> > In some applications, I have transformations (such as squared 
> terms) in some
> > column(s) in x. So I include the following procedure in the mail (calling)
> > program, hoping to over-write the default procedure under the same name in
> > the package (which is the way other languages works, e.g., Gauss):
> >
> > fixx <- function(x){
> > x[,6]<-x[,5]^2/10
> > result <- list(x=x)
> > return(result)
> > }
> >
> > This does not seem to work. The procedure in the main (calling) program
> > seems to get ignored. Any idea? Thanks.
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
>
>
>--
>Gregory (Greg) L. Snow Ph.D.
>538280 at gmail.com



More information about the R-help mailing list