I am writing a function where the arguments are names of objects or variable names in a data frame. To convert the strings to the objects I am using eval(parse(text=name)): f.graph.two.vbs<-function(dataname,v1){ val<-paste(dataname,v1,sep="$") val<-eval(parse(text=val)) val } However running this returns an error:>f.graph.two.vbs("data","RECORD")Error in data$RECORD : $ operator is invalid for atomic vectors Repeating the individual commands in the workspace does work though.. Do I need to pass the object data in the arguments for this to work? Thanks, Jorge -- Jorge A. Ahumada Technical Director Tropical Ecology Assessment and Monitoring Network (TEAM) Science and Knowledge Division Conservation International 2011 Crystal Dr. Suite 500 Arlington, VA 22202 Phone: 703-341-2543 Mobile: 202-716-3374 http://www.teamnetwork.org/
Dear Jorge, It might help if you included some sample data that replicates your problem, or, perhaps, the results of sessionInfo(). I cannot replicate it. Here are my results:> data <- data.frame(A = 1:10, RECORD = 1:10) > > f.graph.two.vbs<-function(dataname,v1){+ val<-paste(dataname,v1,sep="$") + val<-eval(parse(text=val)) + val + }> > f.graph.two.vbs("data","RECORD")[1] 1 2 3 4 5 6 7 8 9 10>Everything seems to work as expected. I am using:> sessionInfo()R version 2.11.1 (2010-05-31) x86_64-pc-mingw32 Josh On Tue, Jul 27, 2010 at 11:34 AM, Jorge A. Ahumada <j.ahumada at conservation.org> wrote:> I am writing a function where the arguments are names of objects or variable > names in a data frame. To convert the strings to the objects I am using > eval(parse(text=name)): > > f.graph.two.vbs<-function(dataname,v1){ > > ? ? val<-paste(dataname,v1,sep="$") > ? ? val<-eval(parse(text=val)) > ? ? val > ? ?} > > However running this returns an error: > >>f.graph.two.vbs("data","RECORD") > Error in data$RECORD : $ operator is invalid for atomic vectors > > Repeating the individual commands in the workspace does work though.. Do I > need to pass the object data in the arguments for this to work? > > Thanks, > > Jorge > -- > Jorge A. Ahumada > Technical Director > Tropical Ecology Assessment and Monitoring Network (TEAM) > Science and Knowledge Division > Conservation International > 2011 Crystal Dr. Suite 500 > Arlington, VA 22202 > Phone: 703-341-2543 > Mobile: 202-716-3374 > http://www.teamnetwork.org/ > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Hi Jose, What's the problem? # some data set.seed(123) mydata <- data.frame(x = rnorm(10), y = rpois(10, 10)) mydata f.graph.two.vbs<-function(dataname,v1){ val<-paste(dataname,v1,sep="$") val<-eval(parse(text=val)) val } f.graph.two.vbs('mydata', 'x') [1] -0.56047565 -0.23017749 1.55870831 0.07050839 0.12928774 1.71506499 0.46091621 -1.26506123 [9] -0.68685285 -0.44566197 Here is what I am using: R version 2.11.1 Patched (2010-05-31 r52180) x86_64-apple-darwin9.8.0 locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_2.11.1 HTH, Jorge On Tue, Jul 27, 2010 at 2:34 PM, Jorge A. Ahumada <> wrote:> I am writing a function where the arguments are names of objects or > variable > names in a data frame. To convert the strings to the objects I am using > eval(parse(text=name)): > > f.graph.two.vbs<-function(dataname,v1){ > > val<-paste(dataname,v1,sep="$") > val<-eval(parse(text=val)) > val > } > > However running this returns an error: > > >f.graph.two.vbs("data","RECORD") > Error in data$RECORD : $ operator is invalid for atomic vectors > > Repeating the individual commands in the workspace does work though.. Do I > need to pass the object data in the arguments for this to work? > > Thanks, > > Jorge > -- > Jorge A. Ahumada > Technical Director > Tropical Ecology Assessment and Monitoring Network (TEAM) > Science and Knowledge Division > Conservation International > 2011 Crystal Dr. Suite 500 > Arlington, VA 22202 > Phone: 703-341-2543 > Mobile: 202-716-3374 > http://www.teamnetwork.org/ > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Well, the data set I am using is quite large, but RECORD is also a numeric variable and data is a data frame. Here is my sessionInfo():>sessionInfo()R version 2.11.0 (2010-04-22) i386-apple-darwin9.8.0 locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_2.11.0 Passing the object itself as an argument in the function made it work, but still do not understand why.. Jorge On 7/27/10 3:04 PM, "Joshua Wiley" <jwiley.psych@gmail.com> wrote:> Dear Jorge, > > It might help if you included some sample data that replicates your > problem, or, perhaps, the results of sessionInfo(). I cannot > replicate it. Here are my results: > > >> > data <- data.frame(A = 1:10, RECORD = 1:10) >> > >> > f.graph.two.vbs<-function(dataname,v1){ > + val<-paste(dataname,v1,sep="$") > + val<-eval(parse(text=val)) > + val > + } >> > >> > f.graph.two.vbs("data","RECORD") > [1] 1 2 3 4 5 6 7 8 9 10 >> > > > Everything seems to work as expected. I am using: > >> > sessionInfo() > R version 2.11.1 (2010-05-31) > x86_64-pc-mingw32 > > Josh > > On Tue, Jul 27, 2010 at 11:34 AM, Jorge A. Ahumada > <j.ahumada@conservation.org> wrote: >> > I am writing a function where the arguments are names of objects or >> variable >> > names in a data frame. To convert the strings to the objects I am using >> > eval(parse(text=name)): >> > >> > f.graph.two.vbs<-function(dataname,v1){ >> > >> > val<-paste(dataname,v1,sep="$") >> > val<-eval(parse(text=val)) >> > val >> > } >> > >> > However running this returns an error: >> > >>> >>f.graph.two.vbs("data","RECORD") >> > Error in data$RECORD : $ operator is invalid for atomic vectors >> > >> > Repeating the individual commands in the workspace does work though.. Do I >> > need to pass the object data in the arguments for this to work? >> > >> > Thanks, >> > >> > Jorge >> > -- >> > Jorge A. Ahumada >> > Technical Director >> > Tropical Ecology Assessment and Monitoring Network (TEAM) >> > Science and Knowledge Division >> > Conservation International >> > 2011 Crystal Dr. Suite 500 >> > Arlington, VA 22202 >> > Phone: 703-341-2543 >> > Mobile: 202-716-3374 >> > http://www.teamnetwork.org/ >> > >> > ______________________________________________ >> > R-help@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. >> > > > > > -- > Joshua Wiley > Ph.D. Student, Health Psychology > University of California, Los Angeles > http://www.joshuawiley.com/ >-- Jorge A. Ahumada Technical Director Tropical Ecology Assessment and Monitoring Network (TEAM) Science and Knowledge Division Conservation International 2011 Crystal Dr. Suite 500 Arlington, VA 22202 Phone: 703-341-2543 Mobile: 202-716-3374 http://www.teamnetwork.org/ [[alternative HTML version deleted]]
On 27/07/2010 2:34 PM, Jorge A. Ahumada wrote:> I am writing a function where the arguments are names of objects or variable > names in a data frame. To convert the strings to the objects I am using > eval(parse(text=name)): > > f.graph.two.vbs<-function(dataname,v1){ > > val<-paste(dataname,v1,sep="$") > val<-eval(parse(text=val)) > val > } > > However running this returns an error: > > >f.graph.two.vbs("data","RECORD") > Error in data$RECORD : $ operator is invalid for atomic vectors >That is telling you that the object named data is not a list (or dataframe), it's an atomic vector. When I create a dataframe named "data", and run your code, it's fine: so I think this is simply a scoping error. The value of your function is the same as if you had coded data$RECORD right in your function, and apparently the data object thus found was not a dataframe.> Repeating the individual commands in the workspace does work though.. Do I > need to pass the object data in the arguments for this to work? >No, but you do need to make sure that the variable named by dataname is visible from within the function, or tell eval() to do the evaluation somewhere else. I'd guess you want val<-eval(parse(text=val), envir=parent.frame()) Duncan Murdoch> Thanks, > > Jorge >
Yes, this solved the problem. Thanks. Jorge On 7/27/10 3:13 PM, "Duncan Murdoch" <murdoch.duncan@gmail.com> wrote:> On 27/07/2010 2:34 PM, Jorge A. Ahumada wrote: >> > I am writing a function where the arguments are names of objects or >> variable >> > names in a data frame. To convert the strings to the objects I am using >> > eval(parse(text=name)): >> > >> > f.graph.two.vbs<-function(dataname,v1){ >> > >> > val<-paste(dataname,v1,sep="$") >> > val<-eval(parse(text=val)) >> > val >> > } >> > >> > However running this returns an error: >> > >>> > >f.graph.two.vbs("data","RECORD") >> > Error in data$RECORD : $ operator is invalid for atomic vectors >> > > > That is telling you that the object named data is not a list (or > dataframe), it''s an atomic vector. When I create a dataframe named > "data", and run your code, it''s fine: so I think this is simply a > scoping error. The value of your function is the same as if you had coded > > data$RECORD > > right in your function, and apparently the data object thus found was > not a dataframe. > > >> > Repeating the individual commands in the workspace does work though.. Do I >> > need to pass the object data in the arguments for this to work? >> > > > No, but you do need to make sure that the variable named by dataname is > visible from within the function, or tell eval() to do the evaluation > somewhere else. I''d guess you want > > val<-eval(parse(text=val), envir=parent.frame()) > > > Duncan Murdoch >> > Thanks, >> > >> > Jorge >> > > >-- Jorge A. Ahumada Technical Director Tropical Ecology Assessment and Monitoring Network (TEAM) Science and Knowledge Division Conservation International 2011 Crystal Dr. Suite 500 Arlington, VA 22202 Phone: 703-341-2543 Mobile: 202-716-3374 http://www.teamnetwork.org/ [[alternative HTML version deleted]]
It is probably easier to do this without eval and parse (see fortune(106)). Something like: val <- get(dataname)[[v1]] hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Jorge A. Ahumada > Sent: Tuesday, July 27, 2010 12:35 PM > To: r-help at r-project.org > Subject: [R] Eval() or parse() do not work inside function > > I am writing a function where the arguments are names of objects or > variable > names in a data frame. To convert the strings to the objects I am using > eval(parse(text=name)): > > f.graph.two.vbs<-function(dataname,v1){ > > val<-paste(dataname,v1,sep="$") > val<-eval(parse(text=val)) > val > } > > However running this returns an error: > > >f.graph.two.vbs("data","RECORD") > Error in data$RECORD : $ operator is invalid for atomic vectors > > Repeating the individual commands in the workspace does work though.. > Do I > need to pass the object data in the arguments for this to work? > > Thanks, > > Jorge > -- > Jorge A. Ahumada > Technical Director > Tropical Ecology Assessment and Monitoring Network (TEAM) > Science and Knowledge Division > Conservation International > 2011 Crystal Dr. Suite 500 > Arlington, VA 22202 > Phone: 703-341-2543 > Mobile: 202-716-3374 > http://www.teamnetwork.org/ > > ______________________________________________ > 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.