[R] Bug in formals<-

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Sep 27 08:23:58 CEST 2006


Frank E Harrell Jr <f.harrell at vanderbilt.edu> writes:

> Deepayan Sarkar wrote:
> > On 9/26/06, Frank E Harrell Jr <f.harrell at vanderbilt.edu> wrote:
> >> I think this is new since a previous version of R:
> >>
> >>  > h <- function(x, trantab) trantab[x]
> >>  > w <- 6:4
> >>  > names(w) <- c('cat','dog','giraffe')
> >>  > w
> >>      cat     dog giraffe
> >>        6       5       4
> >>  >
> >>  > formals(h) <- list(x=numeric(0), trantab=w)
> >>  > h
> >> function (x = numeric(0), trantab = c(6, 5, 4))
> >> trantab[x]
> >>
> >> You can see that the names have been dropped from trantab's default
> >> values.
> > 
> > Are you sure? I get
> > 
> >> formals(h)
> > $x
> > numeric(0)
> > 
> > $trantab
> >    cat     dog giraffe
> >      6       5       4
> > 
> >> h(1)
> > cat
> >  6
> > 
> > R version 2.4.0 beta (2006-09-21 r39463)
> > x86_64-unknown-linux-gnu
> > 
> > -Deepayan
> 
> Deepayan -
> 
> You are correct.  h('cat') is 6 as intended.  I just looked at the 
> function definition - the names attribute doesn't show for some reason. 
>   I was expecting function(..., trantab=c(cat=6, ..).

It's a generic deparsing issue. Also:

> substitute(f(w),list(w=w))
f(c(6, 5, 4))

We should probably fix it at some point. Notice though that you can't
parse your way into such situations, only get there by computing on
the language.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list