Skip to content

Check if the object is a polars object

Description

Functions to check if the object is a polars object. is_ functions return TRUE of FALSE depending on the class of the object. check_ functions throw an informative error if the object is not the correct class. Suffixes are corresponding to the polars object classes:

  • \*\_dtype: For polars data types.
  • \*\_df: For polars data frames.
  • \*\_expr: For polars expressions.
  • \*\_lf: For polars lazy frames.
  • \*\_selector: For polars selectors.
  • \*\_series: For polars series.

Usage

is_polars_dtype(x)

is_polars_df(x)

is_polars_expr(x, ...)

is_polars_lf(x)

is_polars_selector(x, ...)

is_polars_series(x)

is_list_of_polars_dtype(x, n = NULL)

check_polars_dtype(
  x,
  ...,
  allow_null = FALSE,
  arg = caller_arg(x),
  call = caller_env()
)

check_polars_df(
  x,
  ...,
  allow_null = FALSE,
  arg = caller_arg(x),
  call = caller_env()
)

check_polars_expr(
  x,
  ...,
  allow_null = FALSE,
  arg = caller_arg(x),
  call = caller_env()
)

check_polars_lf(
  x,
  ...,
  allow_null = FALSE,
  arg = caller_arg(x),
  call = caller_env()
)

check_polars_selector(
  x,
  ...,
  allow_null = FALSE,
  arg = caller_arg(x),
  call = caller_env()
)

check_polars_series(
  x,
  ...,
  allow_null = FALSE,
  arg = caller_arg(x),
  call = caller_env()
)

check_list_of_polars_dtype(
  x,
  ...,
  allow_null = FALSE,
  arg = caller_arg(x),
  call = caller_env()
)

Arguments

x An object to check.
Arguments passed to rlang::abort().
n Expected length of a vector.
allow_null If TRUE, NULL is allowed as a valid input.
arg An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem.
call The execution environment of a currently running function, e.g. caller_env(). The function will be mentioned in error messages as the source of the error. See the call argument of abort() for more information.

Details

check_polars_* functions are derived from the standalone-types-check functions from the rlang package (Can be installed with usethis::use_standalone(“r-lib/rlang”, file = “types-check”)).

Value

  • is_polars\_\* functions return TRUE or FALSE.
  • check_polars\_\* functions return NULL invisibly if the input is valid.

See Also

  • infer_polars_dtype(): Check if the object can be converted to a Series.

Examples

library("polars")

is_polars_df(as_polars_df(mtcars))
#> [1] TRUE
is_polars_df(mtcars)
#> [1] FALSE
# Use `check_polars_*` functions in a function
# to ensure the input is a polars object
sample_func <- function(x) {
  check_polars_df(x)
  TRUE
}

sample_func(as_polars_df(mtcars))
#> [1] TRUE
try(sample_func(mtcars))
#> Error in sample_func(mtcars) : 
#>   `x` must be a polars data frame, not a <data.frame> object.