tidyverse / dtplyr

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

Incorrect translation of data-frame-format dots in `summarise()` #454

Open owenjonesphe opened 1 year ago

owenjonesphe commented 1 year ago

In the Arguments section of help(dtplyr::summarise.dtplyr_step), the "dots" argument is explained in a similar way to dplyr::summarise():

... \<data-masking> Name-value pairs of summary functions. The name will be the name of the variable in the result. The value can be:

  • A vector of length 1, e.g. min(x)n(), or sum(is.na(y)).
  • A data frame, to add multiple columns from a single expression.

However, using a data frame within ... fails when summarising lazy_dt objects. The error message suggests that {data.table} is being asked to create a single column containing the entire data frame object, rather than multiple columns based on the columns of the data frame.

# tibble - works as expected
mtcars |> 
  dplyr::summarise(
    stats::quantile(mpg, probs = c(0.5, 0.9)) |> 
      tibble::as_tibble_row(),
    .by = cyl
  )
#>   cyl  50%   90%
#> 1   6 19.7 21.16
#> 2   4 26.0 32.40
#> 3   8 15.2 18.28

# lazy_dt - error when constructing `j`
mtcars |> 
  dtplyr::lazy_dt() |> 
  dplyr::summarise(
    stats::quantile(mpg, probs = c(0.5, 0.9)) |> 
      tibble::as_tibble_row(),
    .by = cyl
  )
#> Error in `[.data.table`(`_DT1`, , .(`tibble::as_tibble_row(stats::quantile(mpg, probs = c(0.5, 0.9)))` = tibble::as_tibble_row(stats::quantile(mpg, : All items in j=list(...) should be atomic vectors or lists. If you are trying something like j=list(.SD,newcol=mean(colA)) then use := by group instead (much quicker), or cbind or merge afterwards.

Created on 2023-11-02 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.1 (2023-06-16) #> os Amazon Linux 2 #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz UTC #> date 2023-11-02 #> pandoc 3.1.1 @ /usr/lib/rstudio-server/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.0) #> data.table 1.14.8 2023-02-17 [1] CRAN (R 4.3.1) #> digest 0.6.33 2023-07-07 [2] CRAN (R 4.3.1) #> dplyr 1.1.2 2023-04-20 [2] CRAN (R 4.3.1) #> dtplyr 1.3.1 2023-03-22 [1] CRAN (R 4.3.1) #> evaluate 0.21 2023-05-05 [2] CRAN (R 4.3.1) #> fansi 1.0.4 2023-01-22 [2] CRAN (R 4.3.1) #> fastmap 1.1.1 2023-02-24 [2] CRAN (R 4.3.1) #> fs 1.6.2 2023-04-25 [2] CRAN (R 4.3.1) #> generics 0.1.3 2022-07-05 [2] CRAN (R 4.3.1) #> glue 1.6.2 2022-02-24 [1] RSPM #> htmltools 0.5.5 2023-03-23 [2] CRAN (R 4.3.1) #> knitr 1.43 2023-05-25 [2] CRAN (R 4.3.1) #> lifecycle 1.0.3 2022-10-07 [1] RSPM #> magrittr 2.0.3 2022-03-30 [2] CRAN (R 4.3.1) #> pillar 1.9.0 2023-03-22 [2] CRAN (R 4.3.1) #> pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.3.1) #> purrr 1.0.1 2023-01-10 [2] CRAN (R 4.3.1) #> R.cache 0.16.0 2022-07-21 [2] CRAN (R 4.3.1) #> R.methodsS3 1.8.2 2022-06-13 [2] CRAN (R 4.3.1) #> R.oo 1.25.0 2022-06-12 [2] CRAN (R 4.3.1) #> R.utils 2.12.2 2022-11-11 [2] CRAN (R 4.3.1) #> R6 2.5.1 2021-08-19 [2] CRAN (R 4.3.1) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.3.0) #> rlang 1.1.1 2023-04-28 [1] RSPM #> rmarkdown 2.23 2023-07-01 [2] CRAN (R 4.3.1) #> rstudioapi 0.15.0 2023-07-07 [2] CRAN (R 4.3.1) #> sessioninfo 1.2.2 2021-12-06 [2] CRAN (R 4.3.1) #> styler 1.10.1 2023-06-05 [2] CRAN (R 4.3.1) #> tibble 3.2.1 2023-03-20 [2] CRAN (R 4.3.1) #> tidyselect 1.2.0 2022-10-10 [2] CRAN (R 4.3.1) #> utf8 1.2.3 2023-01-31 [2] CRAN (R 4.3.1) #> vctrs 0.6.2 2023-04-19 [1] RSPM #> withr 2.5.0 2022-03-03 [2] CRAN (R 4.3.1) #> xfun 0.39 2023-04-20 [2] CRAN (R 4.3.1) #> yaml 2.3.7 2023-01-23 [2] CRAN (R 4.3.1) #> #> [1] /home/owen10004/efs/R/x86_64-pc-linux-gnu-library/4.3 #> [2] /usr/local/lib64/R/site-library #> [3] /usr/local/lib64/R/library #> #> ────────────────────────────────────────────────────────────────────────────── ```