|
10 | 10 | #' `qenv()` instantiates a `qenv` with an empty environment. |
11 | 11 | #' Any changes must be made by evaluating code in it with `eval_code` or `within`, thereby ensuring reproducibility. |
12 | 12 | #' |
13 | | -#' |
14 | | -#' `new_qenv()` (`r badge("deprecated")` and not recommended) |
15 | | -#' can instantiate a `qenv` object with data in the environment and code registered. |
16 | | -#' |
17 | | -#' @section Extracting objects from `qenv`: |
18 | | -#' |
19 | | -#' Extracting an object from the `qenv` by name can be done using the following methods: |
20 | | -#' |
21 | | -#' - `x[[name]]` |
22 | | -#' - `x$name` |
23 | | -#' - `get(name, envir = x)` |
24 | | -#' |
25 | | -#' note: `get_var(name)` was superseded by the native \R methods above. |
26 | | -#' |
27 | | -#' To list all objects in the environment, use `ls(x)` (which doesn't show |
28 | | -#' objects that have a dot prefix with default arguments) or `names(x)` (shows all objects). |
29 | | -#' |
30 | | -#' @section Environment: |
31 | | -#' |
32 | | -#' The `qenv` object behaves like an environment that is locked and one can use |
33 | | -#' some of the `base` functions dedicated to the `environment`. List of supported |
34 | | -#' functions includes: |
35 | | -#' `names()`, `ls()`, `get()`, `exists()`, `parent.env()`, `lapply`, `sapply` |
36 | | -#' `vapply`, `local`, `as.environment()`, `is.environment()`, `as.list()`, ... |
37 | | -#' We don't recommend using any function outside of the `teal.code` exports and these |
38 | | -#' mentioned above. |
39 | | -#' |
40 | | -#' |
41 | 13 | #' @name qenv |
42 | 14 | #' |
43 | | -#' @return `qenv` and `new_qenv` return a `qenv` object. |
| 15 | +#' @return Returns a `qenv` object. |
44 | 16 | #' |
| 17 | +#' @seealso [`base::within()`], [`get_var()`], [`get_env()`], [`get_warnings()`], [`join()`], [`concat()`] |
45 | 18 | #' @examples |
46 | 19 | #' # create empty qenv |
47 | 20 | #' qenv() |
|
50 | 23 | qenv <- function() { |
51 | 24 | methods::new("qenv") |
52 | 25 | } |
53 | | - |
54 | | -#' @param code `r badge("deprecated")` |
55 | | -#' (`character(1)` or `language`) code to evaluate. Accepts and stores comments also. |
56 | | -#' @param env `r badge("deprecated")` (`environment`) |
57 | | -#' Environment being a result of the `code` evaluation. |
58 | | -#' |
59 | | -#' @examples |
60 | | -#' # create qenv with data and code (deprecated) |
61 | | -#' new_qenv(env = list2env(list(a = 1)), code = quote(a <- 1)) |
62 | | -#' new_qenv(env = list2env(list(a = 1)), code = parse(text = "a <- 1", keep.source = TRUE)) |
63 | | -#' new_qenv(env = list2env(list(a = 1)), code = "a <- 1") |
64 | | -#' |
65 | | -#' @rdname qenv |
66 | | -#' @aliases new_qenv,environment,expression-method |
67 | | -#' @aliases new_qenv,environment,character-method |
68 | | -#' @aliases new_qenv,environment,language-method |
69 | | -#' @aliases new_qenv,environment,missing-method |
70 | | -#' @aliases new_qenv,missing,missing-method |
71 | | -#' |
72 | | -#' @seealso [`base::within()`], [`get_var()`], [`get_env()`], [`get_warnings()`], [`join()`], [`concat()`] |
73 | | -#' |
74 | | -#' @export |
75 | | -setGeneric("new_qenv", function(env = new.env(parent = parent.env(.GlobalEnv)), code = character()) { |
76 | | - lifecycle::deprecate_warn(when = " 0.5.0", what = "new_qenv()", with = "qenv()", always = TRUE) |
77 | | - standardGeneric("new_qenv") |
78 | | -}) |
79 | | - |
80 | | -setMethod( |
81 | | - "new_qenv", |
82 | | - signature = c(env = "environment", code = "expression"), |
83 | | - function(env, code) { |
84 | | - new_qenv(env, paste(lang2calls(code), collapse = "\n")) |
85 | | - } |
86 | | -) |
87 | | - |
88 | | -setMethod( |
89 | | - "new_qenv", |
90 | | - signature = c(env = "environment", code = "character"), |
91 | | - function(env, code) { |
92 | | - new_env <- rlang::env_clone(env, parent = parent.env(.GlobalEnv)) |
93 | | - lockEnvironment(new_env, bindings = TRUE) |
94 | | - if (length(code) > 0) code <- paste(code, collapse = "\n") |
95 | | - id <- sample.int(.Machine$integer.max, size = length(code)) |
96 | | - methods::new( |
97 | | - "qenv", |
98 | | - .xData = new_env, |
99 | | - code = code, |
100 | | - warnings = rep("", length(code)), |
101 | | - messages = rep("", length(code)), |
102 | | - id = id |
103 | | - ) |
104 | | - } |
105 | | -) |
106 | | - |
107 | | -setMethod( |
108 | | - "new_qenv", |
109 | | - signature = c(env = "environment", code = "language"), |
110 | | - function(env, code) { |
111 | | - new_qenv(env = env, code = paste(lang2calls(code), collapse = "\n")) |
112 | | - } |
113 | | -) |
114 | | - |
115 | | -setMethod( |
116 | | - "new_qenv", |
117 | | - signature = c(code = "missing", env = "missing"), |
118 | | - function(env, code) { |
119 | | - new_qenv(env = env, code = code) |
120 | | - } |
121 | | -) |
0 commit comments