From efcd0d159d78ec3c26121aaeaf2f9721ac519430 Mon Sep 17 00:00:00 2001 From: go_gonzo Date: Fri, 30 Aug 2024 10:20:50 +0200 Subject: [PATCH] return ls(@env) if @datanames not specified --- R/join_keys.R | 1 - R/teal_data-class.R | 2 -- R/teal_data-datanames.R | 20 ++++++++++++++------ man/datanames.Rd | 6 ++---- tests/testthat/test-datanames.R | 6 ++++++ 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/R/join_keys.R b/R/join_keys.R index 1798a46af..47fb97836 100644 --- a/R/join_keys.R +++ b/R/join_keys.R @@ -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 } diff --git a/R/teal_data-class.R b/R/teal_data-class.R index 3015797b9..9a7e59611 100644 --- a/R/teal_data-class.R +++ b/R/teal_data-class.R @@ -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, diff --git a/R/teal_data-datanames.R b/R/teal_data-datanames.R index d32727859..57c6f024a 100644 --- a/R/teal_data-datanames.R +++ b/R/teal_data-datanames.R @@ -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` @@ -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 @@ -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 }) diff --git a/man/datanames.Rd b/man/datanames.Rd index 4908979a2..b484c0e7a 100644 --- a/man/datanames.Rd +++ b/man/datanames.Rd @@ -26,10 +26,8 @@ Get or set the value of the \code{datanames} slot. } \details{ The \verb{@datanames} slot in a \code{teal_data} object specifies which of the variables stored in its environment -(the \verb{@env} slot) are data sets to be taken into consideration. -The contents of \verb{@datanames} can be specified upon creation and default to all variables in \verb{@env}. -Variables created later, which may well be data sets, are not automatically considered such. -Use this function to update the slot. +(the \verb{@env} slot) are data sets to be taken into consideration. If not set explicitly, then \code{\link[=datanames]{datanames()}} returns +all variable names from \verb{@env}. } \examples{ td <- teal_data(iris = iris) diff --git a/tests/testthat/test-datanames.R b/tests/testthat/test-datanames.R index e0c99bb63..83b936746 100644 --- a/tests/testthat/test-datanames.R +++ b/tests/testthat/test-datanames.R @@ -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)