[R] Read code from character string

Johannes Huesing johannes at huesing.name
Sun Jun 20 14:24:15 CEST 2010


Gabor Grothendieck <ggrothendieck at gmail.com> [Sat, Jun 19, 2010 at 11:19:35AM CEST]:
> On Sat, Jun 19, 2010 at 4:56 AM, Johannes Huesing <johannes at huesing.name> wrote:
> > A rule would be a data structure containing the ID of the rule, the
> > rule in human readable language, an expression evaluating variables
> > within the environment of the appropriate data frame (and resolving to
> > a logical vector), possibly the data frame itself, and the query
> > message (possibly as an sprintf expression). The data structure may be
> > an S4 object or a list. In our current workflow, we manage the
> > validation rules using a spreadsheet, and import them into a
> > competitor's analysis software.
> >
> > I could pass
> >
> > function(df) with(df, (vsstresn < 30 | vsstresn > 130) & vstestcd == "HR")
> 
> Another possibility would be to use an sql statement or fragment so if
> you passed this sql fragment:
> 
> >   fragment <- "(vsstresn < 30 or vsstresn > 130) and vstestcd = 'HR'"
> >
> > # then your program would run this code:
> >
> >   df <- data.frame(vsstresn = seq(10, 200, 10), vstestcd = rep(c("HR", "RH"), c(16, 4)))
> >   library(sqldf)
> >   sqldf(paste("select * from df where", fragment))
>   vsstresn vstestcd
> 1       10       HR
> 2       20       HR
> 3      140       HR
> 4      150       HR
> 5      160       HR

Many thanks! (ok, next time I will post a data example myself.)

Is this a corollary of fortune 106: If the answer is eval(parse()),
you should reconsider the language?

-- 
Johannes Hüsing               There is something fascinating about science. 
                              One gets such wholesale returns of conjecture 
mailto:johannes at huesing.name  from such a trifling investment of fact.                
http://derwisch.wikidot.com         (Mark Twain, "Life on the Mississippi")



More information about the R-help mailing list