From ggrothendieck at gmail.com  Thu Nov 11 22:07:36 2010
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Thu, 11 Nov 2010 16:07:36 -0500
Subject: [Rd] Evaluation puzzle
In-Reply-To: <201011112008.oABK8xjk025512@punchbuggy.mayo.edu>
References: <201011112008.oABK8xjk025512@punchbuggy.mayo.edu>
Message-ID: <AANLkTim4PP95Ts=wPBRs1JskyTQwM3WdFrnYgscEzAXe@mail.gmail.com>

On Thu, Nov 11, 2010 at 3:08 PM, Terry Therneau <therneau at mayo.edu> wrote:
> The survexp function can fail when called from another function. ?The "why" of
> this has me baffled, however.
>
> Here is a simple test case, using a very stripped down version of survexp:
>
> survexp.test <- function(formula, data,
> ? ? ? ?weights, subset, na.action, rmap,
> ? ? ? ?times, ?cohort=TRUE, ?conditional=FALSE,
> ? ? ? ?ratetable=survexp.us, scale=1, npoints, se.fit,
> ? ? ? ?model=FALSE, x=FALSE, y=FALSE) {
> ? ?call <- match.call()
> ? ?m <- match.call(expand.dots=FALSE)
>
> ? ?# keep the first element (the call), and the following selected arguments
> ? ?m <- m[c(1, match(c('formula', 'data', 'weights', 'subset', 'na.action'),
> ? ? ? ? ? ? ? ? ? ? ?names(m), nomatch=0))]
> ? ?m[[1]] <- as.name("model.frame")
>
> ? ?# Add in the ratetable variables
> ? ?varlist <- attr(ratetable, 'dimid')
> ? ?tform <- paste(deparse(formula), paste(varlist, collapse='+'), sep='+')

At the above statement you have lost the environment of your formula.

> ? ?m$formula <- tform

Replace this with:

m$formula <- as.formula(tform, environment(formula))



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



[Rd] Evaluation puzzle
Gabor Grothendieck