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

pipe chain using !! results in error "object of type 'symbol' is not subsettable" #317

Closed
CerebralMastication opened this issue Jun 7, 2019 · 1 comment

Comments

@CerebralMastication
Copy link

This is a cross post from over in the Studio Community because I was not sure where to park this comment/issue.


So I got bit by a breaking change in dbplyr. In 1.4.0 there was a breaking change that impacts where eval happens in dbplyr (local vs on the server).

This code used to work in 1.3 but now breaks:

library(tidyverse)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang
testfun <- function(tab, tst_input){
  en_tst <- dplyr::enquo(tst_input)
  tab %>%
    filter(!!en_tst > 15) -> out
  return(out)
}

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

mtcars2 <- tbl(con, "mtcars")
testfun(tab = mtcars2, tst_input = mpg)
#> Error in call[[1]]: object of type 'symbol' is not subsettable

but that's easy enough to fix by replacing
filter(!!en_tst > 15)

with

filter(local(!!en_tst) > 15)

Not a huge deal, but it was really tricky for me to debug. And while I'm not super well versed in tidy development, I'm typically reasonably competent. So I was a little surprised how hard this was to figure out.

In the release notes there's mention of changes that break subsetting:

Subsetting ( [[ , $ , and [ ) functions are no longer evaluated locally.

So what changed that makes using !! in dbplyr problematic? Is it the subsetting change mentioned above or is it something else altogether?

FWIW, after the change I mention above, the code works great like this:

library(tidyverse)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang
testfun <- function(tab, tst_input){
  en_tst <- dplyr::enquo(tst_input)
  tab %>%
    filter(local(!!en_tst) > 15) -> out
  return(out)
}

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

mtcars2 <- tbl(con, "mtcars")

testfun(tab = mtcars2, tst_input = mpg)
#> # Source:   lazy query [?? x 11]
#> # Database: sqlite 3.22.0 [:memory:]
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  8  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#>  9  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> 10  17.8     6  168.   123  3.92  3.44  18.9     1     0     4     4
#> # … with more rows
@hadley
Copy link
Member

hadley commented Jun 7, 2019

I think this is just a bug, not related to the change in behaviour for$/[[ etc. Minimal reprex here:

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

testfun <- function(df, var){
  var <- enquo(var)
  filter(df, !!var > 15)
}

df <- memdb_frame(x = 13:16)

testfun(df, x)
#> Error in call[[1]]: object of type 'symbol' is not subsettable
testfun(df, x + 1)
#> # Source:   lazy query [?? x 1]
#> # Database: sqlite 3.22.0 [:memory:]
#>       x
#>   <int>
#> 1    15
#> 2    16

Created on 2019-06-07 by the reprex package (v0.2.1.9000)

@hadley hadley closed this as completed in fa7cc82 Jun 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants