GlareDB table is a class that has a struct similar to arrow::Table innerly and
can be converted from/to other classes via nanoarrow::as_nanoarrow_array_stream()
.
Usage
as_glaredb_table(x, ...)
# Default S3 method
as_glaredb_table(x, ..., schema = NULL)
# S3 method for class 'nanoarrow_array_stream'
as_glaredb_table(x, ...)
# S3 method for class 'RGlareDbExecutionOutput'
as_glaredb_table(x, ...)
Arguments
- x
An object to be coerced to a GlareDB table.
- ...
Additional arguments passed to methods.
- schema
An optional schema used to enforce conversion to a particular type. Defaults to
infer_nanoarrow_schema()
.
Details
The default method of as_glaredb_table()
calls nanoarrow::as_nanoarrow_array_stream()
internally, and all arguments are passed to it.
Examples
con <- glaredb_connect()
# Create a GlareDB table from a data frame with a specified schema
dat <- data.frame(a = 1:3, b = letters[1:3]) |>
as_glaredb_table(
schema = nanoarrow::na_struct(
list(
a = nanoarrow::na_int64(),
b = nanoarrow::na_large_string()
)
)
)
# Run an SQL query against the connection,
# and convert the result to a GlareDB table
glaredb_sql("SELECT * FROM dat", con) |>
as_glaredb_table()
#> ┌───────┬───────────┐
#> │ a │ b │
#> │ ── │ ── │
#> │ Int64 │ LargeUtf8 │
#> ╞═══════╪═══════════╡
#> │ 1 │ a │
#> │ 2 │ b │
#> │ 3 │ c │
#> └───────┴───────────┘
# Convert the GlareDB table to an R data frame
dat |>
as.data.frame()
#> a b
#> 1 1 a
#> 2 2 b
#> 3 3 c
# Convert the GlareDB table to an arrow Table
if (requireNamespace("arrow", quietly = TRUE)) {
dat |>
arrow::as_arrow_table()
}
#> Table
#> 3 rows x 2 columns
#> $a <int64>
#> $b <large_string>
# Convert the GlareDB table to a polars DataFrame
if (requireNamespace("polars", quietly = TRUE)) {
dat |>
polars::as_polars_df()
}
#> shape: (3, 2)
#> ┌─────┬─────┐
#> │ a ┆ b │
#> │ --- ┆ --- │
#> │ i64 ┆ str │
#> ╞═════╪═════╡
#> │ 1 ┆ a │
#> │ 2 ┆ b │
#> │ 3 ┆ c │
#> └─────┴─────┘