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

211 [.teal_data S3 method #346

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ Collate:
'join_keys.R'
'teal.data.R'
'teal_data-class.R'
'teal_data-constructor.R'
'teal_data-datanames.R'
'teal_data-extract.R'
'teal_data-get_code.R'
'teal_data-show.R'
'teal_data.R'
'testhat-helpers.R'
'topological_sort.R'
'verify.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
3 changes: 3 additions & 0 deletions R/teal_data.R → R/teal_data-constructor.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#'
#' @export
#'
#' @name teal_data
#' @rdname teal_data
#'
m7pr marked this conversation as resolved.
Show resolved Hide resolved
m7pr marked this conversation as resolved.
Show resolved Hide resolved
#' @examples
#' teal_data(x1 = iris, x2 = mtcars)
#'
Expand Down
49 changes: 49 additions & 0 deletions R/teal_data-extract.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#'
#' @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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this is inherited from [.qenv but usually subset uses i argument to reuse the generic base extractor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but with i it works for numeric input, and we only support character input :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then in my opinion, the natural way to handle this would be to convert the numeric input to names and use that internally, easier for the user and simple for the developer. But it can also provide an informative error message to users. Users will try to use numbers given that this is what usually R accepts. Sorry, to raise a new thing, could you add a test for numeric subsetting and NA_character_? I know we get the Assertion error, but I want to make sure this is tested/documented for users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@llrs-roche oh yeah, but we don't want to introduce anything for numeric as qenv is basically an environment that is not subsettable for numeric as it does not have a natural order (like atomic vectors have).

library(teal.code)
q <- qenv()
q <- eval_code(q, "a <- 1; b <- 2")
q@env[1]
# > Error in q@env[1] : object of type 'environment' is not subsettable
q@env[[1]]
# > Error in q@env[[1]] : wrong arguments for subsetting an environment
q@env[["a"]]

Numbers are fine for atomic vectors, or simple S3 objects that naturally use indexes, but I don't think it is the case for the environmemnt. We actually don't want it specifically as we only allow extraction by name, not by numeric index, so that you specifically need to provide the name of the object. With numeric input you can get a different element, depending on the current state of the object : )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, to raise a new thing, could you add a test for numeric subsetting and NA_character_? I know we get the Assertion error, but I want to make sure this is tested/documented for users.

No worries, good idea : )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are extended tests 5f17af0

#' @param x (`teal_data`)
m7pr marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @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
m7pr marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @export
`[.teal_data` <- function(x, names) {
checkmate::assert_class(names, "character")
m7pr marked this conversation as resolved.
Show resolved Hide resolved
names_in_env <- intersect(names, ls(get_env(x)))
if (!length(names_in_env)) {
return(teal_data())
}
# From ?NextMethod
# To pass `names` to `NextMethod` - it looks like it needs to be called `names`
# and created in the environment of the function that calls `NextMehod`.

# NextMethod works by creating a special call frame for the next method. If no new arguments are supplied, the
# arguments will be the same in number, order and name as those to the current method but their values will be
# promises to evaluate their name in the current method and environment. Any named arguments matched to ... are
# handled specially: they either replace existing arguments of the same name or are appended to the argument list.
# They are passed on as the promise that was supplied as an argument to the current environment.
# (S does this differently!) If they have been evaluated in the current (or a previous environment) they remain
# evaluated. (This is a complex area, and subject to change: see the draft ‘R Language Definition’.)

names <- names_in_env
x <- NextMethod("`[`", x)
m7pr marked this conversation as resolved.
Show resolved Hide resolved
x@join_keys <- x@join_keys[names_in_env]

x
}
28 changes: 27 additions & 1 deletion man/teal_data.Rd

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

Loading