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 = FALSE)
# 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
x
|
An R object. |
…
|
Additional arguments passed to the methods. |
keep_series
|
A logical value indicating whether to treat the object as a Series or
scalar value. If TRUE , the output is ensured to be a Series
literal even if the length of the object is 1 .
|
structify
|
A logical. If TRUE , convert multi-column expressions to a
single struct expression by calling pl$struct() . Otherwise
(default), done nothing.
|
as_lit
|
A logical value indicating whether to treat vector as literal values or
not. This argument is always set to TRUE when calling this
function from pl$lit() , and expects to return literal
values. See examples for details.
|
raw_as_binary
|
A logical value indicating whether to convert raw vector to a Binary
type scalar. If TRUE (default), the output is a Binary type
scalar instead of UInt8 type literal.
|
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")
#> cols(["a", "b"])
#> 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
#> cols(["a", "b"])
#> cols(["a", "b"]).as_struct()