Skip to content

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

@sonoshah

Description

@sonoshah

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
Details
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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions