Skip to content

Commit

Permalink
return ls(@env) if @datanames not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
gogonzo committed Aug 30, 2024
1 parent 6f9fd0f commit efcd0d1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
1 change: 0 additions & 1 deletion R/join_keys.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ join_keys.teal_data <- function(...) {
#' join_keys(td)
`join_keys<-.teal_data` <- function(x, value) {
join_keys(x@join_keys) <- value
datanames(x) <- x@datanames # datanames fun manages some exceptions
x
}

Expand Down
2 changes: 0 additions & 2 deletions R/teal_data-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ new_teal_data <- function(data,
new_env <- rlang::env_clone(list2env(data), parent = parent.env(.GlobalEnv))
lockEnvironment(new_env, bindings = TRUE)

datanames <- .get_sorted_datanames(datanames = datanames, join_keys = join_keys, env = new_env)

methods::new(
"teal_data",
env = new_env,
Expand Down
20 changes: 14 additions & 6 deletions R/teal_data-datanames.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
#' Get or set the value of the `datanames` slot.
#'
#' The `@datanames` slot in a `teal_data` object specifies which of the variables stored in its environment
#' (the `@env` slot) are data sets to be taken into consideration.
#' The contents of `@datanames` can be specified upon creation and default to all variables in `@env`.
#' Variables created later, which may well be data sets, are not automatically considered such.
#' Use this function to update the slot.
#' (the `@env` slot) are data sets to be taken into consideration. If not set explicitly, then [datanames()] returns
#' all variable names from `@env`.
#'
#' @param x (`teal_data`) object to access or modify
#' @param value (`character`) new value for `@datanames`; all elements must be names of variables existing in `@env`
Expand All @@ -31,7 +29,17 @@
#' @export
setGeneric("datanames", function(x) standardGeneric("datanames"))
setMethod("datanames", signature = "teal_data", definition = function(x) {
x@datanames
datanames <- if (length(x@datanames)) {
x@datanames
} else {
ls(teal.code::get_env(x), all.names = TRUE)
}
# sort and add parent if exists
.get_sorted_datanames(
datanames = datanames,
join_keys = join_keys(x),
env = teal.code::get_env(x)
)
})
setMethod("datanames", signature = "qenv.error", definition = function(x) {
NULL
Expand All @@ -42,7 +50,7 @@ setMethod("datanames", signature = "qenv.error", definition = function(x) {
setGeneric("datanames<-", function(x, value) standardGeneric("datanames<-"))
setMethod("datanames<-", signature = c("teal_data", "character"), definition = function(x, value) {
checkmate::assert_subset(value, names(x@env))
x@datanames <- .get_sorted_datanames(datanames = value, join_keys = x@join_keys, env = x@env)
x@datanames <- value
methods::validObject(x)
x
})
Expand Down
6 changes: 2 additions & 4 deletions man/datanames.Rd

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

6 changes: 6 additions & 0 deletions tests/testthat/test-datanames.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ testthat::test_that("variables not in @datanames are omitted", {
testthat::expect_identical(datanames(td), c("i", "m"))
})

testthat::test_that("datanames() returns all object names from @env if @datasets not specified", {
td <- teal_data(i = iris, m = mtcars)
td <- within(td, f <- faithful)
testthat::expect_identical(datanames(td), c("i", "m"))
})

# set ----
testthat::test_that("datanames can set value of @datanames", {
td <- teal_data(i = iris, m = mtcars)
Expand Down

0 comments on commit efcd0d1

Please sign in to comment.