blob: ae558873f5ea7866fff1c7dc984edc8af332ab4f [file] [log] [blame]
<!---
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
# Creating Arrow Objects {#creating-arrow-objects}
## Create an Arrow Array from an R object
You want to convert an existing vector in R to an Arrow Array object.
### Solution
```{r, array_create}
# Create an example vector
score = c(99, 97, 99)
# Convert to Arrow Array
score_array <- Array$create(score)
# View Array
score_array
```
```{r, test_array_create, opts.label = "test"}
test_that("array_create works as expected", {
expect_s3_class(score_array, "Array")
expect_identical(as.vector(score_array), score)
})
```
## Create a Arrow Table from an R object
You want to convert an existing data frame in R to an Arrow Table object.
### Solution
```{r, table_create}
# Create an example data frame
my_tibble <- tibble::tibble(group = c("A", "B", "C"), score = c(99, 97, 99))
# Convert to Arrow Table
my_table <- arrow_table(my_tibble)
# View table
my_table
```
```{r, test_table_create, opts.label = "test"}
test_that("table_create works as expected", {
expect_s3_class(my_table, "Table")
expect_identical(dplyr::collect(my_table), my_tibble)
})
```
## View the contents of an Arrow Table or RecordBatch
You want to view the contents of an Arrow Table or RecordBatch.
### Solution
```{r, table_collect}
# View Table
dplyr::collect(my_table)
```
```{r, test_table_collect, opts.label = "test"}
test_that("table_collect works as expected", {
expect_identical(dplyr::collect(my_table), my_tibble)
})
```
## Manually create a RecordBatch from an R object.
You want to convert an existing data frame in R to an Arrow RecordBatch object.
### Solution
```{r, record_batch_create}
# Create an example data frame
my_tibble <- tibble::tibble(group = c("A", "B", "C"), score = c(99, 97, 99))
# Convert to Arrow RecordBatch
my_record_batch <- record_batch(my_tibble)
# View RecordBatch
my_record_batch
```
```{r, test_record_batch_create, opts.label = "test"}
test_that("record_batch_create works as expected", {
expect_s3_class(my_record_batch, "RecordBatch")
expect_identical(dplyr::collect(my_record_batch), my_tibble)
})
```