From afinley at stat.umn.edu  Tue Apr  4 19:05:10 2006
From: afinley at stat.umn.edu (Andrew Finley)
Date: Tue, 04 Apr 2006 12:05:10 -0500
Subject: [Rd] change function's formals default values
In-Reply-To: <Pine.LNX.4.64.0604040941280.21419@homer23.u.washington.edu>
References: <1144114759.15762.3.camel@populus.fr.umn.edu>
	<Pine.LNX.4.64.0604040714090.26620@homer22.u.washington.edu>
	<1144161750.18246.7.camel@populus.fr.umn.edu>
	<Pine.LNX.4.64.0604040941280.21419@homer23.u.washington.edu>
Message-ID: <1144170311.18556.10.camel@populus.fr.umn.edu>

Your example is perfectly clear. I'll take your suggestion.
Thanks a lot for your time-
Andrew

On Tue, 2006-04-04 at 10:00 -0700, Thomas Lumley wrote:
> On Tue, 4 Apr 2006, Andrew Finley wrote:
> 
> > Hi Thomas,
> >
> > Thanks for the note. I'm not sure about a lot of things. Setting the
> > formal defaults then calling the function seem straight forward.  I just
> > assumed it would be like setting list values, or the C equivalent of
> > calling formals(fn)<-list(a=1, b=3).
> >
> > Following your suggestion, I can get the names of the parameters used in
> > this user defined function, but am not sure how to do what you are
> > suggesting. For instance, how do I call the function after I use setVar.
> > Could you point me to an example?
> >
> 
> ##In R
> f<-function(a,b){
>    function(x){ a+b*x}
> }
> 
> f23<-f(a=2,b=3)
> 
> dyn.load("cfun.so")
> .Call("cfun",fun=f23,rho=environment(f23),x=c(1,2,3,4),newa=1,newb=2)
> 
> ## in cfun.c
> 
> #include "Rinternals.h"
> 
> SEXP cfun(SEXP fun, SEXP rho, SEXP x, SEXP newa, SEXP newb){
>        SEXP ans, funcall;
> 
>        setVar(install("a"), newa, rho);
>        setVar(install("b"), newb, rho);
> 
>        PROTECT(funcall=lang2(fun,x));
>        ans=eval(funcall,rho);
> 
>        UNPROTECT(1);
>        return ans;
> 
> }
> 
> ##
> 
> This isn't necessarily any simpler, but it seems more natural to modify 
> variables than default arguments, and also it is probably safer since you 
> don't have to worry about the value of NAMED() and whether to copy the 
> arguments.
> 
> This is based on the more complicated code in cox_Rcallback.c in the 
> survival package, for penalized Cox regression
> https://svn.r-project.org/R-packages/trunk/survival/src/cox_Rcallback.c
> 
>  	-thomas
> 
> Thomas Lumley			Assoc. Professor, Biostatistics
> tlumley at u.washington.edu	University of Washington, Seattle
-- 
Research Fellow
Department of Forest Resources
University of Minnesota
Office: 305 Green Hall
Phone: (612) 624-1714
Fax: (612) 625-5212
web: http://blue.fr.umn.edu



[Rd] change function's formals default values
Andrew Finley