[R] Eval() or parse() do not work inside function
Greg Snow
Greg.Snow at imail.org
Wed Jul 28 01:14:24 CEST 2010
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.
More information about the R-help
mailing list