Dear all, I want to make an if condition variable like : a = 10 CONDITION = " a > 0 " if ( CONDITION ) print(" a is bigger" ) I tried get , getElement , eval without success ? Thanks
Hi ce, a<-10 condition<-expression("a>0") if(eval(parse(text=condition))) cat("a>0\n") Jim On Thu, Jun 2, 2016 at 12:30 PM, ce <zadig_1 at excite.com> wrote:> > Dear all, > > I want to make an if condition variable like : > > a = 10 > CONDITION = " a > 0 " > > if ( CONDITION ) print(" a is bigger" ) > > I tried get , getElement , eval without success ? > > Thanks > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
if ( eval(parse(text=CONDITION ))) print(" a is bigger" ) Best, Ista On Jun 1, 2016 10:32 PM, "ce" <zadig_1 at excite.com> wrote:> > Dear all, > > I want to make an if condition variable like : > > a = 10 > CONDITION = " a > 0 " > > if ( CONDITION ) print(" a is bigger" ) > > I tried get , getElement , eval without success ? > > Thanks > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]
Beware of getting too "meta" in your programming... it is rarely worth it. Just write the code and move on with life. That is the beauty of a scripting language. -- Sent from my phone. Please excuse my brevity. On June 1, 2016 7:30:29 PM PDT, ce <zadig_1 at excite.com> wrote:> >Dear all, > >I want to make an if condition variable like : > >a = 10 >CONDITION = " a > 0 " > >if ( CONDITION ) print(" a is bigger" ) > >I tried get , getElement , eval without success ? > >Thanks > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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]]
a <- 10 CONDITION <- (a > 0) if ( CONDITION ) print(" a is bigger" ) On Wed, Jun 1, 2016 at 10:30 PM, ce <zadig_1 at excite.com> wrote:> > Dear all, > > I want to make an if condition variable like : > > a = 10 > CONDITION = " a > 0 " > > if ( CONDITION ) print(" a is bigger" ) > > I tried get , getElement , eval without success ? > > Thanks > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
>>>>> Jim Lemon <drjimlemon at gmail.com> >>>>> on Thu, 2 Jun 2016 13:03:01 +1000 writes:> Hi ce, > a<-10 > condition<-expression("a>0") > if(eval(parse(text=condition))) cat("a>0\n") While this may answer the question asked, the above is *not* good advice, excuse me, Jim :> fortune(106)If the answer is parse() you should usually rethink the question. -- Thomas Lumley R-help (February 2005)> fortune(181)Personally I have never regretted trying not to underestimate my own future stupidity. -- Greg Snow (explaining why eval(parse(...)) is often suboptimal, answering a question triggered by the infamous fortune(106)) R-help (January 2007) --------------------- Good advice would emphasize to use expressions rather than strings.... and yes that's a bit more sophistication. But it's worth it. Martin>> Jim > On Thu, Jun 2, 2016 at 12:30 PM, ce <zadig_1 at excite.com> wrote: >> >> Dear all, >> >> I want to make an if condition variable like : >> >> a = 10 >> CONDITION = " a > 0 " >> >> if ( CONDITION ) print(" a is bigger" ) >> >> I tried get , getElement , eval without success ? >> >> Thanks >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Jeff Newmiller <jdnewmil at dcn.davis.ca.us> writes:> Beware of getting too "meta" in your programming... it is rarely worth > it. Just write the code and move on with life. That is the beauty of a > scripting language.+1 I think this a very common pitfall (I know it from own experience...) and I would say a candidate for a fortune? Rainer -- Rainer M. Krug email: Rainer<at>krugs<dot>de PGP: 0x0F52F982 -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 454 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160602/4a93d6ec/attachment.bin>