A builder plugin that translates dplyr pipe chains to data.table syntax at build time.
Write idiomatic dplyr in your source files, get idiomatic data.table in your package: it uses builder to translate the code at build time.
🔴 This is a simple text repalcement (no AST parsing) so it's not perfect, consider this a proof of concept.
In your srcr/ source files, wrap dplyr pipe chains with
#> fassplyr / #> endfassplyr markers:
library(data.table)
m <- mtcars
#> fassplyr
m |>
dplyr::group_by(cyl) |>
dplyr::summarise(mean_mpg = mean(mpg)) |>
dplyr::arrange(dplyr::desc(mean_mpg))
#> endfassplyrAfter running builder, the output in R/ becomes:
library(data.table)
m <- mtcars
data.table::as.data.table(m)[, .(mean_mpg = mean(mpg)), by = .(cyl)][order(-mean_mpg)]Add the plugin to your builder.ini:
plugin: fassplyr::pluginOr pass it to the CLI with the -plugin fassplyr::plugin.
| dplyr | data.table |
|---|---|
dplyr::filter(cond1, cond2) |
[cond1 & cond2] |
dplyr::select(col1, col2) |
[, .(col1, col2)] |
dplyr::mutate(a = expr) |
[, :=(a = expr)] |
dplyr::summarise(a = expr) |
[, .(a = expr)] |
dplyr::arrange(x, dplyr::desc(y)) |
[order(x, -y)] |
dplyr::group_by(g) |
by = .(g) on the next verb |
dplyr::slice(1, 3) |
[c(1, 3)] |
dplyr::distinct(col1, col2) |
unique(...[, .(col1, col2)]) |
dplyr::n() is replaced with .N.
dplyr::desc(x) is replaced with -x inside arrange.
Both n() and desc() also work without the dplyr:: prefix.
summarize (American spelling) is accepted as an alias for summarise.
- Only the
|>(native) pipe is supported - Verbs must use the
dplyr::prefix — bare verbs likefilter()are not supported - Unrecognized verbs produce a warning and are passed through as comments
This is a proof of concept. The following are not handled:
%>%(magrittr pipe)- tidyselect helpers (
starts_with(),everything(),where(), etc.) - Negative selection in
select() across()/c_across()ungroup().data$pronoun- Joins (
left_join,inner_join, etc.)