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

error using dtplyr in function, object not found? #164

Closed
sonoshah opened this issue Sep 3, 2020 · 2 comments
Closed

error using dtplyr in function, object not found? #164

sonoshah opened this issue Sep 3, 2020 · 2 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@sonoshah
Copy link

sonoshah commented Sep 3, 2020

I think I must be missing something obvious here, but I'm not sure what to make of this error. I have a simple function that accepts one argument which is used to filter a df object and pull out the values from a particular column. I had originally written this just using vanilla dplyr, but wanted to see if I could make it more efficient with dtplyr.

The underlying dtplyr code works fine outside the function, but whenever I try to call the function, I get an object not found error. I feel like I must be missing something simple here, any help would be greatly appreciated.

library(reprex)
library(data.table)
library(dtplyr)
library(tidyverse)

data("mtcars")
mtcars <- mtcars %>% rownames_to_column("car_name") 

mtcars_lazy <- lazy_dt(mtcars)

special_cars <- mtcars %>%
  sample_n(10) %>% 
  pull(car_name)

# This works
mtcars_lazy %>% filter(car_name == special_cars[1]) %>% as_tibble() %>% pull(mpg)
#> [1] 30.4

# Wrapping in function...
check_mpg <- function(car){
  print(paste("This is a: ",car))
  
  mtcars_lazy %>%
    filter(car_name == car) %>% as_tibble() %>% pull(mpg)
  
}
# Doesn't work
check_mpg(special_cars[1])
#> [1] "This is a:  Lotus Europa"
#> Error in eval(stub[[3L]], x, enclos): object 'car' not found
sessionInfo()
#> R version 3.6.3 (2020-02-29)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 18.04.4 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] forcats_0.5.0     stringr_1.4.0     dplyr_1.0.1       purrr_0.3.4      
#>  [5] readr_1.3.1       tidyr_1.1.1       tibble_3.0.3      ggplot2_3.3.2    
#>  [9] tidyverse_1.3.0   dtplyr_1.0.1      data.table_1.13.0 reprex_0.3.0     
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.4       cellranger_1.1.0 pillar_1.4.6     compiler_3.6.3  
#>  [5] dbplyr_1.4.4     highr_0.8        tools_3.6.3      digest_0.6.25   
#>  [9] lubridate_1.7.9  jsonlite_1.7.0   evaluate_0.14    lifecycle_0.2.0 
#> [13] gtable_0.3.0     pkgconfig_2.0.3  rlang_0.4.7      cli_2.0.2       
#> [17] DBI_1.1.0        yaml_2.2.1       haven_2.3.1      xfun_0.16       
#> [21] withr_2.2.0      xml2_1.3.2       httr_1.4.2       knitr_1.29      
#> [25] hms_0.5.3        generics_0.0.2   fs_1.5.0         vctrs_0.3.2     
#> [29] grid_3.6.3       tidyselect_1.1.0 glue_1.4.1       R6_2.4.1        
#> [33] fansi_0.4.1      readxl_1.3.1     rmarkdown_2.3    modelr_0.1.8    
#> [37] blob_1.2.1       magrittr_1.5     scales_1.1.1     backports_1.1.8 
#> [41] ellipsis_0.3.1   htmltools_0.5.0  rvest_0.3.6      assertthat_0.2.1
#> [45] colorspace_1.4-1 stringi_1.4.6    munsell_0.5.0    broom_0.7.0     
#> [49] crayon_1.3.4

Created on 2020-09-03 by the reprex package (v0.3.0)

@pnacht
Copy link

pnacht commented Dec 17, 2020

The problem is that dtplyr is interpretting car as a column name, which doesn't exist.

You therefore need to use bang notation (!!) to make it clear that car is a variable, not a column.

check_mpg <- function(car){
  print(paste("This is a: ",car))
  
  mtcars_lazy %>%
    filter(car_name == !!car) %>% as_tibble() %>% pull(mpg)
  
}
check_mpg(special_cars[1])
#> [1] "This is a:  Lincoln Continental"
#> [1] 10.4

Now, as to why this problem appears in a function but not in the global environment, I don't know. I even tried modifying the non-function version by replacing car_name == special_cars[1] with car_name == car (having previously defined car), but it still works there.

@hadley
Copy link
Member

hadley commented Jan 25, 2021

Minimal reprex:

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

df <- data.frame(x = 1:5, y = 2)
dt <- lazy_dt(df)

x_val <- 1
dt %>% filter(x == x_val) %>% collect()
#> # A tibble: 1 x 2
#>       x     y
#>   <int> <dbl>
#> 1     1     2
rm(x_val)

f <- function(x_val) {
  dt %>% filter(x == x_val) %>% collect()
}
f(2)
#> Error in eval(stub[[3L]], x, enclos): object 'x_val' not found

Created on 2021-01-25 by the reprex package (v0.3.0.9001)

I must be using the wrong environment somewhere.

@hadley hadley added the bug an unexpected problem or unintended behavior label Jan 25, 2021
@hadley hadley closed this as completed in 45b29bf Jan 26, 2021
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

3 participants