[R] evaluate a string

Martin Maechler maechler at stat.math.ethz.ch
Wed Jun 26 11:19:54 CEST 2002


>>>>> "Joerg" == Joerg Maeder <maeder at atmos.umnw.ethz.ch> writes:


    Joerg> i want to execute different commands that are saved
    Joerg> in strings (eg: "d <- 4+7" and some more complicated
    Joerg> things). How can i execute/evaluate them. I know i
    Joerg> could save them to a temp-file a read this with
    Joerg> source.  But is there a way without a file in the
    Joerg> middle?  I know also the 'assign' command but I think
    Joerg> it would only the simply cases.

source() builds on parse() and eval() 
which you need here.

parse() : character --> expression
eval () : expressaion --> [evaluated result]

E.g.,

eval(parse(text = "print(d <- 4 + 7)"))
> [1] 11

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list