tidyverse / dtplyr

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

dplyr:across does not work with data.table objects #355

Closed andreassoteriadesmoj closed 2 years ago

andreassoteriadesmoj commented 2 years ago

Hi,

Looks like dtplyr and dplyr:across don't get along when the table is a data.table instead of a data frame or tibble.

# Works fine with data frame
group <- 'Species'
iris %>% 
    dplyr::group_by(
        dplyr::across(
            dplyr::all_of(group)
        )
    ) %>% 
    dplyr::summarize(n = dplyr::n())

# # A tibble: 3 x 2
#  #  Species        n
#   <fct>      <int>
# 1 setosa        50
# 2 versicolor    50
# 3 virginica     49

# Doesn't work with data.table
group <- 'Species'
iris %>% 
    data.table::as.data.table() %>% 
    dplyr::group_by(
        dplyr::across(
            dplyr::all_of(group)
        )
    ) %>% 
    dplyr::summarize(n = dplyr::n())

#Error: Must subset columns with a valid subscript vector.
#x Subscript has the wrong type `function`.
#It must be logical, numeric, or character.
#Run `rlang::last_error()` to see where the error occurred.

Andreas

markfairbanks commented 2 years ago

Have you updated to the latest version of dtplyr? Everything works fine when I run this.

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

group <- 'Species'
iris %>% 
    data.table::as.data.table() %>% 
    dplyr::group_by(
        dplyr::across(
            dplyr::all_of(group)
        )
    ) %>% 
    dplyr::summarize(n = dplyr::n())
#> Source: local data table [3 x 2]
#> Call:   `_DT1`[, .(n = .N), keyby = .(Species)]
#> 
#>   Species        n
#>   <fct>      <int>
#> 1 setosa        50
#> 2 versicolor    50
#> 3 virginica     50
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
groceryheist commented 2 years ago

I just hit this bug right now. Trying on the development version.

markfairbanks commented 2 years ago

@groceryheist do you have an example this occurs on? Or does it occur on the example above?

This works fine for me on both the CRAN version and the development version.

groceryheist commented 2 years ago

It worked fine on the development version

andreassoteriadesmoj commented 2 years ago

Have you updated to the latest version of dtplyr? Everything works fine when I run this.

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

group <- 'Species'
iris %>% 
    data.table::as.data.table() %>% 
    dplyr::group_by(
        dplyr::across(
            dplyr::all_of(group)
        )
    ) %>% 
    dplyr::summarize(n = dplyr::n())
#> Source: local data table [3 x 2]
#> Call:   `_DT1`[, .(n = .N), keyby = .(Species)]
#> 
#>   Species        n
#>   <fct>      <int>
#> 1 setosa        50
#> 2 versicolor    50
#> 3 virginica     50
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

@markfairbanks- the update did the trick, thanks.