|
3 | 3 | #' Retrieve code from `teal_data` object. |
4 | 4 | #' |
5 | 5 | #' Retrieve code stored in `@code`, which (in principle) can be used to recreate all objects found in `@env`. |
6 | | -#' Use `datanames` to limit the code to one or more of the datasets enumerated in `@datanames`. |
| 6 | +#' Use `names` to limit the code to one or more of the datasets enumerated in `@datanames`. |
7 | 7 | #' |
8 | 8 | #' @section Extracting dataset-specific code: |
9 | | -#' When `datanames` is specified, the code returned will be limited to the lines needed to _create_ |
| 9 | +#' When `names` is specified, the code returned will be limited to the lines needed to _create_ |
10 | 10 | #' the requested datasets. The code stored in the `@code` slot is analyzed statically to determine |
11 | 11 | #' which lines the datasets of interest depend upon. The analysis works well when objects are created |
12 | 12 | #' with standard infix assignment operators (see `?assignOps`) but it can fail in some situations. |
|
23 | 23 | #' x <- 0 |
24 | 24 | #' y <- foo(x) |
25 | 25 | #' }) |
26 | | -#' get_code(data, datanames = "y") |
| 26 | +#' get_code(data, names = "y") |
27 | 27 | #' ``` |
28 | | -#' `x` has no dependencies, so `get_code(data, datanames = "x")` will return only the second call.\cr |
29 | | -#' `y` depends on `x` and `foo`, so `get_code(data, datanames = "y")` will contain all three calls. |
| 28 | +#' `x` has no dependencies, so `get_code(data, names = "x")` will return only the second call.\cr |
| 29 | +#' `y` depends on `x` and `foo`, so `get_code(data, names = "y")` will contain all three calls. |
30 | 30 | #' |
31 | 31 | #' _Case 2: Some objects are created by a function's side effects._ |
32 | 32 | #' ```r |
|
39 | 39 | #' foo() |
40 | 40 | #' y <- x |
41 | 41 | #' }) |
42 | | -#' get_code(data, datanames = "y") |
| 42 | +#' get_code(data, names = "y") |
43 | 43 | #' ``` |
44 | 44 | #' Here, `y` depends on `x` but `x` is modified by `foo` as a side effect (not by reassignment) |
45 | | -#' and so `get_code(data, datanames = "y")` will not return the `foo()` call.\cr |
| 45 | +#' and so `get_code(data, names = "y")` will not return the `foo()` call.\cr |
46 | 46 | #' To overcome this limitation, code dependencies can be specified manually. |
47 | 47 | #' Lines where side effects occur can be flagged by adding "`# @linksto <object name>`" at the end.\cr |
48 | 48 | #' Note that `within` evaluates code passed to `expr` as is and comments are ignored. |
|
58 | 58 | #' foo() # @linksto x |
59 | 59 | #' y <- x |
60 | 60 | #' ") |
61 | | -#' get_code(data, datanames = "y") |
| 61 | +#' get_code(data, names = "y") |
62 | 62 | #' ``` |
63 | 63 | #' Now the `foo()` call will be properly included in the code required to recreate `y`. |
64 | 64 | #' |
|
72 | 72 | #' |
73 | 73 | #' |
74 | 74 | #' @param object (`teal_data`) |
75 | | -#' @param datanames `r lifecycle::badge("experimental")` (`character`) vector of dataset names to return the code for. |
| 75 | +#' @param datanames `r lifecycle::badge("deprecated")` (`character`) vector of dataset names to return the code for. |
| 76 | +#' For more details see the "Extracting dataset-specific code" section. Use `names` instead. |
| 77 | +#' @param names (`character`) Successor of `datanames`. Vector of dataset names to return the code for. |
76 | 78 | #' For more details see the "Extracting dataset-specific code" section. |
77 | 79 | #' @param deparse (`logical`) flag specifying whether to return code as `character` (`deparse = TRUE`) or as |
78 | 80 | #' `expression` (`deparse = FALSE`). |
|
81 | 83 | #' `code` but are passed in `datanames`. To remove the warning, set `check_names = FALSE`. |
82 | 84 | #' |
83 | 85 | #' @return |
84 | | -#' Either a character string or an expression. If `datanames` is used to request a specific dataset, |
| 86 | +#' Either a character string or an expression. If `names` is used to request a specific dataset, |
85 | 87 | #' only code that _creates_ that dataset (not code that uses it) is returned. Otherwise, all contents of `@code`. |
86 | 88 | #' |
87 | 89 | #' @examples |
|
92 | 94 | #' c <- list(x = 2) |
93 | 95 | #' }) |
94 | 96 | #' get_code(tdata1) |
95 | | -#' get_code(tdata1, datanames = "a") |
96 | | -#' get_code(tdata1, datanames = "b") |
| 97 | +#' get_code(tdata1, names = "a") |
| 98 | +#' get_code(tdata1, names = "b") |
97 | 99 | #' |
98 | 100 | #' tdata2 <- teal_data(x1 = iris, code = "x1 <- iris") |
99 | 101 | #' get_code(tdata2) |
|
103 | 105 | #' @aliases get_code,teal_data-method |
104 | 106 | #' |
105 | 107 | #' @export |
106 | | -setMethod("get_code", signature = "teal_data", definition = function(object, deparse = TRUE, datanames = NULL, ...) { |
107 | | - checkmate::assert_character(datanames, min.len = 1L, null.ok = TRUE) |
108 | | - checkmate::assert_flag(deparse) |
| 108 | +setMethod("get_code", |
| 109 | + signature = "teal_data", |
| 110 | + definition = function(object, deparse = TRUE, names = NULL, datanames = lifecycle::deprecated(), ...) { |
| 111 | + if (lifecycle::is_present(datanames)) { |
| 112 | + lifecycle::deprecate_warn( |
| 113 | + when = "0.6.1", |
| 114 | + what = "teal.data::get_code(datanames)", |
| 115 | + with = "teal.code::get_code(names)", |
| 116 | + always = TRUE |
| 117 | + ) |
| 118 | + names <- datanames |
| 119 | + } |
109 | 120 |
|
110 | | - # Normalize in case special it is backticked |
111 | | - if (!is.null(datanames)) { |
112 | | - datanames <- gsub("^`(.*)`$", "\\1", datanames) |
113 | | - } |
| 121 | + if (!is.null(names) && lifecycle::is_present(datanames)) { |
| 122 | + stop("Please use either 'names' (recommended) or 'datanames' parameter.") |
| 123 | + } |
114 | 124 |
|
115 | | - code <- if (!is.null(datanames)) { |
116 | | - get_code_dependency(object@code, datanames, ...) |
117 | | - } else { |
118 | | - object@code |
119 | | - } |
| 125 | + checkmate::assert_character(names, min.len = 1L, null.ok = TRUE) |
| 126 | + checkmate::assert_flag(deparse) |
120 | 127 |
|
121 | | - if (deparse) { |
122 | | - if (length(code) == 0) { |
123 | | - code |
124 | | - } else { |
125 | | - paste(code, collapse = "\n") |
126 | | - } |
127 | | - } else { |
128 | | - parse(text = paste(c("{", code, "}"), collapse = "\n"), keep.source = TRUE) |
| 128 | + methods::callNextMethod(object = object, deparse = deparse, names = names, ...) |
129 | 129 | } |
130 | | -}) |
| 130 | +) |
0 commit comments