Skip to content
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

Data pronoun .env fails in i position functions #450

Open
markfairbanks opened this issue Sep 13, 2023 · 2 comments
Open

Data pronoun .env fails in i position functions #450

markfairbanks opened this issue Sep 13, 2023 · 2 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@markfairbanks
Copy link
Collaborator

library(dtplyr)
library(dplyr, warn.conflicts = FALSE)

x <- 3

dt <- lazy_dt(tibble(x = 1:3, y = 1:3))

# Works
dt %>%
  mutate(x_x = .data$x * .env$x)
#> Source: local data table [3 x 3]
#> Call:   copy(`_DT1`)[, `:=`(x_x = x * ..x)]
#> 
#>       x     y   x_x
#>   <int> <int> <dbl>
#> 1     1     1     3
#> 2     2     2     6
#> 3     3     3     9
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

# Fails
dt %>%
  filter(.data$x < .env$x)
#> Error: Object '..x' not found amongst [x, y]

This one is going to be a tough one since the .. shortcut doesn't work in i

@markfairbanks markfairbanks added the bug an unexpected problem or unintended behavior label Sep 13, 2023
@markfairbanks
Copy link
Collaborator Author

I think the easiest solution is to wait until the newest version of data.table is on CRAN and we can use the new env arg.

suppressMessages(library(data.table))

df <- data.table(x = 1:3, y = 1:3)

x <- 3

df[x < .env_x, env = list(.env_x = x)]
#>        x     y
#>    <int> <int>
#> 1:     1     1
#> 2:     2     2

@markfairbanks
Copy link
Collaborator Author

markfairbanks commented Sep 14, 2023

We could probably just even detect if .env is used and do something like this.

suppressMessages(library(data.table))

df <- data.table(x = 1:3, y = 1:3)

x <- 3

df[x < .env$x, env = list(.env = environment())]
#>        x     y
#>    <int> <int>
#> 1:     1     1
#> 2:     2     2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

1 participant