On Sun, 24 Oct 2010, Erik Iverson wrote:
Hello,
How would you go about handling the following situation?
This is on R 2.12.0 on Ubuntu 32-bit.
I have a wrapper function to lm. I want to pass in a
subset argument. First, I just thought I'd use "...".
The subset arg needs to be unevaluated until lm() - or whatever function
- is called in your function.
The canonical advice for this kind of question about passing unevaluated
args is to study the first lines of the function lm noting what it does
with objects cl <- match.call() and mf <- match.call( expand.dots = FALSE
).
Something like this might be what you want:
testlm2 <- function(formula, ...) {
mc <- match.call()
mc[[1]] <- as.name('lm')
eval(mc)
}
testlm2(bmi ~ age, data= df1, subset = age > 50)
HTH,
Chuck
## make example reproducible
set.seed(123)
df1 <- data.frame(age = rnorm(100, 50, 10),
bmi = rnorm(100, 30, sd = 2))
## create a wrapper using "..."
testlm <- function(formula, ...) {
lm(formula, data = df1, ...)
}
testlm(bmi ~ age, subset = age > 50)
Error in eval(expr, envir, enclos) :
..1 used in an incorrect context, no ... to look in
I found some other examples of this error message,
but couldn't piece together how it fits in with this
example.
Next, I tried specifying a subset argument.
testlm2 <- function(formula, subset) {
lm(formula, data = df1, subset = subset)
}
testlm2(bmi ~ age, subset = age > 50)
Error in xj[i] : invalid subscript type 'closure'
I also don't understand this one.
Any pointers on if I'm just missing the easy
solution to do what I want? Any explanations
as to the above behavior (I know it has to do
with model.frame, but not sure how) would also
be greatly appreciated!
Thanks!
--Erik
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:[email protected] UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.