[R] Eval() or parse() do not work inside function
Joshua Wiley
jwiley.psych at gmail.com
Tue Jul 27 21:04:03 CEST 2010
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/
More information about the R-help
mailing list