Skip to content

Commit

Permalink
chore: run air format .
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Dec 21, 2024
1 parent 749e3fc commit 6979b1c
Show file tree
Hide file tree
Showing 157 changed files with 17,987 additions and 16,839 deletions.
65 changes: 34 additions & 31 deletions R/arrange.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,40 @@
#' arrange(-x2, .by_group = TRUE)

arrange.RPolarsDataFrame <- function(.data, ..., .by_group = FALSE) {

grps <- attributes(.data)$pl_grps
mo <- attributes(data)$maintain_grp_order
is_grouped <- !is.null(grps)

attr(.data, "called_from_arrange") <- TRUE

polars_exprs <- translate_dots(
.data,
...,
env = rlang::current_env(),
caller = rlang::caller_env()
)

descending <- vapply(polars_exprs, function(x) {
attr(x, "descending") %||% FALSE
}, FUN.VALUE = logical(1L))

if (is_grouped && isTRUE(.by_group)) {
polars_exprs <- c(grps, polars_exprs)
descending <- c(rep(FALSE, length(grps)), descending)
}

out <- if (is_grouped) {
.data$sort(polars_exprs, descending = descending) |>
group_by(all_of(grps), maintain_order = mo)
} else {
.data$sort(polars_exprs, descending = descending)
}

add_tidypolars_class(out)
grps <- attributes(.data)$pl_grps
mo <- attributes(data)$maintain_grp_order
is_grouped <- !is.null(grps)

attr(.data, "called_from_arrange") <- TRUE

polars_exprs <- translate_dots(
.data,
...,
env = rlang::current_env(),
caller = rlang::caller_env()
)

descending <- vapply(
polars_exprs,
function(x) {
attr(x, "descending") %||% FALSE
},
FUN.VALUE = logical(1L)
)

if (is_grouped && isTRUE(.by_group)) {
polars_exprs <- c(grps, polars_exprs)
descending <- c(rep(FALSE, length(grps)), descending)
}

out <- if (is_grouped) {
.data$sort(polars_exprs, descending = descending) |>
group_by(all_of(grps), maintain_order = mo)
} else {
.data$sort(polars_exprs, descending = descending)
}

add_tidypolars_class(out)
}

#' @export
Expand Down
9 changes: 5 additions & 4 deletions R/as_tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
#' filter(Sepal.Length > 6) |>
#' as_tibble()
as_tibble.tidypolars <- function(
x,
int64_conversion = polars::polars_options()$int64_conversion,
...) {
dplyr::as_tibble(as.data.frame(x, int64_conversion = int64_conversion, ...))
x,
int64_conversion = polars::polars_options()$int64_conversion,
...
) {
dplyr::as_tibble(as.data.frame(x, int64_conversion = int64_conversion, ...))
}

#' @export
Expand Down
190 changes: 101 additions & 89 deletions R/bind.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#' bind_rows_polars(p1 = p1, p2 = p2, .id = "id")

bind_rows_polars <- function(..., .id = NULL) {
concat_(..., how = "diagonal_relaxed", .id = .id)
concat_(..., how = "diagonal_relaxed", .id = .id)
}

#' Append multiple Data/LazyFrames next to each other
Expand Down Expand Up @@ -61,103 +61,115 @@ bind_rows_polars <- function(..., .id = NULL) {
# bind_* functions are not generics: https://github.com/tidyverse/dplyr/issues/6905

bind_cols_polars <- function(
...,
.name_repair = "unique"
) {
arg_match0(.name_repair, values = c("unique", "universal", "check_unique", "minimal"))
concat_(..., how = "horizontal", .name_repair = .name_repair)
...,
.name_repair = "unique"
) {
arg_match0(
.name_repair,
values = c("unique", "universal", "check_unique", "minimal")
)
concat_(..., how = "horizontal", .name_repair = .name_repair)
}

concat_ <- function(..., how, .id = NULL, .name_repair = NULL) {
dots <- rlang::list2(...)
if (length(dots) == 1 && rlang::is_bare_list(dots[[1]])) {
dots <- dots[[1]]
}
dots <- rlang::list2(...)
if (length(dots) == 1 && rlang::is_bare_list(dots[[1]])) {
dots <- dots[[1]]
}

all_df <- all(vapply(dots, inherits, "RPolarsDataFrame", FUN.VALUE = logical(1L)))
all_lf <- all(vapply(dots, inherits, "RPolarsLazyFrame", FUN.VALUE = logical(1L)))
all_df <- all(
vapply(dots, inherits, "RPolarsDataFrame", FUN.VALUE = logical(1L))
)
all_lf <- all(
vapply(dots, inherits, "RPolarsLazyFrame", FUN.VALUE = logical(1L))
)

if (!(all_df || all_lf)) {
rlang::abort(
"All elements in `...` must be of the same class (either all Polars DataFrames or all Polars LazyFrames).",
call = caller_env()
)
}
if (!(all_df || all_lf)) {
rlang::abort(
"All elements in `...` must be of the same class (either all Polars DataFrames or all Polars LazyFrames).",
call = caller_env()
)
}

if (!is.null(.id)) {
all_named <- !is.null(names(dots)) && all(names(dots) != "")
dots <- lapply(seq_along(dots), \(x) {
val <- if (all_named) {
names(dots)[x]
} else {
as.character(x)
}
dots[[x]]$
with_columns(pl$lit(val)$alias(.id))$
select(pl$col(.id), pl$col("*")$exclude(.id))
})
}
if (!is.null(.id)) {
all_named <- !is.null(names(dots)) && all(names(dots) != "")
dots <- lapply(seq_along(dots), \(x) {
val <- if (all_named) {
names(dots)[x]
} else {
as.character(x)
}
dots[[x]]$with_columns(pl$lit(val)$alias(.id))$select(
pl$col(.id),
pl$col("*")$exclude(.id)
)
})
}

out <- switch(
how,
"horizontal" = {
all_names <- unlist(lapply(dots, names), use.names = FALSE)
if (anyDuplicated(all_names) > 0) {
dupes <- get_dupes(all_names)
out <- switch(
how,
"horizontal" = {
all_names <- unlist(lapply(dots, names), use.names = FALSE)
if (anyDuplicated(all_names) > 0) {
dupes <- get_dupes(all_names)

if (.name_repair == "check_unique") {
msg <- make_dupes_msg(dupes)
rlang::abort(
c(
"Names must be unique.",
"x" = "These names are duplicated (`variable` (locations)):",
" " = msg
),
call = caller_env()
)
} else if (.name_repair == "minimal") {
rlang::abort(
c(
"Argument `.name_repair = \"minimal\"` doesn't work on Polars Data/LazyFrames.",
"i" = "Either provide unique names or use `.name_repair = \"universal\"`."
),
call = caller_env()
)
} else {
# default with quiet = FALSE is *incredibly* slow (more than 60ms)
# so I print the message myself
new_names <- vctrs::vec_as_names(all_names, repair = .name_repair, quiet = TRUE)
mapping <- as.list(new_names)
names(mapping) <- all_names
if (.name_repair == "check_unique") {
msg <- make_dupes_msg(dupes)
rlang::abort(
c(
"Names must be unique.",
"x" = "These names are duplicated (`variable` (locations)):",
" " = msg
),
call = caller_env()
)
} else if (.name_repair == "minimal") {
rlang::abort(
c(
"Argument `.name_repair = \"minimal\"` doesn't work on Polars Data/LazyFrames.",
"i" = "Either provide unique names or use `.name_repair = \"universal\"`."
),
call = caller_env()
)
} else {
# default with quiet = FALSE is *incredibly* slow (more than 60ms)
# so I print the message myself
new_names <- vctrs::vec_as_names(
all_names,
repair = .name_repair,
quiet = TRUE
)
mapping <- as.list(new_names)
names(mapping) <- all_names

# make the message myself
tmp <- mapping[!names(mapping) %in% all_names]
msg <- paste0("`", tmp, "`", " -> ", "`", names(tmp), "`")
names(msg) <- rep("*", length(msg))
rlang::inform(msg)
# make the message myself
tmp <- mapping[!names(mapping) %in% all_names]
msg <- paste0("`", tmp, "`", " -> ", "`", names(tmp), "`")
names(msg) <- rep("*", length(msg))
rlang::inform(msg)

for (i in seq_along(dots)) {
n_names <- ncol(dots[[i]])
dots[[i]] <- dots[[i]]$rename(mapping[1:n_names])
mapping <- mapping[-(1:n_names)]
}
}
}
for (i in seq_along(dots)) {
n_names <- ncol(dots[[i]])
dots[[i]] <- dots[[i]]$rename(mapping[1:n_names])
mapping <- mapping[-(1:n_names)]
}
}
}

if (inherits(dots[[1]], "RPolarsDataFrame")) {
pl$concat(dots, how = how)
} else {
if (length(dots) > 2) {
rlang::abort(
"`bind_cols_polars()` doesn't work with more than two LazyFrames.",
call = caller_env()
)
}
dots[[1]]$with_context(dots[[2]])$select(pl$all())
}
},
# default
pl$concat(dots, how = how)
)
add_tidypolars_class(out)
if (inherits(dots[[1]], "RPolarsDataFrame")) {
pl$concat(dots, how = how)
} else {
if (length(dots) > 2) {
rlang::abort(
"`bind_cols_polars()` doesn't work with more than two LazyFrames.",
call = caller_env()
)
}
dots[[1]]$with_context(dots[[2]])$select(pl$all())
}
},
# default
pl$concat(dots, how = how)
)
add_tidypolars_class(out)
}
Loading

0 comments on commit 6979b1c

Please sign in to comment.