How Can I Execute a String Expression?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How Can I Execute a String Expression?

Donald Macnaughton
I have the string ggstr that I've built with string manipulation:

ggstr =  "ggplot(df1, aes(x,y)) + geom_smooth(se=FALSE, span=0.01)"

Assuming df1 is properly defined, this string will execute properly if I
submit it manually without the quotes. How can execute the command as a
string, so that I can run it repeatedly (with minor modifications) in a
loop?  Presumably, it would be something like:

execute(ggstr)

Thanks for your help,

Don Macnaughton

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How Can I Execute a String Expression?

Rolf Turner

On 13/06/17 13:55, Donald Macnaughton wrote:

> I have the string ggstr that I've built with string manipulation:
>
> ggstr =  "ggplot(df1, aes(x,y)) + geom_smooth(se=FALSE, span=0.01)"
>
> Assuming df1 is properly defined, this string will execute properly if I
> submit it manually without the quotes. How can execute the command as a
> string, so that I can run it repeatedly (with minor modifications) in a
> loop?  Presumably, it would be something like:
>
> execute(ggstr)
>
> Thanks for your help.

eval(parse(text = ggstr))

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How Can I Execute a String Expression?

Bert Gunter-2
In reply to this post by Donald Macnaughton
eval(parse(text = yourstring))  # your string must be  quoted, because
that's what a string is.

But .... don't do this! (usually)

install.packages("fortunes") ## if not already downloaded and installed
library("fortunes")
fortune(106)

See ?substitute and ?bquote for perhaps better ways to proceed.

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Jun 12, 2017 at 6:55 PM, Donald Macnaughton <[hidden email]> wrote:

> I have the string ggstr that I've built with string manipulation:
>
> ggstr =  "ggplot(df1, aes(x,y)) + geom_smooth(se=FALSE, span=0.01)"
>
> Assuming df1 is properly defined, this string will execute properly if I
> submit it manually without the quotes. How can execute the command as a
> string, so that I can run it repeatedly (with minor modifications) in a
> loop?  Presumably, it would be something like:
>
> execute(ggstr)
>
> Thanks for your help,
>
> Don Macnaughton
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How Can I Execute a String Expression?

Jeff Newmiller
In reply to this post by Donald Macnaughton
R is not a very good macro language... I recommend against this strategy.

We could be more concrete in offering alternatives if you were a little more complete in your reproducible example [1][2][3]. What variations exactly were you thinking of? What kind of data are you working with? The way you read in the data can make a big difference in how effectively you analyze it. (Not asking for your actual data... just a fake version of it.)

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

[2] http://adv-r.had.co.nz/Reproducibility.html

[3] https://cran.r-project.org/web/packages/reprex/index.html
--
Sent from my phone. Please excuse my brevity.

On June 12, 2017 6:55:40 PM PDT, Donald Macnaughton <[hidden email]> wrote:

>I have the string ggstr that I've built with string manipulation:
>
>ggstr =  "ggplot(df1, aes(x,y)) + geom_smooth(se=FALSE, span=0.01)"
>
>Assuming df1 is properly defined, this string will execute properly if
>I
>submit it manually without the quotes. How can execute the command as a
>string, so that I can run it repeatedly (with minor modifications) in a
>loop?  Presumably, it would be something like:
>
>execute(ggstr)
>
>Thanks for your help,
>
>Don Macnaughton
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Loading...