Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Mapped props should evaluate in environment function #30

Closed
hadley opened this issue Aug 19, 2013 · 3 comments
Closed

Mapped props should evaluate in environment function #30

hadley opened this issue Aug 19, 2013 · 3 comments

Comments

@hadley
Copy link
Member

hadley commented Aug 19, 2013

i.e. so the current code doesn't fail

f <- function(n) {
  force(n)
  props(x ~ n + wt)
}
prop_value(f(5)$x, mtcars)

That implies that props() should also capture the environment and store it, and prop_value needs to use the environment. Maybe that implies that this particular type of prop should be a different class?

@wch
Copy link
Contributor

wch commented Aug 19, 2013

How common do you think this use case would be? If it's not too common, it could be an argument to prop, e.g.:

props(x = prop(quote(n + wt), env = environment()))

Where env has a default value of NULL, which means to not capture the env and evaluate later.

The props function could also have an arg that tells it to pass the env argument to each individual prop, so you could also use this syntax:

props(x ~ n + wt, .env = environment()))

@hadley
Copy link
Member Author

hadley commented Aug 19, 2013

I think your proposal for prop is fine.

But I think props should handle it automatically, because it should just work in most cases without the user having to think about scoping etc. It would be easy to do after adding env to prop because formulas capture the environment in which they are defined:

f <- function(var, n) {
  force(n)
  eval(substitute(var ~ x + n, list(var = substitute(var))))
}
props(f(x, 5), f(y, 10))

environment(f(y, 5))$n
environment(f(y, 10))$n

although admittedly that example is a bit unlikely.

@wch
Copy link
Contributor

wch commented Aug 21, 2013

Closed by cde70bf and 785532f.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants