-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Function calls in functions can not access to environment variables in parent functions #386
Comments
Simpler reprex: library(dplyr, w = FALSE)
library(dtplyr)
f <- function(x, .v = "x") {
mutate(x, a = .v)
}
x <- data.frame(a = "y")
lazy_dt(x) |>
f()
#> Error in eval(jsub, SDenv, parent.frame()): object '.v' not found |
The issue - library(dplyr, w = FALSE)
library(dtplyr)
f <- function(x, .v = "x") {
mutate(x, a = .v)
}
x <- lazy_dt(data.frame(a = "y"))
x$env
#> <environment: R_GlobalEnv> |
We could always grab the evaluation environment from the However this would fail if two functions (let's say a my_mutate <- function(x, .v = "x") {
mutate(x, a = .v)
}
my_filter <- function(x, .val = "x") {
filter(x, a == .val)
}
x <- lazy_dt(data.frame(a = "y"))
x %>%
my_mutate() %>%
my_filter() I'm not too familiar with "combining" environments (and possible issues or performance implications), but as far as I can tell that would be the only way to deal with this. |
Until/unless this is fixed in dtplyr, a workaround is to inject the variable library(dplyr, w = FALSE)
library(dtplyr)
f <- function(x, .v = "x") {
mutate(x, a = !!.v)
}
x <- data.frame(a = "y")
lazy_dt(x) |>
f()
#> Source: local data table [1 x 1]
#> Call: copy(`_DT1`)[, `:=`(a = "x")]
#>
#> a
#> <chr>
#> 1 x
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results Created on 2022-08-15 by the reprex package (v2.0.1.9000) |
This comment was marked as outdated.
This comment was marked as outdated.
Perhaps this bug is the same as #260. |
Ignore my previous comment. I think the issue is just that because Line 80 in ca698f4
|
Yeah, you are correct! library(dplyr, w = FALSE)
library(dtplyr)
f <- function(x, v = "x") {
mutate(x, a = v)
}
x <- data.frame(a = "y")
lazy_dt(x) |>
f()
#> Source: local data table [1 x 1]
#> Call: copy(`_DT1`)[, `:=`(a = "x")]
#>
#> a
#> <chr>
#> 1 x
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results Created on 2022-08-16 by the reprex package (v2.0.1) So this won't be fixed? Users should inject it by themselves? |
I think we can just do a better job of detecting whether the variable is a data.table symbol like |
@markfairbanks Still fails in such circumstances (seemingly, function calls in functions can not access environment variables, either):
Created on 2022-08-15 by the reprex package (v2.0.1)
Originally posted by @psychelzh in #317 (comment)
The text was updated successfully, but these errors were encountered: