s-news
[Top] [All Lists]

Re: [S] Error in function...

To: Luca Boni <boni@ermes.cba.unige.it>
Subject: Re: [S] Error in function...
From: Prof Brian D Ripley <ripley@stats.ox.ac.uk>
Date: Sat, 24 Jul 1999 21:22:05 +0100 (BST)
Cc: s-news@wubios.wustl.edu
In-reply-to: <3.0.6.32.19990724172907.009db100@ermes.cba.unige.it>
Sender: owner-s-news@wubios.wustl.edu
On Sat, 24 Jul 1999, Luca Boni wrote:

> 
> I wrote this simple function:
> 
> "ct.planning"<-
> function( tev, tref, mc, hratio, accrual, tmin, nc.c = 0, nc.i = 0 )
> {
>       totp<-uniroot( function(x) e.ev( tref, mc, hratio, x, accrual, tmin, 
> nc.c,
> nc.i ) - tev,         c(1,50000))$root
>       totp
> }
> 
> (Note that e.ev is another function that I wrote and it works well).
> When I make a call to ct.planning, an error occurs:
> Error in f(1): Object "mc" not found

This is a scope problem: you need to understand the S scope rules, and how
to deal with them (allocate mc to frame 1).  In short, mc is not visible
to any routine called by uniroot: another solution is to do something
like

>       totp<-uniroot( function(x,mc) e.ev( tref, mc, hratio, x, accrual,
[...]), mc=mc)

See ?uniroot for an explanation.

In short: S does not have hierarchical scope.

-- 
Brian D. Ripley,                  ripley@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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>