tidyverse / dtplyr

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

namespace issue using dtplyr in package - cannot find function "." #349

Closed mkoohafkan closed 2 years ago

mkoohafkan commented 2 years ago

Probably related issues:

Using functions from a package that imports dtplyr can result in errors that do not occur when running the same code directly. I suspect it has something to do with dtplyr's ability to access data.table namespace when dtplyr is imported by a package vs being called from the top-level environment. I created a reprex package here: https://github.com/mkoohafkan/turbo-octo-invention which contains a single function (contents copied below):

#' Test DT
#' 
#' Test lazy_dt stuff.
#' 
#' @importFrom dplyr distinct pull as_tibble summarize
#' @importFrom dtplyr lazy_dt
#' @export
test_dt = function() {

  mtcars_dt = lazy_dt(mtcars)

  mtcars_dt |>
    summarize(cyl = unique(cyl)) |>
    as_tibble() |>
    pull(cyl)
}

And here's the reprex:

# install the package
# usethis::install_github("mkoohafkan/turbo-octo-invention")
library(testDT)
test_dt()
## Error in .(cyl = unique(cyl)) : could not find function "."

Running the code directly is fine:

# in package directory
devtools::load_all()

mtcars_dt = lazy_dt(mtcars)
mtcars_dt |>
  summarize(cyl = unique(cyl)) |>
  as_tibble() |>
  pull(cyl)
## [1] 6 4 8
markfairbanks commented 2 years ago

Yep - this occurs because testDT isn't data.table "aware". Until #184 is fixed all you need to do is define .datatable.aware <- TRUE somewhere in your package.

You can see here for an example. You only need to do this once btw, it doesn't need to be done for each function.

# devtools::install_github("markfairbanks/turbo-octo-invention@datatable-aware")
library(testDT)
test_dt()
#> [1] 6 4 8

I'm going to close this issue since it's a duplicate of #184, but if you have any further questions let me know.

mkoohafkan commented 2 years ago

Oh that's excellent, thank you! I did see Hadley's comment about checking for .datatable.aware in the evaluation environment but I didn't make the connection that I could simply add that statement to my own package.