Skip to content

Commit

Permalink
[r] Add four add_new_* setters to CollectionBase (#1091)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddelbuettel authored Mar 12, 2023
1 parent 59b6cae commit b21f68e
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 2 deletions.
44 changes: 43 additions & 1 deletion apis/r/R/SOMACollectionBase.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ SOMACollectionBase <- R6::R6Class(
#'
#' @param uri URI of the TileDB group
#' @param platform_config Optional storage-engine specific configuration
#' @param ctx optional tiledb context
#' @param ctx Optional TileDB context
initialize = function(uri, platform_config = NULL, ctx = NULL) {
super$initialize(uri, platform_config, ctx)
},
Expand Down Expand Up @@ -42,6 +42,48 @@ SOMACollectionBase <- R6::R6Class(
#' @returns SOMA object.
get = function(name) {
super$get(name)
},

#' @description Add a new SOMA collection to this collection. (lifecycle: experimental)
#' @param object SOMA collection object.
#' @param key The key to be added.
add_new_collection = function(object, key) {
# TODO: Check that object is a collection
super$set(object, key)
},

#' @description Add a new SOMA dataframe to this collection. (lifecycle: experimental)
#' @param key The key to be added.
#' @param schema Arrow schema argument passed on to DataFrame$create()
#' @param index_column_names Index column names passed on to DataFrame$create()
add_new_dataframe = function(key, schema, index_column_names) {
## TODO: Check argument validity
## TODO: platform_config ?
ndf <- SOMADataFrame$new( file.path(self$uri, key) )
ndf$create(schema, index_column_names)
super$set(ndf, key)
},

#' @description Add a new SOMA DenseNdArray to this collection. (lifecycle: experimental)
#' @param key The key to be added.
#' @param type an [Arrow type][arrow::data-type] defining the type of each
#' element in the array.
#' @param shape a vector of integers defining the shape of the array.
add_new_dense_ndarray = function(key, type, shape) {
ndarr <- SOMADenseNDArray$new( file.path(self$uri, key) )
ndarr$create(type, shape)
super$set(ndarr, key)
},

#' @description Add a new SOMA SparseNdArray to this collection. (lifecycle: experimental)
#' @param key The key to be added.
#' @param type an [Arrow type][arrow::data-type] defining the type of each
#' element in the array.
#' @param shape a vector of integers defining the shape of the array.
add_new_sparse_ndarray = function(key, type, shape) {
ndarr <- SOMASparseNDArray$new( file.path(self$uri, key) )
ndarr$create(type, shape)
super$set(ndarr, key)
}

),
Expand Down
4 changes: 4 additions & 0 deletions apis/r/man/SOMACollection.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 89 additions & 1 deletion apis/r/man/SOMACollectionBase.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions apis/r/man/SOMAExperiment.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions apis/r/man/SOMAMeasurement.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions apis/r/tests/testthat/test-SOMACollection.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,28 @@ test_that("SOMACollection basics", {

readback_dataframe <- readback_collection$get("sdf")
expect_is(readback_dataframe, "SOMADataFrame")

# Add the collection to the collection
collection$add_new_collection(collection, "collection")
collection2 <- collection$get("collection")
expect_true(collection2$soma_type == "SOMACollection")
expect_true(collection2$exists())
df2 <- collection2$get("sdf")
## -- uri differs by "./" so cannot compare R6 objects directly
expect_equal(readback_dataframe$object[], df2$object[])

# Add new dataframe to the collection
collection$add_new_dataframe("new_df", create_arrow_schema(), "foo")
df3 <- collection$get("new_df")
expect_true(df3$soma_type == "SOMADataFrame")

# Add new DenseNdArray to the collection
collection$add_new_dense_ndarray("nd_d_arr", arrow::int32(), shape = c(10, 5))
arr <- collection$get("nd_d_arr")
expect_true(arr$soma_type == "SOMADenseNDArray")

# Add new SparseNdArray to the collection
collection$add_new_sparse_ndarray("nd_s_arr", arrow::int32(), shape = c(10, 5))
arr <- collection$get("nd_s_arr")
expect_true(arr$soma_type == "SOMASparseNDArray")
})

0 comments on commit b21f68e

Please sign in to comment.