Skip to content

Commit 07d1126

Browse files
jp-darkaaronwolen
andcommitted
[r] Add functions for setting/getting global context (#4364)
* Add functions to set/get global default context * Update NEWS, docs, and version number * Add export to function * Update NEWS * Fix typo in check * Update get_context_default and docs * Fix typo * Add `replace` option to context and format file * Update docs and comment out deprecation test * Apply suggested documentation improvements Co-authored-by: Aaron Wolen <[email protected]> * Update documentation * Apply suggestions from code review Co-authored-by: Aaron Wolen <[email protected]> * Update documentation --------- Co-authored-by: Aaron Wolen <[email protected]> (cherry picked from commit 671a49f)
1 parent d6fb2ba commit 07d1126

33 files changed

+235
-37
lines changed

apis/r/NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ export(SparseReadIter)
8888
export(TableReadIter)
8989
export(TileDBCreateOptions)
9090
export(extract_dataset)
91+
export(get_default_context)
9192
export(list_datasets)
9293
export(load_dataset)
9394
export(matrixZeroBasedView)
95+
export(set_default_context)
9496
export(set_log_level)
9597
export(show_package_versions)
9698
export(soma_context)

apis/r/NEWS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# tiledbsoma 2.3.0
22

3+
## Added
4+
5+
- A new `SOMAContext` method was added that replaces `SOMATileDBContext`. ([#4355](https://github.com/single-cell-data/TileDB-SOMA/pull/4355))
6+
- New methods `set_default_context` and `get_default_context` for setting and getting the `SOMAContext`. ([#4364](https://github.com/single-cell-data/TileDB-SOMA/pull/4364))
7+
38
## Deprecated
49

510
- The function `soma_context` is deprecated. Use class `SOMAContext` instead. ([#4355](https://github.com/single-cell-data/TileDB-SOMA/pull/4355))
@@ -8,7 +13,6 @@
813

914
## Fixed
1015

11-
- The SOMA Context is only cached as an environment variable when the function `soma_context` is called directly. ([#4355](https://github.com/single-cell-data/TileDB-SOMA/pull/4355))
1216
- `SOMATileDBContext` no longer replaces `sm.mem.reader.sparse_global_order.ratio_array_data` when set in the input config. ([#4355](https://github.com/single-cell-data/TileDB-SOMA/pull/4355))
1317

1418
# tiledbsoma 2.2.0

apis/r/R/Factory.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
#' @param platform_config Optional platform configuration.
3131
#' @param tiledbsoma_ctx Optional (DEPRECATED) SOMATileDBContext.
3232
#' @param tiledb_timestamp Optional Datetime (POSIXct) for TileDB timestamp.
33-
#' @param context Optional TileDB SOMA context.
33+
#' @param context Optional \code{SOMAContext} object used for TileDB operations.
34+
#' If a context is not provided, then the default context will be used. Call
35+
#' \code{set_default_context} once before other SOMA operations to configure
36+
#' the default context.
3437
#'
3538
#' @return A new \link[tiledbsoma:SOMADataFrame]{SOMA data frame} stored at
3639
#' \code{uri} opened for writing.

apis/r/R/SOMAContext.R

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ SOMAContext <- R6::R6Class(
3333
get_data_protocol = function(uri) {
3434
return(get_data_protocol_from_soma_context(private$.handle, uri))
3535
}
36-
3736
),
3837
active = list(
3938
#' @field handle External pointer to the C++ interface
@@ -44,11 +43,73 @@ SOMAContext <- R6::R6Class(
4443
}
4544
return(private$.handle)
4645
}
47-
4846
),
4947
private = list(
5048
# @field Internal 'external pointer' for C++ interface ...
5149
#
5250
.handle = NULL
5351
)
5452
)
53+
54+
#' Set the Default Global Context
55+
#'
56+
#' Configure a default \code{\link{SOMAContext}} to be used by all TileDB-SOMA
57+
#' operations when no explicit context is provided.
58+
#'
59+
#' This function should be called once at the beginning of your session before
60+
#' opening any SOMA objects if you want to customize the TileDB context
61+
#' parameters that will apply to all subsequent operations. Otherwise, a default
62+
#' context will be created automatically with standard parameters when you first
63+
#' open a SOMA object.
64+
#'
65+
#' If the global context was already set, an error will be raised unless
66+
#' \code{replace=True}. Setting a new default context will not change the
67+
#' context for TileDB-SOMA objects that were already created.
68+
#'
69+
#' @template param-config
70+
#' @param replace Allow replacing the existing default context with new
71+
#' configuration parameters.
72+
#'
73+
#' @return Invisibly, the default default context object.
74+
#'
75+
#' @export
76+
#'
77+
set_default_context <- function(config = NULL, replace = FALSE) {
78+
if (!replace && !is.null(.pkgenv[["somactx"]])) {
79+
stop(
80+
"A default context was already created. To replace the default context for new objects call again with `replace=True`.",
81+
call. = FALSE
82+
)
83+
}
84+
context <- SOMAContext$new(config)
85+
.pkgenv[["somactx"]] <- context
86+
invisible(context)
87+
}
88+
89+
#' Get the Default SOMA Context
90+
#'
91+
#' Retrieve the current default \code{\link{SOMAContext}} used by TileDB-SOMA
92+
#' operations.
93+
#'
94+
#' This function returns the context that was either:
95+
#' \itemize{
96+
#' \item Explicitly set via \code{\link{set_default_context}}, or
97+
#' \item Automatically created when a SOMA object was first created
98+
#' }
99+
#'
100+
#' An error is raised if no default context is set.
101+
#'
102+
#' @return The context that will be used for TileDB-SOMA API when no context is provided by the user.
103+
#'
104+
#' @export
105+
#'
106+
get_default_context <- function() {
107+
context <- .pkgenv[["somactx"]]
108+
if (is.null(context)) {
109+
stop(
110+
"No default context is set. Call `set_default_context()` to initialize the context.",
111+
call. = FALSE
112+
)
113+
}
114+
return(context)
115+
}

apis/r/R/SOMAObject.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ SOMAObject <- R6::R6Class(
2222
#' @param tiledbsoma_ctx Optional (DEPRECATED) TileDB SOMA context
2323
#' @param tiledb_timestamp Optional timestamp (\code{\link[base]{POSIXct}})
2424
#' to open the object at
25-
#' @param context A \code{SOMAContext} object. Required internally but
26-
#' automatically provided by factory functions.
25+
#' @param context Optional \code{SOMAContext} object used for TileDB operations.
26+
#' If a context is not provided, then the default context will be used. Call
27+
#' \code{set_default_context} once before other SOMA operations to configure
28+
#' the default context.
2729
initialize = function(
2830
uri,
2931
...,

apis/r/R/datasets.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ extract_dataset <- function(name, dir = tempdir()) {
7474
#'
7575
#' @param tiledbsoma_ctx Optional (DEPRECATED) TileDB \dQuote{Context} object
7676
#' that defaults to \code{NULL}.
77-
#' @param context Optional SOMA context object that defaults to \code{NULL}.
77+
#' @param context Optional \code{SOMAContext} object used for TileDB operations.
78+
#' If a context is not provided, then the default context will be used. Call
79+
#' \code{set_default_context} once before other SOMA operations to configure
80+
#' the default context.
7881
#'
7982
#' @return \code{load_dataset()}: returns a SOMA object.
8083
#'

apis/r/R/soma_array_reader.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
#' @param result_order Character value with the desired result order, defaults to \sQuote{auto}
1818
#' @param loglevel Character value with the desired logging level, defaults to \sQuote{auto}
1919
#' which lets prior setting prevail, any other value is set as new logging level.
20-
#' @param context Optional instance of a SOMA Context object
20+
#' @param context Optional \code{SOMAContext} object used for TileDB operations.
21+
#' If a context is not provided, then the default context will be used. Call
22+
#' \code{set_default_context} once before other SOMA operations to configure
23+
#' the default context.
2124
#' @return A List object with two pointers to Arrow array data and schema is returned
2225
#' @examples
2326
#' \dontrun{

apis/r/R/utils.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ get_soma_context <- function(context, tiledbsoma_ctx, what = NULL) {
7272
if (is.null(tiledbsoma_ctx)) {
7373
context <- .pkgenv[["somactx"]]
7474
if (is.null(context)) {
75-
return(SOMAContext$new())
76-
} else {
77-
return(context)
75+
return(set_default_context())
7876
}
77+
return(context)
7978
}
8079
if (!inherits(x = tiledbsoma_ctx, what = 'SOMATileDBContext')) {
8180
stop(

apis/r/R/write_soma.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
#' @param platform_config Optional \link[tiledbsoma:PlatformConfig]{platform
1111
#' configuration}.
1212
#' @param tiledbsoma_ctx Optional (DEPRECATED) \code{\link{SOMATileDBContext}}.
13-
#' @param context Optional \code{\link{SOMAContext}}.
13+
#' @param context Optional \code{SOMAContext} object used for TileDB operations.
14+
#' If a context is not provided, then the default context will be used. Call
15+
#' \code{set_default_context} once before other SOMA operations to configure
16+
#' the default context.
1417
#'
1518
#' @return The URI to the resulting \code{\link{SOMAExperiment}} generated from
1619
#' the data contained in \code{x}.

apis/r/man/SOMACollectionCreate.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)