[R] Executable expressions
R. Michael Weylandt
michael.weylandt at gmail.com
Thu Jan 19 19:42:32 CET 2012
Fabulous example, Baptiste!
Yeah, the knock is that it's just not at all clear and too often
symptomatic of someone trying to be too clever by half when there's
usually a better way. There are times when these tricks can be really
helpful for non-standard evaluation (e.g., the curve() function --
super cool, but not at all easy to understand on a quick read) but
overall it's just not a very R idiom. You can see where it shows up
"in the wild":
which(sapply(apropos("*"), function(f) any(grepl("eval(parse",
deparse(get(f)), fixed = TRUE)))) #Speaking of a lack of clarity....
Michael
On Wed, Jan 18, 2012 at 10:03 PM, baptiste auguie
<baptiste.auguie at googlemail.com> wrote:
> One reason might be that you can easily fool the user into running
> unexpected/unreadable commands. Guess what this does:
>
> cmd <- paste(c(letters[c(19L, 25L, 19L, 20L, 5L, 13L)], "(' ",
> letters[c(19L, 21L, 4L, 15L)], " ", letters[c(4L,
> 5L, 19L, 20L, 18L, 15L, 25L)], " ", letters[c(1L, 12L, 12L)], " ')"),
> collapse="")
>
> ## not run
> ## eval(parse(text=cmd))
>
> b.
>
> On 19 January 2012 11:05, Wet Bell Diver <wetbelldiver at gmail.com> wrote:
>>
>> for my info, why is this rarely a good idea? Is that the case for this
>> particular example , or is eval(paste()) generally rarely a good idea?
>>
>> --Peter
>>
>> Op 18-1-2012 22:22, R. Michael Weylandt schreef:
>>
>>> eval(parse(text = a))
>>>
>>> But this is rarely a good idea....perhaps you could say a little more
>>> about your overall goal and we could direct you to a more "R"-ish
>>> solution?
>>>
>>> library(fortunes)
>>> fortune("rethink")
>>>
>>> Michael
>>>
>>> On Wed, Jan 18, 2012 at 4:18 PM, Ajay Askoolum<aa2e72e at yahoo.co.uk>
>>> wrote:
>>>>
>>>> Given
>>>>
>>>> a<-"c(1,2,3,4,5)"
>>>>
>>>> How can I evaluate the variable a to return a (numeric) vector
>>>> comprising of 1,2,3,4,5? Thanks.
>>>>
>>>> [[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.
>>>>
>>> ______________________________________________
>>> 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.
>>
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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