-
Notifications
You must be signed in to change notification settings - Fork 118
duckplyr 1.0.0 #724
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
duckplyr 1.0.0 #724
Changes from 57 commits
96861b7
5b2b472
97739a8
1d80f56
11e21c1
eb09f2b
44b7c66
20b6a6e
4ccfa70
692b390
0df0d53
4251569
e30f959
1cec2cb
b276db4
7c92ddb
d8aca67
480551b
6adf074
c955826
1c5153f
78ee84b
1b55a30
ae87276
ae8c8e6
7661ad3
48eba60
8b6d0bd
e25fb07
a0b9b39
b90e8af
5ac6f6e
14ec2f6
b9a277a
5762c0a
6aaf953
f847736
c78073f
1f898c0
5a1f22c
2b4b421
d97b031
4be5ea9
f344b9f
a13315a
ad9825f
a734638
fc8122d
3211710
eea955a
4a20ca3
f5e4a38
20dff03
f231c68
49b4f8b
6b84b25
452d5f2
21c2b74
a21daa5
9022d7f
f0563c0
88846a3
17cdfc3
9e0e496
a5ac341
36a93cb
4f88bbd
ad8866a
beee540
5fe00ac
a186621
a994038
ac86b3a
4d6fa0d
0582a15
735e9d9
217144a
dc07389
663eda2
dd9d20d
0008ac8
ea3fda0
9128e16
eaee543
931fd45
5bb1a84
78729ac
0ef4ace
0f23a74
236d793
5bc98a6
cf61e47
bfac020
d0ed8f9
51931cb
1ddb0e1
85b98cd
58997b1
29f8d0c
af72705
9fbb466
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| --- | ||
| output: hugodown::hugo_document | ||
|
|
||
| slug: duckplyr-1-0-0 | ||
| title: duckplyr fully joins the tidyverse! | ||
| date: 2025-02-13 | ||
| author: Kirill Müller and Maëlle Salmon | ||
| description: > | ||
| duckplyr 1.0.0 is on CRAN and part of the tidyverse! | ||
| A drop-in replacement for dplyr, powered by DuckDB for speed. | ||
| It is the most dplyr-like of dplyr backends. | ||
|
|
||
| photo: | ||
| url: https://www.pexels.com/photo/a-mallard-duck-on-water-6918877/ | ||
| author: Kiril Gruev | ||
|
|
||
| # one of: "deep-dive", "learn", "package", "programming", "roundup", or "other" | ||
| categories: [package] | ||
| tags: | ||
| - duckplyr | ||
| - dplyr | ||
| - tidyverse | ||
| --- | ||
|
|
||
| <!-- | ||
| TODO: | ||
| * [x] Look over / edit the post's title in the yaml | ||
| * [x] Edit (or delete) the description; note this appears in the Twitter card | ||
| * [x] Pick category and tags (see existing with `hugodown::tidy_show_meta()`) | ||
| * [x] Find photo & update yaml metadata | ||
| * [x] Create `thumbnail-sq.jpg`; height and width should be equal | ||
| * [x] Create `thumbnail-wd.jpg`; width should be >5x height | ||
| * [x] `hugodown::use_tidy_thumbnails()` | ||
| * [x] Add intro sentence, e.g. the standard tagline for the package | ||
| * [x] `usethis::use_tidy_thanks()` | ||
| --> | ||
|
|
||
| We're very chuffed to announce the release of [duckplyr](https://duckplyr.tidyverse.org) 1.0.0. | ||
| duckplyr is a drop-in, fully compatible replacement for dplyr, powered by [DuckDB](https://duckdb.org/) for speed. | ||
maelle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| It joins the rank of dplyr backends together with [dtplyr](https://dtplyr.tidyverse.org) and [dbplyr](https://dbplyr.tidyverse.org). | ||
| You can use it instead of dplyr for data small or large. | ||
|
|
||
| <!-- FIXME: | ||
|
|
||
| We have many more dplyr backends, the two above are just from the tidyverse. | ||
|
||
| GitHub search: https://github.com/search?q=org%3Acran+%2FS3method%5B%28%5D%28mutate%7Csummarise%29+*%2C%2F&type=code | ||
| Do we need an "awesome dplyr" like https://github.com/krlmlr/awesome-vctrs/? | ||
|
|
||
| --> | ||
|
|
||
| You can install it from CRAN with: | ||
|
|
||
| ```{r, eval = FALSE} | ||
| install.packages("duckplyr") | ||
|
||
| ``` | ||
|
|
||
| In this article, we'll show how duckplyr can help you with data of different size, explain how you can help improve the package, and ... . | ||
|
|
||
| ## A drop-in replacement for dplyr | ||
|
|
||
| The duckplyr package is a _drop-in replacement for dplyr_ that uses _DuckDB for speed_. | ||
|
||
| You can simply _drop_ duckplyr into your pipeline by loading it, then computations will be efficiently carried out by DuckDB. | ||
| DuckDB is a fast in-memory analytical database system[^duckdb]. | ||
|
|
||
| [^duckdb]: If you haven't heard about it, you can watch [Hannes Mühleisen's keynote at posit::conf(2024)](https://www.youtube.com/watch?v=GELhdezYmP0&feature=youtu.be). | ||
|
|
||
|
|
||
| ```{r} | ||
| library(conflicted) | ||
| library(duckplyr) | ||
| conflict_prefer("filter", "dplyr", quiet = TRUE) | ||
| library(babynames) | ||
|
|
||
| out <- babynames |> | ||
| mutate(prevalence = if_else(prop >= 0.01, "frequent", "rare")) |> | ||
| summarize( | ||
| .by = c(sex, year, prevalence), | ||
| babies_n = sum(n) | ||
| ) |> | ||
| filter(sex == "F") | ||
| class(out) | ||
| out | ||
| ``` | ||
|
|
||
| Like with other dplyr backends like dtplyr and dbplyr, duckplyr allows you to get faster results. | ||
| Unlike other dplyr backends, duckplyr does not require you to learn a different syntax. | ||
maelle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| The duckplyr package is fully compatible with dplyr: if an operation cannot be carried out with DuckDB, it is automatically outsourced to dplyr. | ||
| Over time, we expect fewer and fewer fallbacks to dplyr to be needed. | ||
|
|
||
| ## How to use duckplyr | ||
|
|
||
| To _replace_ dplyr with duckplyr, you can: | ||
|
|
||
| - Load duckplyr and then keep your pipeline as is. Calling `library(duckplyr)` overwrites dplyr methods, enabling duckplyr for the entire session no matter how data.frames are created. | ||
| This is shown in the example above. | ||
|
|
||
| - Create individual "duck frames" using _conversion functions_ like `duckdb_tibble()` or `as_duckdb_tibble()`, or _ingestion functions_ like `read_csv_duckdb()`. | ||
|
|
||
|
|
||
| Then, the data manipulation pipeline uses the exact same syntax as a dplyr pipeline. | ||
| The duckplyr package performs the computation using DuckDB. | ||
|
|
||
| ```{r} | ||
| # Undo the effect of library(duckplyr) | ||
| methods_restore() | ||
|
||
|
|
||
| out <- babynames |> | ||
| as_duckdb_tibble() |> | ||
| mutate(prevalence = if_else(prop >= 0.01, "frequent", "rare")) |> | ||
| summarize( | ||
| .by = c(sex, year, prevalence), | ||
| babies_n = sum(n) | ||
| ) |> | ||
| filter(sex == "F") | ||
| class(out) | ||
| ``` | ||
|
|
||
| In both cases, printing the result only shows the first few rows, as with dbplyr. | ||
|
|
||
| ```{r} | ||
| out | ||
| ``` | ||
|
|
||
| The result can finally be materialized to memory, or computed temporarily, or computed to a file. | ||
|
|
||
| ```{r} | ||
| # to memory | ||
| nrow(out) | ||
| # or for instance collect(out) | ||
maelle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # to a file | ||
| csv_file <- withr::local_tempfile() | ||
| compute_csv(out, csv_file) | ||
| fs::file_size(csv_file) | ||
| ``` | ||
|
|
||
| When duckplyr itself does not support specific functionality, it falls back to dplyr. | ||
| For instance, filtering on grouped data is not supported yet, still it works thanks to the fallback mechanism. | ||
|
|
||
| ```{r} | ||
| babynames |> | ||
| filter(n > 10000, .by = "name") | ||
maelle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| For performance reasons, the output order of the result is not guaranteed to be stable. | ||
| If you need a stable order, you can use `arrange()` or force output order stability by setting an environment variable. | ||
| This and other limitations are documented in [`vignette("limits")`](https://duckplyr.tidyverse.org/articles/limits.html). | ||
|
|
||
maelle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ## Large data | ||
|
|
||
| For large data, duckplyr is a legitimate alternative to dtplyr and dbplyr. | ||
maelle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| With large datasets, you want: | ||
|
|
||
| - input data in an efficient format, like Parquet files, which duckplyr allows thanks to its ingestion functions like `read_parquet_duckdb()`. | ||
| - efficient computation, which duckplyr provides via DuckDB's holistic optimization, without your having to use another syntax than dplyr. | ||
| - the output to not clutter all the memory, which duckplyr supports through two features: | ||
| - computation to files using [`compute_parquet()`](https://duckplyr.tidyverse.org/reference/compute_file.html) or [`compute_csv()`](https://duckplyr.tidyverse.org/reference/compute_file.html). | ||
| - the control of automatic materialization (collection of results into memory). You can disable automatic materialization completely or, as a compromise, disable it up to a certain output size. See [`vignette("prudence")`](https://duckplyr.tidyverse.org/articles/prudence.html) for details. | ||
|
|
||
| See [`vignette("large")`](https://duckplyr.tidyverse.org/articles/large.html) for a walkthrough and more details. | ||
|
|
||
| A drawback of analyzing large data with duckplyr is that the limitations of duckplyr won't be compensated by fallbacks, since fallbacks to dplyr necessitate putting data into memory. | ||
| Therefore, if your pipeline encounters fallbacks, you might want to work around them by converting the duck frame into a table through `compute()` then running SQL code through the experimental `read_sql_duckdb()` function. | ||
|
||
| Again, over time, we expect more native support for dplyr functionality. | ||
|
|
||
| ```{r} | ||
| data <- | ||
| duckdb_tibble(a = 2) |> | ||
| mutate(b = 3) | ||
|
|
||
| computed_data <- | ||
| data |> | ||
| compute(name = "computed_data") | ||
|
|
||
| sql_data <- | ||
| read_sql_duckdb("SELECT *, a * b AS c FROM computed_data") | ||
|
|
||
| sql_data | ||
| ``` | ||
|
|
||
| ## Help us improve duckplyr! | ||
|
|
||
| Our goals for future development of duckplyr include: | ||
|
|
||
| - Enabling users to provide [custom translations](https://github.com/tidyverse/duckplyr/issues/158) of dplyr functionality; | ||
| - Making it easier to contribute code to duckplyr; | ||
| - Supporting more dplyr and tidyr functionality natively in DuckDB. | ||
|
|
||
| You can help! | ||
|
|
||
| - Please report any issues, especially regarding unknown incompabilities. See [`vignette("limits")`](https://duckplyr.tidyverse.org/articles/limits.html). | ||
| - Contribute to the codebase after reading duckplyr's [contributing guide](https://duckplyr.tidyverse.org/CONTRIBUTING.html). | ||
| - Turn on telemetry to help us hear about the most frequent fallbacks so we can prioritize working on the corresponding missing dplyr translation. See [`vignette("telemetry")`](https://duckplyr.tidyverse.org/articles/telemetry.html) and the [`duckplyr::fallback_sitrep()`](https://duckplyr.tidyverse.org/reference/fallback.html) function. | ||
|
|
||
| ## Acknowledgements and additional resources | ||
|
|
||
| A big thanks to all folks who filed issues, created PRs and generally helped to improve duckplyr! | ||
|
|
||
| <!-- FIXME: Can we use_tidy_thanks also for the duckdb repo?, and perhaps merge the two? --> | ||
|
|
||
| [@adamschwing](https://github.com/adamschwing), [@andreranza](https://github.com/andreranza), [@apalacio9502](https://github.com/apalacio9502), [@apsteinmetz](https://github.com/apsteinmetz), [@barracuda156](https://github.com/barracuda156), [@beniaminogreen](https://github.com/beniaminogreen), [@bob-rietveld](https://github.com/bob-rietveld), [@brichards920](https://github.com/brichards920), [@cboettig](https://github.com/cboettig), [@davidjayjackson](https://github.com/davidjayjackson), [@DavisVaughan](https://github.com/DavisVaughan), [@Ed2uiz](https://github.com/Ed2uiz), [@eitsupi](https://github.com/eitsupi), [@era127](https://github.com/era127), [@etiennebacher](https://github.com/etiennebacher), [@eutwt](https://github.com/eutwt), [@fmichonneau](https://github.com/fmichonneau), [@hadley](https://github.com/hadley), [@hannes](https://github.com/hannes), [@hawkfish](https://github.com/hawkfish), [@IndrajeetPatil](https://github.com/IndrajeetPatil), [@JanSulavik](https://github.com/JanSulavik), [@JavOrraca](https://github.com/JavOrraca), [@jeroen](https://github.com/jeroen), [@jhk0530](https://github.com/jhk0530), [@joakimlinde](https://github.com/joakimlinde), [@JosiahParry](https://github.com/JosiahParry), [@larry77](https://github.com/larry77), [@lnkuiper](https://github.com/lnkuiper), [@lorenzwalthert](https://github.com/lorenzwalthert), [@luisDVA](https://github.com/luisDVA), [@maelle](https://github.com/maelle), [@math-mcshane](https://github.com/math-mcshane), [@meersel](https://github.com/meersel), [@multimeric](https://github.com/multimeric), [@mytarmail](https://github.com/mytarmail), [@nicki-dese](https://github.com/nicki-dese), [@PMassicotte](https://github.com/PMassicotte), [@prasundutta87](https://github.com/prasundutta87), [@rafapereirabr](https://github.com/rafapereirabr), [@Robinlovelace](https://github.com/Robinlovelace), [@romainfrancois](https://github.com/romainfrancois), [@sparrow925](https://github.com/sparrow925), [@stefanlinner](https://github.com/stefanlinner), [@thomasp85](https://github.com/thomasp85), [@TimTaylor](https://github.com/TimTaylor), [@Tmonster](https://github.com/Tmonster), [@toppyy](https://github.com/toppyy), [@wibeasley](https://github.com/wibeasley), [@yjunechoe](https://github.com/yjunechoe), [@ywhcuhk](https://github.com/ywhcuhk), and [@zhjx19](https://github.com/zhjx19). | ||
|
|
||
| Special thanks to Joe Thorley ([@joethorley](https://github.com/joethorley)) for help with choosing the right words. | ||
|
|
||
| Eager to learn more about duckplyr -- beside by trying it out yourself? | ||
| The pkgdown website of duckplyr features several [articles](https://duckplyr.tidyverse.org/articles/). | ||
| Furthermore, the blog post ["duckplyr: dplyr Powered by DuckDB"](https://duckdb.org/2024/04/02/duckplyr.html) by Hannes Mühleisen provides some context on duckplyr including its inner workings, as also seen in a [section](https://blog.r-hub.io/2025/02/13/lazy-meanings/#duckplyr-lazy-evaluation-and-prudence) of the R-hub blog post ["Lazy introduction to laziness in R"](https://blog.r-hub.io/2025/02/13/lazy-meanings/) by Maëlle Salmon, Athanasia Mo Mowinckel and Hannah Frick. | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working on that part, duckdb 1.2.0 is about to be released tomorrow.