Create a Polars expression from an R object
Description
The as_polars_expr()
function creates a polars expression
from various R objects. This function is used internally by various
polars functions that accept expressions. In most cases, users should
use pl$lit()
instead of this function, which is a shorthand
for as_polars_expr(x, as_lit = TRUE)
. (In other words, this
function can be considered as an internal implementation to realize the
lit
function of the Polars API in other languages.)
Usage
as_polars_expr(x, ...)
# Default S3 method:
as_polars_expr(x, ..., keep_series = FALSE)
# S3 method for class 'polars_expr'
as_polars_expr(x, ..., structify = deprecated())
# S3 method for class 'character'
as_polars_expr(x, ..., as_lit = FALSE)
# S3 method for class 'raw'
as_polars_expr(x, ..., raw_as_binary = TRUE)
# S3 method for class ''NULL''
as_polars_expr(x, ...)
Arguments
Details
Because R objects are typically mapped to Series, this function often
calls as_polars_series()
internally. However, unlike R,
Polars has scalars of length 1, so if an R object is converted to a
Series of length 1, this function get the first value of the Series and
convert it to a scalar literal. If you want to implement your own
conversion from an R class to a Polars object, define an S3 method for
as_polars_series()
instead of this function.
Default S3 method
Create a Series by calling as_polars_series()
and then
convert that Series to an Expr. If the length of the Series is
1
, it will be converted to a scalar value.
Additional arguments …
are passed to
as_polars_series()
.
S3 method for character
If the as_lit
argument is FALSE
(default),
this function will call pl$col()
and the character vector
is treated as column names. Otherwise, the default method is called.
S3 method for raw
If the raw_as_binary
argument is TRUE
(default), the raw vector is converted to a Binary type scalar.
Otherwise, the default method is called.
S3 method for NULL
NULL
is converted to a Null type null
literal.
Value
A polars expression
See Also
-
as_polars_series()
: R -\> Polars type mapping is mostly defined by this function.
Examples
library("polars")
# character
# as_lit = FALSE (default)
as_polars_expr("a") # Same as `pl$col("a")`
#> col("a")
#> cs.by_name('a', 'b', require_all=true)
#> Series[literal]
#> "a"
#> null
#> Series[literal]
#> b"\x01"
#> 1
#> b"foo"
#> Series[literal]
#> null
#> Series[literal]
#> 1
#> null
#> Series[literal]
#> Series[literal]
#> 1.0
#> null
#> Series[literal]
#> Series[literal]
#> [1.0]
#> Series[literal]
#> Series[literal]
#> 2021-01-01
#> Series[literal]
#> 1.0.alias("")
#> cs.by_name('a', 'b', require_all=true)