Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return ls(@env) if @datanames not specified #330

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading