Skip to content

Commit

Permalink
set safely
Browse files Browse the repository at this point in the history
  • Loading branch information
gogonzo committed Jul 31, 2024
1 parent ee389a5 commit f6be311
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion R/join_keys.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ join_keys.teal_data <- function(...) {
#' join_keys(td)
`join_keys<-.teal_data` <- function(x, value) {
join_keys(x@join_keys) <- value
x@datanames <- sort_datanames(x@datanames, x@join_keys)
datanames(x) <- x@datanames # datanames fun manages some exceptions
x
}

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

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

methods::new(
"teal_data",
Expand Down
12 changes: 7 additions & 5 deletions R/teal_data-datanames.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,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 <- value
x@datanames <- sort_datanames(x@datanames, x@join_keys)
x@datanames <- .get_sorted_datanames(datanames = value, join_keys = x@join_keys, env = x@env)
methods::validObject(x)
x
})
Expand All @@ -54,13 +53,16 @@ setMethod("datanames<-", signature = c("qenv.error", "character"), definition =


#' @keywords internal
sort_datanames <- function(datanames, joinkeys) {
.get_sorted_datanames <- function(datanames, join_keys, env) {
child_parent <- sapply(
datanames,
function(name) parent(joinkeys, name),
function(name) parent(join_keys, name),
USE.NAMES = TRUE,
simplify = FALSE
)

union(unlist(topological_sort(child_parent)), datanames)
union(
intersect(unlist(topological_sort(child_parent)), ls(env)),
datanames
)
}

0 comments on commit f6be311

Please sign in to comment.