Re: [Rd] change function's formals default values

From: Thomas Lumley <>
Date: Tue 04 Apr 2006 - 17:00:23 GMT

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

   function(x){ a+b*x}



## 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);


       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


Thomas Lumley			Assoc. Professor, Biostatistics	University of Washington, Seattle

______________________________________________ mailing list Received on Wed Apr 05 04:40:01 2006

This archive was generated by hypermail 2.1.8 : Tue 04 Apr 2006 - 20:16:53 GMT