Skip to content

Commit

Permalink
175 ls(qenv) (#187)
Browse files Browse the repository at this point in the history
Close #175

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Aleksander Chlebowski <[email protected]>
  • Loading branch information
3 people authored Dec 15, 2023
1 parent a50d3c0 commit 0a4f76b
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 4 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Collate:
'qenv-constructor.R'
'qenv-eval_code.R'
'qenv-get_code.R'
'qenv-get_env.R'
'qenv-get_var.R'
'qenv-get_warnings.R'
'qenv-join.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(concat)
export(dev_suppress)
export(eval_code)
export(get_code)
export(get_env)
export(get_var)
export(get_warnings)
export(join)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* The `@code` field in the `qenv` class now holds `character`, not `expression`.
* The `get_code` method returns a single concatenated string of the code.
* Added `within` support for `qenv.error` class.
* Added `get_env` method that allows to extract environment stored in `qenv@env` slot.

# teal.code 0.4.1

Expand Down
2 changes: 1 addition & 1 deletion R/qenv-constructor.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ qenv <- function() {
#' @aliases new_qenv,environment,missing-method
#' @aliases new_qenv,missing,missing-method
#'
#' @seealso [`base::within()`], [`get_var()`], [`get_warnings()`], [`join()`], [`concat()`]
#' @seealso [`base::within()`], [`get_var()`], [`get_env()`], [`get_warnings()`], [`join()`], [`concat()`]
#'
#' @export
setGeneric("new_qenv", function(env = new.env(parent = parent.env(.GlobalEnv)), code = character()) {
Expand Down
32 changes: 32 additions & 0 deletions R/qenv-get_env.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' 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.
#'
#' @param object (`qenv`)
#'
#' @return An `environment` stored in `qenv@env` slot.
#'
#' @examples
#' q <- qenv()
#' q1 <- within(q, {
#' a <- 5
#' b <- data.frame(x = 1:10)
#' })
#' get_env(q1)
#' ls(get_env(q1))
#'
#' @aliases get_env,qenv-method
#' @aliases get_env,qenv.error-method
#'
#' @export
setGeneric("get_env", function(object) {
standardGeneric("get_env")
})

setMethod("get_env", "qenv", function(object) {
object@env
})

setMethod("get_env", "qenv.error", function(object) {
object
})
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,31 @@ Below is the showcase of the example usage

```r
library(teal.code)
my_qenv <- qenv(env = list2env(list(x = 5)), code = "x <- 5")
my_qenv <- qenv() |> eval_code("x <- 5")
my_qenv
#> <environment: 0x00000225cc85c7a0> [L]
#> Parent: <environment: package:teal.code>
#> Bindings:
#> • x: <dbl> [L]
get_env(my_qenv)
#> <environment: 0x00000225cc85c7a0>
ls(get_env(my_qenv))
#> [1] "x"
```

```r
qenv_2 <- eval_code(my_qenv, "y <- x * 2") |> eval_code("z <- y * 2")
qenv_2
#> <environment: 0x00000135b544cfe8> [L]
#> <environment: 0x00000225ca866d68> [L]
#> Parent: <environment: package:teal.code>
#> Bindings:
#> • x: <dbl> [L]
#> • y: <dbl> [L]
#> • z: <dbl> [L]
get_env(qenv_2)
#> <environment: 0x00000225ca866d68>
ls(get_env(qenv_2))
#> [1] "x" "y" "z"
```

```r
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ reference:
- dev_suppress
- eval_code
- get_code
- get_env
- get_var
- get_warnings
- join
Expand Down
29 changes: 29 additions & 0 deletions man/get_env.Rd

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

2 changes: 1 addition & 1 deletion man/qenv.Rd

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

4 changes: 4 additions & 0 deletions vignettes/qenv.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ library(magrittr)
# evaluate code in qenv
my_qenv <- eval_code(empty_qenv, "x <- 2")
print(my_qenv)
get_env(my_qenv)
q1 <- eval_code(my_qenv, "y <- x * 2") %>% eval_code("z <- y * 2")
# my_qenv still contains only x
print(my_qenv)
ls(get_env(my_qenv))
# q1 contains x, y and z
print(q1)
ls(get_env(q1))
```

The same result can be achieved with the `within` method for the `qenv` class.
Expand Down Expand Up @@ -112,6 +115,7 @@ y_q <- eval_code(common_q, quote(z <- 5))
join_q <- join(x_q, y_q)
print(join_q)
ls(get_env(join_q))
```

The feasibility of joining `qenv` objects hinges on the contents of the environments and the code's order. Refer to the function documentation for further details..
Expand Down

0 comments on commit 0a4f76b

Please sign in to comment.