Hi Duncan,
thanks for your reaction. I'm not following completely though what you mean
with "no guarantee that the length() function will do what I want if I
evaluate it in an environment set by the user". I wasn't intending to give
the user the opportunity to set those environments, but is there
On 17/10/2014, 2:23 PM, Gabriel Becker wrote:
> Joris,
>
> For me
>
> length(environment(form)[["x"]])
>
> Was about twice as fast as
>
> length(get("x",environment(form
>
> In the year-old version of R (3.0.2) that I have on the virtual machine i'm
> currently using.
Those are different:
Thanks again William, I owe you one!
Cheers
Joris
On Fri, Oct 17, 2014 at 11:36 PM, William Dunlap wrote:
> In my example function I did not evaluate the formula either, just a part
> of it.
>
> If you leave off the envir and enclos arguments to eval in your
> function you can get surprising (wr
In my example function I did not evaluate the formula either, just a part of it.
If you leave off the envir and enclos arguments to eval in your
function you can get surprising (wrong) results. E.g.,
> afun(y ~ varnames)
[[1]]
[1] 10 9 8 7 6 5 4 3 2 1
[[2]]
[1] "y""va
Thank you both, great ideas. William, I see the point of using eval, but
the problem is that I can't evaluate the formula itself yet. I need to know
the length of these variables to create a function that is used to
evaluate. So if I try to evaluate the formula in some way before I created
the fun
I got the default value for getRHSLength's data argument wrong - it
should be NULL, not parent.env().
getRHSLength <- function (formula, data = NULL)
{
rhsExpr <- formula[[length(formula)]]
rhsValue <- eval(rhsExpr, envir = data, enclos = environment(formula))
length(rhsV
I would use eval(), but I think that most formula-using functions do
it more like the following.
getRHSLength <-
function (formula, data = parent.frame())
{
rhsExpr <- formula[[length(formula)]]
rhsValue <- eval(rhsExpr, envir = data, enclos = environment(formula))
length(rhsValue)
}
Joris,
For me
length(environment(form)[["x"]])
Was about twice as fast as
length(get("x",environment(form
In the year-old version of R (3.0.2) that I have on the virtual machine i'm
currently using.
As for you, the eval method was much slower (though my factor was much
larger than 20)
>
Dear R gurus,
I need to know the length of a variable (let's call that X) that is
mentioned in a formula. So obviously I look for the environment from which
the formula is called and then I have two options:
- using eval(parse(text='length(X)'),
envir=environment(formula) )
-