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.
Hi, Trying to change the way functions inside packages work is almost certainly not what you want to do. It's possible but usually there is an easier way. Your example is so simplified that I suspect it doesn't capture your actual needs very well, but in the case of your example you can simply do pre <- function(x){ x[,6]<-x[,5]^2/10 return(x) } fixx(pre(x)) Best, Ista On Tue, Sep 2, 2014 at 2: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.
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
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. 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. -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 9/2/14, 11:45 AM, "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.