Re: [R] execute r-code stored in a string variable

2011-08-02 Thread Samuel Le
Yes, you can use:
eval(parse(text=c))

On the other hand I would not recommend to use c as a variable name as it is 
the name of a very important function in the R language to aggregate data.

HTH,
Samuel

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Kim Lillesøe
Sent: 02 August 2011 13:22
To: r-help@R-project.org
Subject: [R] execute r-code stored in a string variable

Dear all

I have a simple R question. How do I execute R-code stored in a variable?

E.g if I have a variable which contains some R-code:
c = "reg <- lm(sales$sales~sales$price)"

Is it possible to execute c
E.g like Exec(c)

I hope someone can help.

Thank you
Kim Lillesøe

[[alternative HTML version deleted]]



__ Information from ESET NOD32 Antivirus, version of virus signature 
database 6275 (20110707) __

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__ Information from ESET NOD32 Antivirus, version of virus signature 
database 6275 (20110707) __

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__
R-help@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.


Re: [R] execute r-code stored in a string variable

2011-08-02 Thread Ista Zahn
Hi Kim,
You can use

eval(parse(text = c))

Best,
Ista

On Tue, Aug 2, 2011 at 8:22 AM, Kim Lillesøe  wrote:
> Dear all
>
> I have a simple R question. How do I execute R-code stored in a variable?
>
> E.g if I have a variable which contains some R-code:
> c = "reg <- lm(sales$sales~sales$price)"
>
> Is it possible to execute c
> E.g like Exec(c)
>
> I hope someone can help.
>
> Thank you
> Kim Lillesře
>
>        [[alternative HTML version deleted]]
>
>
> __
> R-help@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.
>
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

__
R-help@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.