Re: [R] Bug in formals<-

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed 27 Sep 2006 - 01:46:14 GMT

This seems to be related to using c to define transtab. If we use list in place of c then it displays ok:

> h <- function(x, trantab) transtab[x]
> formals(h) <- list(x = numeric(0), transtab = c(cat = 6, dog = 5))
> print(h) # bad display

function (x = numeric(0), transtab = c(6, 5)) transtab[x]
> h("cat") # runs ok

cat
  6
> formals(h) <- list(x = numeric(0), transtab = list(cat = 6, dog = 5))
> print(h) # now display is ok

function (x = numeric(0), transtab = list(cat = 6, dog = 5)) transtab[x]
> h("cat") # runs ok

$cat
[1] 6

On 9/26/06, Frank E Harrell Jr <f.harrell@vanderbilt.edu> wrote:
> Deepayan Sarkar wrote:
> > On 9/26/06, Frank E Harrell Jr <f.harrell@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, ..).
>

> Thanks
>

> Frank
>

> >
> >
> >> Version 2.3.1 (2006-06-01)
> >> i486-pc-linux-gnu
> >>
> >> attached base packages:
> >> [1] "grid" "methods" "stats" "graphics" "grDevices" "utils"
> >> [7] "datasets" "base"

> >>
> >> other attached packages:
> >> lattice acepack Hmisc
> >> "0.13-10" "1.3-2.2" "3.0-12"
> >>
> >
>

> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed Sep 27 11:51:49 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 27 Sep 2006 - 02:30:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.