I'm trying to build a recursive set of functions that take a set of arguments, change some of the arguments and recursively call the same (or different) function. For example here's a stupid recursive counting function that prints back all integers from x to 0 (and ignores arguments y and z) cnt <- function(x, y, z) { stopifnot(is.numeric(x)) print (x) recursionFUN <- match.call() recursionFUN$x <- x - 1 if (x <= 0) { invisible(TRUE) } else { eval(recursionFUN) } } My problem is that sometimes I want to set one of the arguments to NULL. But trying to set one of the match.call() arguments to NULL causes it to be ignored (since the match.call() output is coerced into a list). What I'd like is that the match.call() output could be converted into an alist, so that tagged values with no arguments could be handled and passed on to the next function call without being ignored. However, I haven't been able to figure out how to construct this alist without knowing ahead of time what all of the function arguments are and typing them in explicitly: e.g. alist(x = 5, y = 2, z = 3). Re-assigning and then re-evaluating output of the match.call() function might not be the way to go, but I am not sure, and would appreciate any comments on the best way to set function arguments to NULL before evaluating. Thanks, Robert Robert McGehee Geode Capital Management, LLC 53 State Street, 5th Floor | Boston, MA | 02109 Tel: 617/392-8396 Fax:617/476-6389 mailto:robert.mcgehee@geodecapital.com This e-mail, and any attachments hereto, are intended for us...{{dropped}}
I'm not sure what you're doing but did you try recursionFUN$x <- list(NULL) ? -roger McGehee, Robert wrote:> I'm trying to build a recursive set of functions that take a set of > arguments, change some of the arguments and recursively call the same > (or different) function. > > For example here's a stupid recursive counting function that prints back > all integers from x to 0 (and ignores arguments y and z) > > cnt <- function(x, y, z) { > stopifnot(is.numeric(x)) > print (x) > recursionFUN <- match.call() > recursionFUN$x <- x - 1 > if (x <= 0) { > invisible(TRUE) > } else { > eval(recursionFUN) > } > } > > My problem is that sometimes I want to set one of the arguments to NULL. > But trying to set one of the match.call() arguments to NULL causes it to > be ignored (since the match.call() output is coerced into a list). What > I'd like is that the match.call() output could be converted into an > alist, so that tagged values with no arguments could be handled and > passed on to the next function call without being ignored. > > However, I haven't been able to figure out how to construct this alist > without knowing ahead of time what all of the function arguments are and > typing them in explicitly: > e.g. alist(x = 5, y = 2, z = 3). > > Re-assigning and then re-evaluating output of the match.call() function > might not be the way to go, but I am not sure, and would appreciate any > comments on the best way to set function arguments to NULL before > evaluating. > > Thanks, > Robert > > Robert McGehee > Geode Capital Management, LLC > 53 State Street, 5th Floor | Boston, MA | 02109 > Tel: 617/392-8396 Fax:617/476-6389 > mailto:robert.mcgehee@geodecapital.com > > > > This e-mail, and any attachments hereto, are intended for us...{{dropped}} > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
But passing list(NULL) or NULL into a function may have entirely different results. For instance,> paste("Hello world", NULL)[1] "Hello world "> paste("Hello world", list(NULL))[1] "Hello world NULL" Simply put, I'd like to look at a function's call (i.e. using match.call()), and replace one of its arguments with NULL and re-evaluate it. I was hoping for something generic, such that I didn't have to know all of a function's arguments ahead of time (i.e. I could just replace the one I wanted without even knowing the names or values of the other arguments), but this may be easier said than done. -----Original Message----- From: Roger D. Peng [mailto:rpeng@jhsph.edu] Sent: Monday, February 28, 2005 4:58 PM To: McGehee, Robert Cc: r-devel@stat.math.ethz.ch Subject: Re: [Rd] Changing function arguments to NULL I'm not sure what you're doing but did you try recursionFUN$x <- list(NULL) ? -roger McGehee, Robert wrote:> I'm trying to build a recursive set of functions that take a set of > arguments, change some of the arguments and recursively call the same > (or different) function. > > For example here's a stupid recursive counting function that printsback> all integers from x to 0 (and ignores arguments y and z) > > cnt <- function(x, y, z) { > stopifnot(is.numeric(x)) > print (x) > recursionFUN <- match.call() > recursionFUN$x <- x - 1 > if (x <= 0) { > invisible(TRUE) > } else { > eval(recursionFUN) > } > } > > My problem is that sometimes I want to set one of the arguments toNULL.> But trying to set one of the match.call() arguments to NULL causes itto> be ignored (since the match.call() output is coerced into a list).What> I'd like is that the match.call() output could be converted into an > alist, so that tagged values with no arguments could be handled and > passed on to the next function call without being ignored. > > However, I haven't been able to figure out how to construct this alist > without knowing ahead of time what all of the function arguments areand> typing them in explicitly: > e.g. alist(x = 5, y = 2, z = 3). > > Re-assigning and then re-evaluating output of the match.call()function> might not be the way to go, but I am not sure, and would appreciateany> comments on the best way to set function arguments to NULL before > evaluating. > > Thanks, > Robert > > Robert McGehee > Geode Capital Management, LLC > 53 State Street, 5th Floor | Boston, MA | 02109 > Tel: 617/392-8396 Fax:617/476-6389 > mailto:robert.mcgehee@geodecapital.com > > > > This e-mail, and any attachments hereto, are intended forus...{{dropped}}> > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >