Skip to content
Prev 299044 / 398506 Next

"evaluating expressions" contained in a dataframe

Hello,

I've changed the way you create data.frame 'tests', like it was the 
conditions were factors, which are coded as integers. We need them of 
class character to be parsed. Note that the same could be done with the 
data.frame 'info' but it's not absolutely needed.


tests <- data.frame(rule=c("info$country == 'Greenland'", "info$age > 50"),
	stringsAsFactors=FALSE)
str(tests)
tests

#------------------ This does the trick
fun <- function(x){
	f <- function(){}  # an empty function, no body

	force(x)                 # evaluate the argument
	body(f) <- parse(text=x) # parse it and assign the function
	f  # return the function
}

# See if it works
expr1 <- fun(tests$rule[1])  # This creates a function
expr1()


Hope this helps,

Rui Barradas

Em 03-07-2012 17:24, New RUser escreveu: