I'm trying to write a function that has default arguments in R. The last argument is telling me how the user would like variable 'g' to be calculated. The default is "s + a" (the sum of the two previous arguments), but in principle it could be specified by any function (e.g. "s - a" or "s*a"...).
myFunc <- function(n,
s = rbernoulli(n, p = 0.5),
a = rnorm(n,sd = 2),
g = s + a){
data.frame(s = factor(s),
a = a,
g = as.numeric(g>0))
}
This works fine if I call the function itself:
myFunc(5)
To specify how I want 'g' to be calculated, I would like to do this:
myFunc(n = 5, g = a - s) (I)
or
myFunc(n = 5, a = ., s = ., g = a - s) (II)
It seems (I) will cause R to look for variables s/a in the workspace, which is not what I want. And (II) doesn't exist, but it would be my way of saying "use the default calculation for it".
I tried specifying my function with NULL, but that didn't work either. Please note that I'd like to be able to use 'g' within the function after I have its value (so I can't substitute it by a function, for example).