Skip to content

Commit 97f5968

Browse files
committed
split qenv methods to separate pages and link in qenv() docs
1 parent 418d248 commit 97f5968

File tree

11 files changed

+282
-324
lines changed

11 files changed

+282
-324
lines changed

R/qenv-constructor.R

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1-
#' Code tracking with `qenv` object
1+
#' Instantiates a `qenv` environment
22
#'
33
#' @description
44
#' `r badge("stable")`
55
#'
6-
#' Create a `qenv` object and evaluate code in it to track code history.
7-
#'
8-
#' @param names (`character`) for `x[names]`, names of objects included in `qenv` to subset. Names not present in `qenv`
9-
#' are skipped. For `get_code` `r lifecycle::badge("experimental")` vector of object names to return the code for.
10-
#' For more details see the "Extracting dataset-specific code" section.
6+
#' Instantiates a `qenv` environment.
117
#'
128
#' @details
9+
#' `qenv` class has following characteristics:
1310
#'
14-
#' `qenv()` instantiates a with an empty `qenv` environment.
15-
#'
16-
#' @section `qenv` characteristics:
17-
#'
18-
#' A `qenv` inherits from the `environment` class, behaves like an environment, and has the
19-
#' following characteristics:
20-
#'
21-
#' - `qenv` environment is locked, and data modification is only possible through the `eval_code()`
22-
#' and `within()` functions.
23-
#' - It stores metadata about the code used to create the data.
11+
#' - It inherits from the environment and methods such as [`$`], [get()], [ls()], [as.list()],
12+
#' [parent.env()] work out of the box.
13+
#' - `qenv` is a locked environment, and data modification is only possible through the [eval_code()]
14+
#' and [within.qenv()] functions.
15+
#' - It stores metadata about the code used to create the data (see [get_code()]).
2416
#' - Is immutable which means that each code evaluation does not modify the original `qenv`
2517
#' environment directly. See the following code:
2618
#'
@@ -32,13 +24,14 @@
3224
#'
3325
#' @name qenv
3426
#'
35-
#' @return `qenv` returns a `qenv` object.
27+
#' @return `qenv` environment.
3628
#'
37-
#' @seealso [`base::within()`], [`get_var()`], [`get_env()`], [`get_warnings()`], [`join()`], [`concat()`]
29+
#' @seealso [eval_code()], [get_var()], [get_env()],[get_warnings()], [join()], [concat()]
3830
#' @examples
39-
#' # create empty qenv
40-
#' qenv()
41-
#'
31+
#' q <- qenv()
32+
#' q2 <- within(q, a <- 1)
33+
#' ls(q2)
34+
#' q2$a
4235
#' @export
4336
qenv <- function() {
4437
methods::new("qenv")

R/qenv-eval_code.R

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44
#'
55
#' `eval_code()` evaluates given code in the `qenv` environment and appends it to the `code` slot.
66
#' Thus, if the `qenv` had been instantiated empty, contents of the environment are always a result of the stored code.
7-
#' The `qenv` object is immutable, even though it inherits from the environment class, which is typically mutable by
8-
#' design. This means that each code evaluation does not modify the original `qenv` object directly. Instead, every
9-
#' code evaluation creates a new `qenv` object that reflects the result of the changes, leaving the original object
10-
#' unchanged.
117
#'
128
#' @param object (`qenv`)
139
#' @param code (`character`, `language` or `expression`) code to evaluate.
1410
#' It is possible to preserve original formatting of the `code` by providing a `character` or an
1511
#' `expression` being a result of `parse(keep.source = TRUE)`.
1612
#'
1713
#' @return
18-
#' `eval_code` and `within` returns a `qenv` object with `expr` evaluated or `qenv.error` if evaluation fails.
14+
#' Returns a `qenv` object with `code/expr` evaluated or `qenv.error` if evaluation fails.
1915
#'
2016
#' @examples
2117
#' # evaluate code in qenv
@@ -25,8 +21,6 @@
2521
#' q <- eval_code(q, quote(library(checkmate)))
2622
#' q <- eval_code(q, expression(assert_number(a)))
2723
#'
28-
#' @name eval_code
29-
#' @rdname qenv
3024
#' @aliases eval_code,qenv,character-method
3125
#' @aliases eval_code,qenv,language-method
3226
#' @aliases eval_code,qenv,expression-method

R/qenv-extract.R

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1+
#' Subset `qenv`
12
#'
2-
#' @section Subsetting by the `names`:
3-
#'
4-
#' `x[names]` subsets `qenv` environment and limits the code to the necessary needed to build limited objects.
5-
#' `...` passes parameters to further methods.
3+
#' @description
4+
#' Subsets [`qenv`] environment and limits the code to the necessary needed to build limited objects.
65
#'
76
#' @param x (`qenv`)
7+
#' @param names (`character`) names of objects included in [`qenv`] to subset. Names not present in [`qenv`]
8+
#' are skipped.
9+
#' @param ... internal usage, please ignore.
810
#'
911
#' @examples
10-
#'
11-
#' # Subsetting
1212
#' q <- qenv()
1313
#' q <- eval_code(q, "a <- 1;b<-2")
1414
#' q["a"]
1515
#' q[c("a", "b")]
1616
#'
17-
#' @rdname qenv
18-
#'
1917
#' @export
2018
`[.qenv` <- function(x, names, ...) {
2119
checkmate::assert_character(names, any.missing = FALSE)

R/qenv-get_code.R

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,17 @@
1-
#' @name qenv-inheritted
2-
#' @rdname qenv
3-
#'
4-
#' @details
5-
#'
6-
#' `x[[name]]`, `x$name` and `get(name, x)` are generic \R operators to access the objects in the environment.
7-
#' See [`[[`] for more details.
8-
#' `names(x)` calls on the `qenv` object and will list all objects in the environment.
9-
#'
10-
#' @return `[[`, `$` and `get` return the value of the object named `name` in the `qenv` object.
11-
#' @return `names` return a character vector of all the names of the objects in the `qenv` object.
12-
#' @return `ls` return a character vector of the names of the objects in the `qenv` object.
13-
#' It will only show the objects that are not named with a dot prefix, unless
14-
#' the `all.names = TRUE`, which will show all objects.
15-
#'
16-
#' @examples
17-
#' # Extract objects from qenv
18-
#' q[["a"]]
19-
#' q$a
20-
#'
21-
#' # list objects in qenv
22-
#' names(q)
23-
NULL
24-
251
#' Get code from `qenv`
262
#'
27-
#' @details
28-
#' `get_code()` retrieves the code stored in the `qenv`. `...` passes arguments to methods.
3+
#' @description
4+
#' Retrieves the code stored in the `qenv`.
295
#'
306
#' @param object (`qenv`)
317
#' @param deparse (`logical(1)`) flag specifying whether to return code as `character` or `expression`.
32-
#' @param ... see `Details`
33-
#'
8+
#' @param ... internal usage, please ignore.
9+
#' @param names (`character`) `r lifecycle::badge("experimental")` vector of object names to return the code for.
10+
#' For more details see the "Extracting dataset-specific code" section.
3411
#'
35-
#' @section Subsetting by the `names`:
12+
#' @section Extracting dataset-specific code:
3613
#'
37-
#' `get_code(x, names)` limits the returned code to contain only those lines needed to _create_
14+
#' `get_code(object, names)` limits the returned code to contain only those lines needed to _create_
3815
#' the requested objects. The code stored in the `qenv` is analyzed statically to determine
3916
#' which lines the objects of interest depend upon. The analysis works well when objects are created
4017
#' with standard infix assignment operators (see `?assignOps`) but it can fail in some situations.
@@ -99,7 +76,7 @@ NULL
9976
#' - creating and evaluating language objects, _e.g._ `eval(<call>)`
10077
#'
10178
#' @return
102-
#' `get_code` returns the traced code in the form specified by `deparse`.
79+
#' Returns the traced code in the form specified by `deparse`.
10380
#'
10481
#' @examples
10582
#' # retrieve code
@@ -115,8 +92,6 @@ NULL
11592
#' q <- eval_code(q, code = c("a <- 1", "b <- 2"))
11693
#' get_code(q, names = "a")
11794
#'
118-
#' @name get_code
119-
#' @rdname qenv
12095
#' @aliases get_code,qenv-method
12196
#' @aliases get_code,qenv.error-method
12297
#'

R/qenv-get_var.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#' Get object from `qenv`
22
#'
33
#' @description
4-
#' `r lifecycle::badge("deprecated")` by native \R operators/functions:
5-
#' `x[[name]]`, `x$name` or [get()].
4+
#' `r lifecycle::badge("deprecated")`
5+
#' Instead of [get_var()] use native \R operators/functions:
6+
#' `x[[name]]`, `x$name` or [get()]:
67
#'
78
#' Retrieve variables from the `qenv` environment.
89
#'
@@ -17,8 +18,6 @@
1718
#' q2 <- eval_code(q1, code = "b <- a")
1819
#' get_var(q2, "b")
1920
#'
20-
#' @name get_var
21-
#' @rdname get_var
2221
#' @aliases get_var,qenv,character-method
2322
#' @aliases get_var,qenv.error,ANY-method
2423
#'

R/qenv-within.R

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
#' Evaluate Expression in `qenv`
2-
#'
31
#' @details
4-
#' `within()` is a convenience function for evaluating inline code inside the environment of a `qenv`.
5-
#' It is a method for the `base` generic that wraps `eval_code` to provide a simplified way of passing code.
6-
#' `within` accepts only inline expressions (both simple and compound) and allows for injecting values into `expr`
7-
#' through the `...` argument:
8-
#' as `name:value` pairs are passed to `...`, `name` in `expr` will be replaced with `value`.
2+
#' `within()` is a convenience method that wraps `eval_code` to provide a simplified way of passing expression.
3+
#' `within` accepts only inline expressions (both simple and compound) and allows to substitute `expr`
4+
#' with the `...` argument values.
95
#'
106
#' @section Using language objects with `within`:
117
#' Passing language objects to `expr` is generally not intended but can be achieved with `do.call`.
128
#' Only single `expression`s will work and substitution is not available. See examples.
139
#'
1410
#' @param data (`qenv`)
1511
#' @param expr (`expression`) to evaluate. Must be inline code, see `Using language objects...`
16-
#' @param ... see `Details`
12+
#' @param ... (`named`) argument value will substitute a symbol in the `expr` matched by the name.
13+
#' For practical examples see the #usage.
1714
#'
18-
#' @return
19-
#' `within` returns a `qenv` object with `expr` evaluated or `qenv.error` if evaluation fails.
2015
#'
2116
#' @examples
2217
#' # evaluate code using within
@@ -49,7 +44,7 @@
4944
#' within(q, exprlist) # fails
5045
#' do.call(within, list(q, do.call(c, exprlist)))
5146
#'
52-
#' @rdname qenv
47+
#' @rdname eval_code
5348
#'
5449
#' @export
5550
#'

man/eval_code.Rd

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)