Your problem right now is that you cannot evaluate the subset
argument as cast
uses no standard evaluation to capture it. By using match.call()
you avoid the evaluation of subset
. The variable
referenced in the error is from the expression subset=variable=="ozone"
.
Here is a solution. If you really are only going to check the data and not attempt to modify it then this is trivial. All you need to do is check your variables, and if okay, change the call to be to cast
and evaluate. If you need to modify the variables, then it is a bit trickier, but not that much. You need to make sure that the call refers to the modified variables in the environment of the function. I do this below:
cast2 <- function(data, formula = ... ~ variable, fun.aggregate = NULL,
margins = FALSE, subset = TRUE, df = FALSE, fill = NULL,
add.missing = FALSE, value = guess_value(data)) {
#RunChecksOnData()
cast.call <- match.call()
cast.call[[1]] <- quote(cast) # update call
data$value <- data$value * 100 # modify data
cast.call[["data"]] <- quote(data) # update call for modified data (otherwise this refers to aqm)
eval(cast.call)
}
EDIT: if you do use this, you need to be careful about avoiding conflicts between your function environment and the environment that generates the call. This here works so long as your function doesn't contain any variables with the same name as any referenced in the call. If you cannot guarantee this is the case, then you need to be careful about how you proceed with evaluation. I would create a new environment that has for parent the parent.frame of the function, assign the modified objects (e.g. data
in this case) there, and then evaluate cast.call
in that environment. This complication only applies if you need to modify arguments. If you only need to check the arguments, you can modify the last line to be eval(cast.call, parent.frame())
and you should be fine.