-
-
Notifications
You must be signed in to change notification settings - Fork 8
Change qenv as environment "type" -- adds names(qenv/qenv.error), get() and $ S3 methods
#218
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
Merged
Merged
Changes from 52 commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
bfa8390
feat: initial support for names(teal_data)
averissimo 127bc7c
docs: update NEWS
averissimo cb2cc57
docs: minor change
averissimo 72f0595
fix: warning in R CMD check
averissimo 9a6343a
docs: typo
averissimo e1a69b3
fix: remove implementation of names()<- as error message is self
averissimo c335f52
fix: remove extra arguments for names, not supported
averissimo 873a1b6
fix: remove extra word from wordlist
averissimo a203f81
feat: `qenv` inherits from environment class
averissimo fd93704
Merge branch 'main' into 333_deprecate_datanames@main
averissimo 8b855f3
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] 4efca24
fix: improves tests and adds $ getter to qenv.error with similar beha…
averissimo 0a02726
feat: prevent assignment to qenv.error
averissimo 281c994
doc: update news
averissimo b1ffbe7
fix: problem when printing qenv.error
averissimo e457a4c
docs: adds .xData slot documentation
averissimo 2c76a8a
chore: cleanup of previous implementation of names.qenv
averissimo ab9e342
chore: lintr cleanup
averissimo c56a5ca
feat: expand on compatibility with an environment
averissimo 9904570
fix: complete tests for qenv-class
averissimo 5c0ef7d
doc: adds section to qenv constructor
averissimo 558bd72
fix: test and adds extra protection on qenv validation
averissimo 4ec3e9f
fix: move constructor logic to "initialize" method of qenv
averissimo e00fd92
fix: problem with integer (1L) shorthand in within
averissimo be480f1
test: problem with integer (1L) shorthand in within
averissimo b7d1885
fix: order and formal of callNextMethod
averissimo 2a32022
fix: minor bugs
averissimo 9049379
chore: fix lintr
averissimo bb5c5fb
Apply suggestions from code review
averissimo 709265f
docs: update
averissimo 1fe8b18
docs: small improvements
averissimo 3ae0541
[skip style] [skip vbump] Restyle files
github-actions[bot] 0a64498
Update tests/testthat/test-qenv_within.R
averissimo d4ee6d0
docs: implements @gogonzo suggestions and cleans up docs
averissimo af7054b
docs: move section around
averissimo c668d98
docs: superseded
averissimo faa843b
fix: use newlines in code parseing on multiline expression with within
averissimo 2f553eb
Merge branch 'main' into 333_deprecate_datanames@main
averissimo 015f11c
fix: problems with check
averissimo 7cd8949
chore: rename instances of ls to names
averissimo 50be4b0
chore: rename instances of join to c
averissimo 9d2ec00
docs: improvement on join() documentation
averissimo c76e148
Apply suggestions from code review
averissimo 620849b
pr: apply suggestions
averissimo 1e25681
pr: apply suggestions (remove duplicate test)
averissimo 7b7ae6a
Update R/qenv-join.R
averissimo 7ea19e3
fix: error with suggestion
averissimo bf5ed47
fix: tests
averissimo 903a43c
feat: qenv constructor improvement
averissimo f9fef18
Update tests/testthat/test-qenv-class.R
averissimo 3bd3ff5
[skip style] [skip vbump] Restyle files
github-actions[bot] d8d1a8e
chore: trigger CI
averissimo 236ce59
Update README.md
averissimo 4564f34
Update R/qenv-get_env.R
averissimo 9f76e48
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] 49e49c9
fix: remove unnecessary listing
averissimo 1f4b755
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] a5fcb9a
chore: trigger CI
averissimo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| #' If two `qenv` can be joined | ||
| #' | ||
| #' Checks if two `qenv` objects can be combined. | ||
| #' For more information, please see [`join`] | ||
| #' @param x (`qenv`) | ||
| #' @param y (`qenv`) | ||
| #' @return `TRUE` if able to join or `character` used to print error message. | ||
| #' @keywords internal | ||
| .check_joinable <- function(x, y) { | ||
| checkmate::assert_class(x, "qenv") | ||
| checkmate::assert_class(y, "qenv") | ||
|
|
||
| common_names <- intersect(rlang::env_names([email protected]), rlang::env_names([email protected])) | ||
| is_overwritten <- vapply(common_names, function(el) { | ||
| !identical(get(el, [email protected]), get(el, [email protected])) | ||
| }, logical(1)) | ||
| if (any(is_overwritten)) { | ||
| return( | ||
| paste( | ||
| "Not possible to join qenv objects if anything in their environment has been modified.\n", | ||
| "Following object(s) have been modified:\n - ", | ||
| paste(common_names[is_overwritten], collapse = "\n - ") | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| shared_ids <- intersect(x@id, y@id) | ||
| if (length(shared_ids) == 0) { | ||
| return(TRUE) | ||
| } | ||
|
|
||
| shared_in_x <- match(shared_ids, x@id) | ||
| shared_in_y <- match(shared_ids, y@id) | ||
|
|
||
| # indices of shared ids should be 1:n in both slots | ||
| if (identical(shared_in_x, shared_in_y) && identical(shared_in_x, seq_along(shared_ids))) { | ||
| TRUE | ||
| } else if (!identical(shared_in_x, shared_in_y)) { | ||
| paste( | ||
| "The common shared code of the qenvs does not occur in the same position in both qenv objects", | ||
| "so they cannot be joined together as it's impossible to determine the evaluation's order.", | ||
| collapse = "" | ||
| ) | ||
| } else { | ||
| paste( | ||
| "There is code in the qenv objects before their common shared code", | ||
| "which means these objects cannot be joined.", | ||
| collapse = "" | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| #' @rdname join | ||
| #' @param ... (`qenv` or `qenv.error`). | ||
| #' @examples | ||
| #' q <- qenv() | ||
| #' q1 <- within(q, { | ||
| #' iris1 <- iris | ||
| #' mtcars1 <- mtcars | ||
| #' }) | ||
| #' q1 <- within(q1, iris2 <- iris) | ||
| #' q2 <- within(q1, mtcars2 <- mtcars) | ||
| #' qq <- c(q1, q2) | ||
| #' cat(get_code(qq)) | ||
| #' | ||
| #' @export | ||
| c.qenv <- function(...) { | ||
| dots <- rlang::list2(...) | ||
| if (!checkmate::test_list(dots[-1], types = c("qenv", "qenv.error"))) { | ||
| return(NextMethod(c, dots[[1]])) | ||
| } | ||
|
|
||
| first_non_qenv_ix <- which.min(vapply(dots, inherits, what = "qenv", logical(1))) | ||
| if (first_non_qenv_ix > 1) { | ||
| return(dots[[first_non_qenv_ix]]) | ||
| } | ||
|
|
||
| Reduce( | ||
| x = dots[-1], | ||
| init = dots[[1]], | ||
| f = function(x, y) { | ||
| join_validation <- .check_joinable(x, y) | ||
|
|
||
| # join expressions | ||
| if (!isTRUE(join_validation)) { | ||
| stop(join_validation) | ||
| } | ||
|
|
||
| id_unique <- !y@id %in% x@id | ||
| x@id <- c(x@id, y@id[id_unique]) | ||
| x@code <- c(x@code, y@code[id_unique]) | ||
| x@warnings <- c(x@warnings, y@warnings[id_unique]) | ||
| x@messages <- c(x@messages, y@messages[id_unique]) | ||
|
|
||
| # insert (and overwrite) objects from y to x | ||
| [email protected] <- rlang::env_clone([email protected], parent = parent.env(.GlobalEnv)) | ||
| rlang::env_coalesce(env = [email protected], from = [email protected]) | ||
| x | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| #' @rdname join | ||
| #' @export | ||
| c.qenv.error <- function(...) { | ||
| rlang::list2(...)[[1]] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| #' @name qenv-class | ||
| #' @rdname qenv-class | ||
| #' @slot code (`character`) representing code necessary to reproduce the environment | ||
| #' @slot env (`environment`) environment which content was generated by the evaluation | ||
| #' @slot .xData (`environment`) environment with content was generated by the evaluation | ||
| #' of the `code` slot. | ||
| #' @slot id (`integer`) random identifier of the code element to make sure uniqueness | ||
| #' when joining. | ||
|
|
@@ -14,11 +14,60 @@ | |
| #' @exportClass qenv | ||
| setClass( | ||
| "qenv", | ||
| slots = c(env = "environment", code = "character", id = "integer", warnings = "character", messages = "character"), | ||
| prototype = list( | ||
| env = new.env(parent = parent.env(.GlobalEnv)), code = character(0), id = integer(0), | ||
| warnings = character(0), messages = character(0) | ||
| ) | ||
| slots = c( | ||
| code = "character", | ||
| id = "integer", | ||
| warnings = "character", | ||
| messages = "character" | ||
| ), | ||
| contains = "environment" | ||
| ) | ||
|
|
||
| #' It initializes the `qenv` class | ||
| #' @noRd | ||
| setMethod( | ||
| "initialize", | ||
| "qenv", | ||
| function(.Object, # nolint: object_name. | ||
| .xData, # nolint: object_name. | ||
| code = character(0L), | ||
| warnings = rep("", length(code)), | ||
| messages = rep("", length(code)), | ||
| id = integer(0L), | ||
| ...) { | ||
| # # Pre-process parameters to ensure they are ready to be used by parent constructors | ||
| stopifnot("`code` must be a character or language object." = any(is.language(code), is.character(code))) | ||
|
|
||
| if (is.language(code)) { | ||
| code <- paste(lang2calls(code), collapse = "\n") | ||
| } | ||
| if (length(code)) { | ||
| code <- paste(code, collapse = "\n") | ||
| } | ||
|
|
||
| if (length(id) == 0L) { | ||
| id <- sample.int(.Machine$integer.max, size = length(code)) | ||
| } | ||
|
|
||
| new_xdata <- if (rlang::is_missing(.xData)) { | ||
| new.env(parent = parent.env(.GlobalEnv)) | ||
| } else { | ||
| checkmate::assert_environment(.xData) | ||
| rlang::env_clone(.xData, parent = parent.env(.GlobalEnv)) | ||
| } | ||
| lockEnvironment(new_xdata, bindings = TRUE) | ||
|
|
||
| # .xData needs to be unnamed as the `.environment` constructor allows at | ||
| # most 1 unnamed formal argument of class `environment`. | ||
| # See methods::findMethods("initialize")$.environment | ||
| .Object <- methods::callNextMethod( # nolint: object_name. | ||
| # Mandatory use of `xData` to build a correct [email protected] | ||
| .Object, new_xdata, | ||
| code = code, messages = messages, warnings = warnings, id = id, ... | ||
| ) | ||
averissimo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| .Object | ||
| } | ||
| ) | ||
|
|
||
| #' It takes a `qenv` class and returns `TRUE` if the input is valid | ||
|
|
@@ -33,6 +82,8 @@ setValidity("qenv", function(object) { | |
| "@code and @messages slots must have the same length" | ||
| } else if (any(duplicated(object@id))) { | ||
| "@id contains duplicated values." | ||
| } else if (!environmentIsLocked([email protected])) { | ||
| "@.xData must be locked." | ||
| } else { | ||
| TRUE | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,10 @@ | ||
| # needed to handle try-error | ||
| setOldClass("qenv.error") | ||
|
|
||
| #' @export | ||
| as.list.qenv.error <- function(x, ...) { | ||
| stop(errorCondition( | ||
| list(message = conditionMessage(x)), | ||
| class = c("validation", "try-error", "simpleError") | ||
| )) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| #' Access environment included in `qenv` | ||
| #' | ||
| #' The access of environment included in `qenv@env` allows to e.g. list object names included in `qenv@env` slot. | ||
| #' The access of environment included in the `qenv` that contains all data objects. | ||
| #' | ||
| #' @param object (`qenv`) | ||
| #' @param object (`qenv`). | ||
| #' | ||
| #' @return An `environment` stored in `qenv@env` slot. | ||
| #' @return An `environment` stored in `qenv` slot with all data objects. | ||
| #' | ||
| #' @examples | ||
| #' q <- qenv() | ||
|
|
@@ -13,7 +13,8 @@ | |
| #' b <- data.frame(x = 1:10) | ||
| #' }) | ||
| #' get_env(q1) | ||
| #' ls(get_env(q1)) | ||
| #' | ||
| #' names(get_env(q1)) # list objects in the environment | ||
averissimo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #' | ||
| #' @aliases get_env,qenv-method | ||
| #' @aliases get_env,qenv.error-method | ||
|
|
@@ -23,10 +24,6 @@ setGeneric("get_env", function(object) { | |
| standardGeneric("get_env") | ||
| }) | ||
|
|
||
| setMethod("get_env", "qenv", function(object) { | ||
| object@env | ||
| }) | ||
| setMethod("get_env", "qenv", function(object) [email protected]) | ||
|
|
||
| setMethod("get_env", "qenv.error", function(object) { | ||
| object | ||
| }) | ||
| setMethod("get_env", "qenv.error", function(object) object) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.