Hi all, I try to write a simple function in a script. The script is as follows 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 The object Xdata isn't found. Why? Is that possible to debug a function with R? Regards/Cordialement ------------- Benoit Boulinguiez Ph.D Ecole de Chimie de Rennes (ENSCR) Bureau 1.20 Equipe CIP UMR CNRS 6226 "Sciences Chimiques de Rennes" Campus de Beaulieu, 263 Avenue du Général Leclerc 35700 Rennes, France Tel 33 (0)2 23 23 80 83 Fax 33 (0)2 23 23 81 20 http://www.ensc-rennes.fr/ [[alternative HTML version deleted]]
Its looking in environment(LgmFormula) for Xdata in order to evaluate Xdata$Qe in the weights argument so just specify weights = Qe. Alternately insert the following as the beginning of yo. It creates a new local LgmFormula but with its environment set to the current environment: environment(LgmFormula) <- environment() See the Environment section of ?formula On Wed, Sep 10, 2008 at 9:22 AM, Benoit Boulinguiez <benoit.boulinguiez at ensc-rennes.fr> wrote:> Hi all, > > I try to write a simple function in a script. The script is as follows > > 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 > > > The object Xdata isn't found. Why? > Is that possible to debug a function with R? > > Regards/Cordialement > > ------------- > Benoit Boulinguiez > Ph.D > Ecole de Chimie de Rennes (ENSCR) Bureau 1.20 > Equipe CIP UMR CNRS 6226 "Sciences Chimiques de Rennes" > Campus de Beaulieu, 263 Avenue du G?n?ral Leclerc > 35700 Rennes, France > Tel 33 (0)2 23 23 80 83 > Fax 33 (0)2 23 23 81 20 > http://www.ensc-rennes.fr/ > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >
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)
Hi Benoit, If you are in the directory that contains your data, you can use> NC60 = read.table("NC60.DATA")# there are some arguments you can use in "read.table" OR>NC60 = read.table("path to your data/NV60.DATA")> yo(NC60)By the way, I do not know your "LgmFormula". I think you already defined it. Rao Fu Ph.D Yale University Department of Statistics Email: rao.fu@yale.edu>>Date: Wed, 10 Sep 2008 15:22:37 +0200 >>From: "Benoit Boulinguiez" <benoit.boulinguiez@ensc-rennes.fr> >>Subject: [R] writing simple function through script >>To: <r-help@r-project.org> >>Message-ID: <001601c91348$4b505a20$>>50111481@PR017080> >>Content-Type: text/plain >> >>Hi all, >> >>I try to write a simple function in a script. The script is as follows >> >>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 >> >> >>The object Xdata isn't found. Why? >>Is that possible to debug a function with R? >> >>Regards/Cordialement >> >>----------- >>Benoit Boulinguiez >>Ph.D >>Ecole de Chimie de Rennes (ENSCR) Bureau 1.20 >>Equipe CIP UMR CNRS 6226 "Sciences Chimiques de Rennes" >>Campus de Beaulieu, 263 Avenue du Général Leclerc >>35700 Rennes, France >>Tel 33 (0)2 23 23 80 83 >>Fax 33 (0)2 23 23 81 20 >>http://www.ensc-rennes.fr/ <http://www.ensc-rennes.fr/>[[alternative HTML version deleted]]