diff --git a/DESCRIPTION b/DESCRIPTION index 25d9bb0c3..0cbeefdbb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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' diff --git a/NAMESPACE b/NAMESPACE index 71db8957b..603ad524e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/teal_data-extract.R b/R/teal_data-extract.R new file mode 100644 index 000000000..fb9c856b6 --- /dev/null +++ b/R/teal_data-extract.R @@ -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 +} diff --git a/man/teal_data.Rd b/man/teal_data.Rd index b249aece4..0ac641980 100644 --- a/man/teal_data.Rd +++ b/man/teal_data.Rd @@ -1,12 +1,19 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/teal_data.R -\name{teal_data} +% Please edit documentation in R/teal_data-extract.R, R/teal_data.R +\name{[.teal_data} +\alias{[.teal_data} \alias{teal_data} \title{Comprehensive data integration function for \code{teal} applications} \usage{ +\method{[}{teal_data}(x, names) + teal_data(..., join_keys = teal.data::join_keys(), code = character(0), check) } \arguments{ +\item{x}{(\code{teal_data})} + +\item{names}{(\code{character}) names of objects included in \code{teal_subset} to subset} + \item{...}{any number of objects (presumably data objects) provided as \code{name = value} pairs.} \item{join_keys}{(\code{join_keys} or single \code{join_key_set}) @@ -27,7 +34,26 @@ A \code{teal_data} object. Universal function to pass data to teal application. } +\section{Subsetting}{ + +\code{x[names]} subsets objects in \code{teal_data} environment and limit the code to the necessary needed to build limited +objects. +} + \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"] + teal_data(x1 = iris, x2 = mtcars) }