When I use this with atomic vector, it works:
x = data.frame(myvar = 1:10)
test_atom = function(var, maxvalue = max(var)) {
return(maxvalue)
}
test_atom(x$myvar)
# [1] 10
But when I try to evaluate a column in a data frame, there is a problem:
test_df = function(data, var, maxvalue = max(var)) {
params = as.list(match.call()[-1])
data$var = eval(params$var, data)
return(maxvalue)
}
test_df(x, myvar)
# Error in test_df(x, myvar) : object 'myvar' not found
Note however that the following works ok, so evaluation seems fine:
test_df2 = function(data, var, maxvalue = max(var)) {
params = as.list(match.call()[-1])
data$var = eval(params$var, data)
return(data$var)
}
test_df2(x, myvar)
# [1] 1 2 3 4 5 6 7 8 9 10
How to properly evaluate the argument so that it detects the maximum value of x$myvar?
EDIT To properly spell out my intention, I want the possibility to set the value for maxvalue manually, but in case I leave it blank, it should set itself to the maximum value of myvar. This could be achieved by a conditional statement within the function to check whether the argument is NULL and then setting the maxvalue to maximum value of myvar within the function - but I wanted to do it in a simpler way, i.e. not within the function.
Example - both should be possible:
test_df(x, myvar, 5) # I set the value of the last argument manually
test_df(x, myvar) # I leave it blank - and it sets itself to the max value of `myvar`
test_df(x, x$myvar). This return '10'.$function. Such format is customary in many R functions, such assubset(x, myvar > 5)