I want to use subset
within another function but to pass on the non-standard evaluation arguments from the top-level function. The following is non-working code, but outlines the idea:
foo_1 <- function(x, mysubset)
{
# some deparse, substitute etc. magic here ??
subset(x, subset)
}
foo_1(ansombe, x1 > 5)
I want this to get the same results as for subset(ansombe, x1 > 5)
. Also, I want the same to work when the argument is passed on to a deeper level, i.e.
foo_2 <- function(x, mysubset)
{
# some deparse, substitute etc. magic here ??
foo_1(x, mysubset)
}
foo_2(ansombe, x1 > 5)
Here also I want the same result as above.
What I have tried so far
I tried a substitute
-deparse
, eval
-parse
combination, like
foo_1 <- function(x, mysubset)
{
tx <- deparse(substitute(mysubset))
subset(x, eval(parse(text=tx)))
}
foo_1(anscombe, x1 >5)
This is fine, but how do I go on for foo_2
now?
Also, I remember the dictum by Thomas Lumley:
If the answer is parse() you should usually rethink the question. -- Thomas Lumley (R-help, February 2005)
So, I was wondering if there is a better approach than an eval
-parse
combination.?
Any ideas?
PS. This question is similar but does not include the deeper nesting: Pass subset argument through a function to subset
PPS: Maybe it is fruitful applying the .
function from plyr
, but I don't know how...