georgi.boshnakov at manchester.ac.uk
2007-Jun-26 17:15 UTC
[Rd] slight anomaly in formals<- ? (PR#9758)
Hi, The R input/output after the following paragraph is from a session with version.string R version 2.4.0 Patched (2006-11-23 r39958). The last element, x$c, of x has no value and after the assignment of x to the formals of f, x$c does not become a formal argument of f. The second assignment does the job. It seems that x$c actually becomes the body of f after the first assignment. There is no problem if the body of f is not NULL (this probably explains why the second time the assignment to the formals of f works as expected.> x <- alist(a=,b=,c=) > x$b <- 4 > f <- function(x) NULL > ffunction(x) NULL> formals(f) <- x > ffunction (a, b = 4)> formals(f) <- x > ffunction (a, b = 4, c)> body(f)-- Dr Georgi Boshnakov tel: (+44) (0)161 306 3684 School of Mathematics fax: (+44) (0)161 306 3669 The University of Manchester email: Georgi.Boshnakov at manchester.ac.uk Sackville Street Manchester M60 1QD UK
ripley at stats.ox.ac.uk
2007-Jun-27 06:00 UTC
[Rd] slight anomaly in formals<- ? (PR#9758)
On Tue, 26 Jun 2007, georgi.boshnakov at manchester.ac.uk wrote:> Hi, > > The R input/output after the following paragraph is from a session with > version.string R version 2.4.0 Patched (2006-11-23 r39958).Which is not recent, but this is unchanged since. The problem is the definition:> `formals<-`function (fun, envir = environment(fun), value) as.function(c(value, body(fun)), envir) <environment: namespace:base> and> c(x, NULL)$a $b [1] 4 $c I've fixed this for 2.6.0 (it is not critical enough for 2.5.1 due tomorrow). If you need the defn for your version, it is `formals<-` <- function (fun, envir = environment(fun), value) { bd <- body(fun) as.function(c(value, if(is.null(bd)) list(bd) else bd), envir) }> > The last element, x$c, of x has no value and after the assignment of x > to the formals of f, x$c does not become a formal argument of f. The > second assignment does the job. It seems that x$c actually becomes the > body of f after the first assignment. There is no problem if the body of > f is not NULL (this probably explains why the second time the assignment > to the formals of f works as expected. > >> x <- alist(a=,b=,c=) >> x$b <- 4Or just x <- alist(a=,b=4,c=)>> f <- function(x) NULL >> f > function(x) NULL >> formals(f) <- x >> f > function (a, b = 4) > >> formals(f) <- x >> f > function (a, b = 4, c) > >> body(f) > > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595