-
-
Notifications
You must be signed in to change notification settings - Fork 10
127 introduce within.qenv
#149
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 all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
7a944a7
define within method for qenv class
0a1beaa
add unit tests
34378c7
add documentation
ed9991a
Merge 34378c769e29a7cf270a720b9bd862600a429490 into 41f660b0b8ffdd543…
chlebowa 23a21af
[skip actions] Restyle files
github-actions[bot] 3a23bb1
trigger
bfc738f
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] cd259af
trigger
ff3b6b7
amend NEWS
e6823a2
fix typo
1e77c71
update pkgdown
beefbc6
minor edit in vignette
fef2f25
update vignette
1c81408
improve vignette
50123bf
Merge 1c814086577f7f1a7c3deb3d3091f457d60b78a0 into 41f660b0b8ffdd543…
chlebowa 2de4149
[skip actions] Restyle files
github-actions[bot] cdaa0d6
enhance documentation
72b762b
remove warning for symbols
00ae3da
remove dropping strings from expr
668bcda
improve tests for value injection
e87a418
improve test description
666d095
add note in vignette
ee0558b
improve tests for return value
eea0513
reduce tests
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,5 +59,6 @@ Collate: | |
| 'qenv-get_warnings.R' | ||
| 'qenv-join.R' | ||
| 'qenv-show.R' | ||
| 'qenv-within.R' | ||
| 'teal.code-package.R' | ||
| 'utils.R' | ||
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,75 @@ | ||
| #' Evaluate expression in `qenv` object. | ||
| #' | ||
| #' Convenience function for evaluating inline code inside the environment of a `qenv`. | ||
| #' | ||
| #' This is a wrapper for `eval_code` that provides a simplified way of passing code for evaluation. | ||
| #' It accepts only inline expressions (both simple and compound) and allows for injecting values into `expr` | ||
| #' through the `...` argument: as `name:value` pairs are passed to `...`, | ||
| #' `name` in `expr` will be replaced with `value`. | ||
| #' | ||
| #' @section Using language objects: | ||
| #' Passing language objects to `expr` is generally not intended but can be achieved with `do.call`. | ||
| #' Only single `expression`s will work and substitution is not available. See examples. | ||
| #' | ||
| #' @param data `qenv` object | ||
| #' @param expr `expression` to evaluate | ||
| #' @param ... `name:value` pairs to inject values into `expr` | ||
| #' | ||
| #' @return | ||
| #' Returns a `qenv` object with `expr` evaluated. If evaluation raises an error, a `qenv.error` is returned. | ||
| #' | ||
| #' @seealso [`eval_code`], [`base::within`] | ||
| #' | ||
| #' @export | ||
| #' | ||
| #' @rdname within | ||
| #' | ||
| #' @examples | ||
| #' | ||
| #' q <- new_qenv() | ||
| #' | ||
| #' # execute code | ||
| #' q <- within(q, { | ||
| #' i <- iris | ||
| #' }) | ||
| #' q <- within(q, { | ||
| #' m <- mtcars | ||
| #' f <- faithful | ||
| #' }) | ||
| #' q | ||
| #' get_code(q) | ||
| #' | ||
| #' # inject values into code | ||
| #' q <- new_qenv() | ||
| #' q <- within(q, i <- iris) | ||
| #' within(q, print(dim(subset(i, Species == "virginica")))) | ||
| #' within(q, print(dim(subset(i, Species == species)))) # fails | ||
| #' within(q, print(dim(subset(i, Species == species))), species = "versicolor") | ||
| #' species_external <- "versicolor" | ||
| #' within(q, print(dim(subset(i, Species == species))), species = species_external) | ||
| #' | ||
| #' # pass language objects | ||
| #' expr <- expression(i <- iris, m <- mtcars) | ||
| #' within(q, expr) # fails | ||
| #' do.call(within, list(q, expr)) | ||
| #' | ||
| #' exprlist <- list(expression(i <- iris), expression(m <- mtcars)) | ||
| #' within(q, exprlist) # fails | ||
| #' do.call(within, list(q, do.call(c, exprlist))) | ||
| #' | ||
| within.qenv <- function(data, expr, ...) { | ||
| expr <- substitute(expr) | ||
| extras <- list(...) | ||
|
|
||
| # Add braces for consistency. | ||
| if (!identical(as.list(expr)[[1L]], as.symbol("{"))) { | ||
| expr <- call("{", expr) | ||
| } | ||
|
|
||
| calls <- as.list(expr)[-1] | ||
|
|
||
| # Inject extra values into expressions. | ||
| calls <- lapply(calls, function(x) do.call(substitute, list(x, env = extras))) | ||
|
|
||
| eval_code(object = data, code = as.expression(calls)) | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -33,3 +33,4 @@ reference: | |
| - join | ||
| - new_qenv | ||
| - show,qenv-method | ||
| - within.qenv | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
| # styler: off | ||
| # nolint start | ||
|
|
||
| # code acceptance ---- | ||
| testthat::test_that("simple and compound expressions are evaluated", { | ||
| q <- new_qenv() | ||
| testthat::expect_no_error( | ||
| within(q, 1 + 1) | ||
| ) | ||
| testthat::expect_no_error( | ||
| within(q, { | ||
| 1 + 1 | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| # code identity ---- | ||
| testthat::test_that("styling of input code does not impact evaluation results", { | ||
| q <- new_qenv() | ||
| q <- within(q, 1 + 1) | ||
| q <- within(q, {1 + 1}) | ||
| q <- within(q, { | ||
| 1 + 1 | ||
| }) | ||
| q <- within(q, { | ||
| 1 + | ||
| 1 | ||
| }) | ||
| all_code <- get_code(q) | ||
| testthat::expect_identical( | ||
| all_code, | ||
| rep("1 + 1", 4L) | ||
| ) | ||
|
|
||
| q <- new_qenv() | ||
| q <- within(q, {1 + 1; 2 + 2}) | ||
| q <- within(q, { | ||
| 1 + 1; 2 + 2 | ||
| }) | ||
| q <- within(q, { | ||
| 1 + 1 | ||
| 2 + 2 | ||
| }) | ||
| q <- within(q, { | ||
| 1 + 1; | ||
| 2 + 2 | ||
| }) | ||
| all_code <- get_code(q) | ||
| testthat::expect_identical( | ||
| all_code, | ||
| rep(c("1 + 1", "2 + 2"), 4L) | ||
| ) | ||
| }) | ||
|
|
||
|
|
||
| # return value ---- | ||
| testthat::test_that("within.qenv renturns a deep copy of `data`", { | ||
| q <- new_qenv() | ||
| q <- within(new_qenv(), i <- iris) | ||
| qq <- within(q, {}) | ||
| testthat::expect_equal(q, qq) | ||
| testthat::expect_false(identical(q, qq)) | ||
| }) | ||
|
|
||
| testthat::test_that("within.qenv renturns qenv.error even if evaluation raises error", { | ||
| q <- new_qenv() | ||
| q <- within(q, i <- iris) | ||
| qq <- within(q, stop("right there")) | ||
| testthat::expect_true( | ||
| exists("qq", inherits = FALSE) | ||
| ) | ||
| testthat::expect_s3_class(qq, "qenv.error") | ||
| }) | ||
|
|
||
|
|
||
| # injecting values ---- | ||
| testthat::test_that("external values can be injected into expressions through `...`", { | ||
| q <- new_qenv() | ||
|
|
||
| external_value <- "virginica" | ||
| q <- within(q, { | ||
| i <- subset(iris, Species == species) | ||
| }, | ||
| species = external_value) | ||
|
|
||
| testthat::expect_identical(get_code(q), "i <- subset(iris, Species == \"virginica\")") | ||
| }) | ||
|
|
||
| testthat::test_that("external values are not taken from calling frame", { | ||
| q <- new_qenv() | ||
| species <- "setosa" | ||
| qq <- within(q, { | ||
| i <- subset(iris, Species == species) | ||
| }) | ||
| testthat::expect_s3_class(qq, "qenv.error") | ||
|
chlebowa marked this conversation as resolved.
|
||
| testthat::expect_error(get_code(qq), "object 'species' not found") | ||
|
|
||
| qq <- within(q, { | ||
| i <- subset(iris, Species == species) | ||
| }, | ||
| species = species) | ||
| testthat::expect_s4_class(qq, "qenv") | ||
| testthat::expect_identical(get_code(qq), "i <- subset(iris, Species == \"setosa\")") | ||
| }) | ||
|
|
||
| # nolint end | ||
| # styler: on | ||
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
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.