Skip to content

Commit 8c05112

Browse files
authored
datanames in vignettes (#239)
concise cods
1 parent b385638 commit 8c05112

17 files changed

+353
-311
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ repos:
1616
name: Regenerate package documentation
1717
additional_dependencies:
1818
- checkmate
19+
- cli
1920
- grDevices
2021
- lifecycle
2122
- methods

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ Depends:
2828
R (>= 4.0)
2929
Imports:
3030
checkmate (>= 2.1.0),
31+
cli (>= 3.4.0),
3132
grDevices,
3233
lifecycle (>= 0.2.0),
3334
rlang (>= 1.1.0),
3435
stats,
3536
utils
3637
Suggests:
37-
cli (>= 3.4.0),
3838
knitr (>= 1.42),
3939
rmarkdown (>= 2.23),
4040
shiny (>= 1.6.0),
@@ -45,7 +45,7 @@ VignetteBuilder:
4545
rmarkdown
4646
RdMacros:
4747
lifecycle
48-
Config/Needs/verdepcheck: mllg/checkmate, r-lib/lifecycle, r-lib/rlang,
48+
Config/Needs/verdepcheck: mllg/checkmate, r-lib/cli, r-lib/lifecycle, r-lib/rlang,
4949
r-lib/cli, yihui/knitr, rstudio/rmarkdown, rstudio/shiny,
5050
r-lib/testthat, r-lib/withr
5151
Config/Needs/website: insightsengineering/nesttemplate

R/qenv-constructor.R

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
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
13-
#'
14-
#' `qenv()` instantiates a `qenv` with an empty environment.
15-
#' Any changes must be made by evaluating code in it with `eval_code` or `within`, thereby ensuring reproducibility.
9+
#' `qenv` class has following characteristics:
10+
#'
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()]).
16+
#' - It supports slicing (see [`subset-qenv`])
17+
#' - It is immutable which means that each code evaluation does not modify the original `qenv`
18+
#' environment directly. See the following code:
19+
#'
20+
#' ```
21+
#' q1 <- qenv()
22+
#' q2 <- eval_code(q1, "a <- 1")
23+
#' identical(q1, q2) # FALSE
24+
#' ```
1625
#'
1726
#' @name qenv
1827
#'
19-
#' @return `qenv` returns a `qenv` object.
28+
#' @return `qenv` environment.
2029
#'
21-
#' @seealso [`base::within()`], [`get_var()`], [`get_env()`], [`get_warnings()`], [`join()`], [`concat()`]
30+
#' @seealso [eval_code()], [get_var()], [`subset-qenv`], [get_env()],[get_warnings()], [join()], [concat()]
2231
#' @examples
23-
#' # create empty qenv
24-
#' qenv()
25-
#'
32+
#' q <- qenv()
33+
#' q2 <- within(q, a <- 1)
34+
#' ls(q2)
35+
#' q2$a
2636
#' @export
2737
qenv <- function() {
2838
methods::new("qenv")

R/qenv-eval_code.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#' Evaluate code in `qenv`
22
#'
33
#' @details
4+
#'
45
#' `eval_code()` evaluates given code in the `qenv` environment and appends it to the `code` slot.
56
#' Thus, if the `qenv` had been instantiated empty, contents of the environment are always a result of the stored code.
67
#'
@@ -10,7 +11,7 @@
1011
#' `expression` being a result of `parse(keep.source = TRUE)`.
1112
#'
1213
#' @return
13-
#' `eval_code` returns a `qenv` object with `expr` evaluated or `qenv.error` if evaluation fails.
14+
#' `qenv` environment with `code/expr` evaluated or `qenv.error` if evaluation fails.
1415
#'
1516
#' @examples
1617
#' # evaluate code in qenv
@@ -20,8 +21,6 @@
2021
#' q <- eval_code(q, quote(library(checkmate)))
2122
#' q <- eval_code(q, expression(assert_number(a)))
2223
#'
23-
#' @name eval_code
24-
#' @rdname qenv
2524
#' @aliases eval_code,qenv,character-method
2625
#' @aliases eval_code,qenv,language-method
2726
#' @aliases eval_code,qenv,expression-method

R/qenv-extract.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
#' Subsets `qenv`
12
#'
2-
#' @section Subsetting:
3-
#' `x[names]` subsets objects in `qenv` environment and limit the code to the necessary needed to build limited objects.
4-
#' `...` passes parameters to further methods.
3+
#' @description
4+
#' Subsets [`qenv`] environment and limits the code to the necessary needed to build limited objects.
55
#'
66
#' @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.
710
#'
8-
#' @examples
11+
#' @name subset-qenv
912
#'
10-
#' # Subsetting
13+
#' @examples
1114
#' q <- qenv()
1215
#' q <- eval_code(q, "a <- 1;b<-2")
1316
#' q["a"]
1417
#' q[c("a", "b")]
1518
#'
16-
#' @rdname qenv
17-
#'
1819
#' @export
1920
`[.qenv` <- function(x, names, ...) {
2021
checkmate::assert_character(names, any.missing = FALSE)

R/qenv-get_code.R

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +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
#'
3512
#' @section Extracting dataset-specific code:
36-
#' When `names` for `get_code` is specified, the code returned will be limited to the lines needed to _create_
13+
#'
14+
#' `get_code(object, names)` limits the returned code to contain only those lines needed to _create_
3715
#' the requested objects. The code stored in the `qenv` is analyzed statically to determine
3816
#' which lines the objects of interest depend upon. The analysis works well when objects are created
3917
#' with standard infix assignment operators (see `?assignOps`) but it can fail in some situations.
@@ -98,7 +76,7 @@ NULL
9876
#' - creating and evaluating language objects, _e.g._ `eval(<call>)`
9977
#'
10078
#' @return
101-
#' `get_code` returns the traced code in the form specified by `deparse`.
79+
#' The code used in the `qenv` in the form specified by `deparse`.
10280
#'
10381
#' @examples
10482
#' # retrieve code
@@ -114,8 +92,6 @@ NULL
11492
#' q <- eval_code(q, code = c("a <- 1", "b <- 2"))
11593
#' get_code(q, names = "a")
11694
#'
117-
#' @name get_code
118-
#' @rdname qenv
11995
#' @aliases get_code,qenv-method
12096
#' @aliases get_code,qenv.error-method
12197
#'

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-show.R

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,36 @@
1616
#' @importFrom methods show
1717
#' @export
1818
setMethod("show", "qenv", function(object) {
19-
rlang::env_print(object@.xData)
19+
env <- get_env(object)
20+
header <- cli::col_blue(sprintf("<environment: %s>", rlang::env_label(env)))
21+
parent <- sprintf("Parent: <environment: %s>", rlang::env_label(rlang::env_parent(env)))
22+
cat(cli::style_bold(header), "\U1F512", "\n")
23+
cat(parent, "\n")
24+
25+
shown <- ls(object)
26+
if (length(shown > 0L)) cat(cli::style_bold("Bindings:\n"))
27+
lapply(shown, function(x) {
28+
cat(
29+
sprintf(
30+
"- %s: [%s]\n",
31+
deparse(rlang::sym(x), backtick = TRUE),
32+
class(object[[x]])[1]
33+
)
34+
)
35+
})
36+
37+
hidden <- setdiff(ls(object, all.names = TRUE), shown)
38+
lapply(hidden, function(x) {
39+
cat(
40+
cli::style_blurred(
41+
sprintf(
42+
"- %s: [%s]\n",
43+
deparse(rlang::sym(x), backtick = TRUE),
44+
class(object[[x]])[1]
45+
)
46+
)
47+
)
48+
})
49+
50+
invisible(object)
2051
})

R/qenv-within.R

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
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 `...` named 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`
17-
#'
18-
#' @return
19-
#' `within` returns a `qenv` object with `expr` evaluated or `qenv.error` if evaluation fails.
12+
#' @param ... named argument value will substitute a symbol in the `expr` matched by the name.
13+
#' For practical usage see Examples section below.
2014
#'
2115
#' @examples
2216
#' # evaluate code using within
@@ -49,7 +43,7 @@
4943
#' within(q, exprlist) # fails
5044
#' do.call(within, list(q, do.call(c, exprlist)))
5145
#'
52-
#' @rdname qenv
46+
#' @rdname eval_code
5347
#'
5448
#' @export
5549
#'

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ reference:
3737
- join
3838
- qenv
3939
- show,qenv-method
40+
- subset-qenv
4041
- within.qenv

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)