Can Luke Tierney's recent S-new post on scoping be included in the R FAQ? I'm just going through my package and eliminating a few instances of "eval" as I hadn't realized the dangers. I noticed that one way I've used eval is for prompting, as in n <- eval(parse(prompt="Enter the number of singular values to use: ")) I presume this is not too dangerous, but this has never really seemed like the right way to do this. Can someone suggest a better alternative? Paul Gilbert -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 26-Jun-98 Paul Gilbert wrote:>Can Luke Tierney's recent S-new post on scoping be included in the R FAQ?Could this be cross-posted?>I'm just going through my package and eliminating a few instances of "eval" as >I >hadn't realized the dangers. I noticed that one way I've used eval is for >prompting, as in > >n <- eval(parse(prompt="Enter the number of singular values to use: ")) > >I presume this is not too dangerous, but this has never really seemed like the >right way to do this. Can someone suggest a better alternative?If you just want to read in a number, I think it's a lot safer to read the input as character data with "readline" and then coerce it to numeric. I wrote a utility function called "read.and.check" which does this. It also checks that the user input satisfies certain conditions (mode, lower and upper limit for numeric data, or set of allowed values), looping until it gets an acceptable answer. It's in the coda package, with a help page. Martyn -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Paul Gilbert wrote:> > > n <- eval(parse(prompt="Enter the number of singular values to use: ")) >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,> p<-function(x) eval(parse(prompt="Enter an expression: ")) > p(3)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 -- Luke Tierney University of Minnesota Phone: 612-625-7843 School of Statistics Fax: 612-624-8868 206 Church Street email: luke@stat.umn.edu Minneapolis, MN 55455 USA WWW: http://www.stat.umn.edu -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._