Re: [R] How to catch value

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Sat 03 Dec 2005 - 23:01:24 EST

>>>>> "Chun-Ying" == Chun-Ying Lee <u9370004@cc.kmu.edu.tw>
>>>>> on Sat, 3 Dec 2005 17:23:35 +0800 writes:

    Chun-Ying> Dear R users:
    Chun-Ying> I have a problem about catch the value from function.

The basic concepts of good S (and hence R) programming are

  1. all functions should return their result, either explicitly via return() -- or "S like" by just have it the last expression ("last line") of your function.
  2. You *assign* the result of all your functions: res <- myfunc(....)

There are very few exceptions to this (where Kristels's advice would apply), but in your case (and in *most*), do *not* use assign nor "<<-".

    Chun-Ying> I have following two functions (part):

    Chun-Ying> fbolus1 <- function()
    Chun-Ying> {.........
    Chun-Ying>   par<-data.frame(Parameter=c("kel","Vd"),Initial=c(0))  
    Chun-Ying>   check(par)

replace the line above with
	par <- check(par)

    Chun-Ying> .....}

    Chun-Ying> check<-function(par)
    Chun-Ying> {
    Chun-Ying> if (par[ ,2] <= 0){
    Chun-Ying> cat("\nEnter again (y/n) ?\n\n")
    Chun-Ying> ans<-readline()
    Chun-Ying> if (ans == "n" ){
    Chun-Ying> return(tidy.up()) 
    Chun-Ying> }
    Chun-Ying> else{
    Chun-Ying> cat("\n")
    Chun-Ying> par<-edit(par)

just replace the line above with   
             return(edit(par))
 
    Chun-Ying> }

    Chun-Ying> }
    Chun-Ying> }  
    Chun-Ying> I wonder if it is possible to catch the value ("par" in this example) which
    Chun-Ying> "check" function generated and than go back to the "fbolus1" function,  and
    Chun-Ying> keep executing the command next to "check(par)", and "fbolus1" function can 
    Chun-Ying> accept the value "check" function generated. If it is possible, please give me
    Chun-Ying> some comments. Thanks in advance !!

You're welcome.
Martin Maechler, ETH Zurich



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Dec 03 23:14:37 2005

This archive was generated by hypermail 2.1.8 : Sun 04 Dec 2005 - 02:24:19 EST