[Rd] model.frame() call from inside a function (PR#3671)

jerome at hivnet.ubc.ca jerome at hivnet.ubc.ca
Thu Aug 7 01:41:50 MEST 2003


R version: 1.7.1
OS: Red Hat Linux 7.2

Hi all,

The formula object in model.frame() is not retrieved properly when 
model.frame() is called from within a function and the "subset" argument 
is supplied.

foo <- function(formula,data,subset=NULL)
{
  cat("\n*****Does formula[-3] == ~y ?**** TRUE *****\n")
  print(formula[-3] == ~y)

  cat("\n*****Result of model.frame() using formula[-3]**** FAIL *****\n")
  print(try(model.frame(formula[-3],data=data,subset=subset)))

  cat("\n*****Result of model.frame() using ~y**** WORKS *****\n")
  print(try(model.frame(~y,data=data,subset=subset)))
}
dat <- data.frame(y=c(5,25))
foo(y~1,dat)

Curiously, if the "subset" argument is removed from the call to 
model.frame(), then the execution is successful in both cases.

In ?model.frame, one can read:
     Variables in the formula, `subset' and in `...' are looked for
     first in `data' and then in the environment of `formula': see the
     help for `formula()' for further details.

However, replacing the line
    subset <- eval(substitute(subset), data, env)
by
    subset <- eval(substitute(subset), data, environment())
in model.frame.default() fixes this problem. I don't know if this 
correction would create more problems in other cases. Perhaps there is a 
better fix.

Sincerely,
Jerome Asselin

-- 

Jerome Asselin (Jérôme), Statistical Analyst
British Columbia Centre for Excellence in HIV/AIDS
St. Paul's Hospital, 608 - 1081 Burrard Street
Vancouver, British Columbia, CANADA V6Z 1Y6
Email: jerome at hivnet.ubc.ca
Phone: 604 806-9112   Fax: 604 806-9044



More information about the R-devel mailing list