[R] Functions within functions in R and S-Plus

From: Victor Gravenholt <victor.gravenholt_at_gmail.com>
Date: Wed 08 Jun 2005 - 02:54:24 EST


Sorry to bother you about a S-Plus related problem, but I hope someone can help.
I have tried to "translate" some code from R to S-Plus (I have colleague that insists on using S-Plus. And yes, I have tried to make him change to R...)
The following code works out fine in R, but in S-Plus (S-PLUS 6.2 for Windows Professional Ed.) I get the error message "Problem in est.theta(x): Object "func" not found ". I have tried to keep most of the structure in my original problem (but simplified it!), so the code could seem a bit strange. I suspect that this has something to do with different scoping rules in R and S-Plus, but still I have not found a workable solution in S-Plus.

mainfunc <- function(x){

est <- function(x,par){
abs(sum(par*x))
}

func <- function(par,x){

    est(x,par)
 }

est.theta <- function(x){

    optimize(func,lower=-10, upper=20,x=x)$minimum     }

est.theta(x)

}

x <- 1:10
mainfunc(x)

Any help is greatly appreciated.

Victor



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 Wed Jun 08 02:59:44 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:27 EST