[R] writing simple function through script

Dieter Menne dieter.menne at menne-biomed.de
Wed Sep 10 16:08:45 CEST 2008


Benoit Boulinguiez <benoit.boulinguiez <at> ensc-rennes.fr> writes:

> yo<-function(Xdata)
> {
> n<-length(Xdata[,1])
> 
> Lgm<-nls(formula=LgmFormula,
>   data=Xdata,
>   start=list(a=1500,b=0.1),weights=Xdata$Qe)
> return(Lgm)
> }
> 
> After the execution of the script, when I call the function yo on data
> called NC60.DATA I get an error.
> 
> #yo(NC60.DATA)
> Erreur dans eval(expr, envir, enclos) : objet "Xdata" not found
> 

This is my most frequently used Brian Ripley trick which I always have to look
up in my examples list. Getting the environment right is the most difficult part
in writing R packages

Dieter

DNase1 <- subset(DNase, Run == 1)
DNase1$Qe = rnorm(nrow(DNase1),1,0.1)
LgmFormula = density ~ SSlogis(log(conc), Asym, xmid, scal)
fm1DNase1 <- nls(LgmFormula, DNase1)

y0 = function(Xdata)
{
  LgmFormula = density ~ SSlogis(log(conc), Asym, xmid, scal)
  # the following fails
  #nls(LgmFormula, Xdata, weights=Xdata$Qe)
# Brian Ripley trick to get around an issue in lme (not only)
# http://finzi.psych.upenn.edu/R/Rhelp02a/archive/16599.html
  eval(substitute(nls(LgmFormula,Xdata),  list(form=LgmFormula)))
}

y0(DNase1)



More information about the R-help mailing list