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
The easiest way, perhaps, is to assign the internal functions to frame 1:> 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 + } + + assign("func", func, frame=1) + assign("est", est, frame=1) + est.theta(x) + + }> > x <- 1:10 > mainfunc(x)[1] 5.286101e-006 You should read about the scoping rule differences in, e.g., S Programming. Andy> From: Victor Gravenholt > > 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 at 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 > > >
Victor Gravenholt wrote:> 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) > > >You need to supply func to est.theta. R uses scoping to find func and est. S-PLUS scoping is defined differently and cannot, so you must assign them to the current frame. is.R <- !is.null(version$language) 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 } if(!is.R) { assign("func", func, frame = 1) assign("est", est, frame = 1) } est.theta(x) } mainfunc(1:10) --sundar
How about the following: mainfunc <- function(x){ est <- function(x,par){ abs(sum(par*x)) } func <- function(par,x, est.=est){ est.(x,par) } est.theta <- function(x, func.=func, est.=est){ optimize(func.,lower=-10, upper=20,x=x, est.=est.)$minimum } est.theta(x, func.=func, est.=est) } > x <- 1:10 > mainfunc(x) [1] 5.286101e-006 spencer graves Victor Gravenholt wrote:> 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 at 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
p.s. This modification gave the same answer for me in both S-Plus 6.2 and R 2.1.0 patched under Windows XP. Spencer Graves wrote:> How about the following: > > mainfunc <- function(x){ > > est <- function(x,par){ > abs(sum(par*x)) > } > > func <- function(par,x, est.=est){ > est.(x,par) > } > > est.theta <- function(x, func.=func, est.=est){ > optimize(func.,lower=-10, upper=20,x=x, > est.=est.)$minimum > } > > est.theta(x, func.=func, est.=est) > > } > > > x <- 1:10 > > mainfunc(x) > [1] 5.286101e-006 > > spencer graves > > Victor Gravenholt wrote: > >> 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 at 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 > >
On Tue, 7 Jun 2005, Liaw, Andy wrote:> The easiest way, perhaps, is to assign the internal functions to frame 1: >Another easy way is to use the MC() function in section 3.3.1 of the R FAQ, which makes function closures in a way that works in S-PLUS. -thomas