R/n-col.R

Defines functions mat_n_col df_n_col ncol

# Copied from dplyr, can be reexported with dplyr > 1.1.4

# Masks `ncol()` to avoid accidentally materializing ALTREP duckplyr
# data frames.
ncol <- function(x) {
  abort("Use `df_n_col()` or `mat_n_col()` instead.")
}

# Alternative to `ncol()` which avoids `dim()`.
#
# `dim()` also requires knowing the number of rows,
# which forces ALTREP duckplyr data frames to materialize.
#
# This function makes the same assertion as vctrs about data frame structure,
# i.e. if `x` inherits from `"data.frame"`, then it is a VECSXP with length
# equal to the number of columns.
df_n_col <- function(x) {
  x <- unclass(x)
  obj_check_list(x)
  length(x)
}

# In a few places we call `ncol()` on matrices, and in those
# cases we want to continue using the base version.
mat_n_col <- function(x) {
  base::ncol(x)
}
duckdblabs/duckplyr documentation built on Nov. 6, 2024, 10 p.m.