tidyverse / dtplyr

Data table backend for dplyr
https://dtplyr.tidyverse.org
Other
664 stars 58 forks source link

replace_na() can't find local variable #304

Closed hadley closed 2 years ago

hadley commented 2 years ago
library(dtplyr)
library(dplyr, warn.conflicts = FALSE)

dt <- lazy_dt(data.frame(x = c(1, NA)), "dt")

local({
  x_miss <- 2

  dt %>% mutate(x = tidyr::replace_na(x, x_miss)) 
})
#> Error in fcoalesce(x, x_miss): object 'x_miss' not found

Created on 2021-10-05 by the reprex package (v2.0.1)

Presumably because we're not correctly inlining x_miss into the call as we normally do:

local({
  x_miss <- 2

  dt %>% mutate(x = x + x_miss) %>% show_query()
})
#> copy(dt)[, `:=`(x = x + 2)]

Created on 2021-10-05 by the reprex package (v2.0.1)

@markfairbanks does this ring any bells?

hadley commented 2 years ago

Slowly bringing back my memory of how this works. I think this minimal reprex illustrates the problem:

local({
  n <- 10
  capture_dot(dt, x + n)
  capture_dot(dt, tidyr::replace_na(x, n))
})
hadley commented 2 years ago

Maybe because https://github.com/tidyverse/dtplyr/blame/master/R/tidyeval.R#L107-L109 doesn't process the other arguments?