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

scoped functions won't work with tbl_lazy when the function is created by ~ #3594

Closed
yutannihilation opened this issue May 24, 2018 · 3 comments

Comments

@yutannihilation
Copy link
Member

This fails (if I use funs() instead of ~, it works):

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

con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "mtcars", mtcars)

tbl(con, "mtcars") %>%
  dplyr::summarise_all(~ any(is.na(.)))
#> Error in (function (..., .x = ..1, .y = ..2, . = ..1) : object 'mpg' not found

The error comes from here: https://github.com/tidyverse/dbplyr/blob/81419cab7d831e5a9d7e4279a4f893fa5750e8a1/R/tbl-lazy.R#L124

The root cause seems that partial_eval() won't accept expressions that generate a function in-place:

library(dbplyr)

q1 <- rlang::quo((function(x) is.na(x))(a))
q2 <- rlang::quo(is.na(a))

partial_eval(q1, vars = "a")
#> Error in (function(x) is.na(x))(a): object 'a' not found

partial_eval(q2, vars = "a")
#> <quosure>
#>   expr: ^is.na(a)
#>   env:  global

Another example here:

add_n <- function(n) {
  function(x) x + n
}

q3 <- rlang::quo(add_n(1)(a))

partial_eval(q3, vars = "a")
#> Error in add_n(1)(a) : object 'a' not found

The fix would probably come around here, but I didn't come up with the proper way of distinguishing the expression that generates function or not.

https://github.com/tidyverse/dbplyr/blob/ae86fb351bf3511aa73f622946db3a9ed7469fc4/R/partial-eval.r#L96

@hadley
Copy link
Member

hadley commented May 28, 2018

Part of #3433

@krlmlr
Copy link
Member

krlmlr commented Jun 25, 2018

Added reference in #3433, and copied reprex there.

@krlmlr krlmlr closed this as completed Jun 25, 2018
@lock
Copy link

lock bot commented Dec 22, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Dec 22, 2018
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

4 participants