Skip to content

Commit

Permalink
[.teal_data S3 method
Browse files Browse the repository at this point in the history
  • Loading branch information
m7pr committed Oct 28, 2024
1 parent eca9695 commit 967c1c5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Collate:
'teal.data.R'
'teal_data-class.R'
'teal_data-datanames.R'
'teal_data-extract.R'
'teal_data-get_code.R'
'teal_data-show.R'
'teal_data.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method("[",join_keys)
S3method("[",teal_data)
S3method("[<-",join_keys)
S3method("[[<-",join_keys)
S3method("join_keys<-",join_keys)
Expand Down
38 changes: 38 additions & 0 deletions R/teal_data-extract.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#'
#' @section Subsetting:
#' `x[names]` subsets objects in `teal_data` environment and limit the code to the necessary needed to build limited
#' objects.
#'
#' @param names (`character`) names of objects included in `teal_subset` to subset
#' @param x (`teal_data`)
#'
#' @examples
#'
#' # Subsetting
#' data <- teal_data()
#' data <- eval_code(data, "a <- 1;b<-2")
#' data["a"]
#' data[c("a", "b")]
#'
#' join_keys(data) <- join_keys(join_key("a", "b", "x"))
#' join_keys(data["a"]) # should show empty keys
#' join_keys(data["b"])
#' join_keys(data)["a"] # should show empty keys
#' join_keys(data)["b"]
#'
#' @rdname teal_data
#'
#' @export
`[.teal_data` <- function(x, names) {
checkmate::assert_class(names, "character")
names_in_env <- intersect(names, ls(get_env(x)))
if (!length(names_in_env)) {
return(teal_data())
}

subset_qenv <- getFromNamespace("[.qenv", "teal.code")
x <- subset_qenv(x, names_in_env)
x@join_keys <- x@join_keys[names_in_env]

x
}
30 changes: 28 additions & 2 deletions man/teal_data.Rd

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

0 comments on commit 967c1c5

Please sign in to comment.