Skip to content
Prev 1378 / 63421 Next

scoping

Paul Gilbert wrote:
As Martyn suggested, if you just want a number it is better to use
someting that just deals with numbers. If you do want the user to be
able to respond with an arbitrary S expression, then this is one case
where eval does make sense, but it should be used with caution. The
default behavior of eval is to use evaluate in the calling environment,
Enter an expression: x
[1] 3

This is probably not what you want. In S the official way to get eval
to use the global environment/frame is eval(expr,F) (this is what the
try.S example in the help page does). In R you seem to need
eval(expr,0). I *think* this works in S too, but I'm always a bit
confused about frames 0 and 1. There may be another approach that
works cleanly in both.

If you have need for this kind of thing in several places it would be
a good idea to abstact it out as a utility function,
prompt.for.expression say, carefully get that function right, and then
only access this facility through that function.

luke
Message-ID: <9806261801.AA29578@nokomis.stat.umn.edu>
In-Reply-To: <98Jun26.120612edt.13454@mailgate.bank-banque-canada.ca> from "Paul Gilbert" at Jun 26, 98 11:41:12 am