Skip to content

Return an expression representing a literal value

Description

This function is a shorthand for as_polars_expr(x, as_lit = TRUE) and in most cases, the actual conversion is done by as_polars_series().

Usage

pl$lit(value, dtype = NULL)

Arguments

value An R object. Passed as the x param of as_polars_expr().
dtype A polars data type or NULL (default). If not NULL, casted to the specified data type.

Value

A polars expression

See Also

  • as_polars_series(): R -\> Polars type mapping is mostly defined by this function.
  • as_polars_expr(): Internal implementation of pl$lit().

Examples

library("polars")

# Literal scalar values
pl$lit(1L)
#> 1
pl$lit(5.5)
#> 5.5
pl$lit(NULL)
#> null
pl$lit("foo_bar")
#> "foo_bar"
# Generally, for a vector (an R object) becomes a Series with length 1,
# it is converted to a Series and then get the first value to become a scalar literal.
pl$lit(as.Date("2021-01-20"))
#> 2021-01-20
pl$lit(as.POSIXct("2023-03-31 10:30:45"))
#> 2023-03-31 10:30:45
pl$lit(data.frame(a = 1, b = "foo"))
#> {1.0,"foo"}
# Literal Series data
pl$lit(1:3)
#> Series[literal]
pl$lit(pl$Series("x", 1:3))
#> Series[x]