|
19 | 19 | `[.qenv` <- function(x, names, ...) { |
20 | 20 | checkmate::assert_class(names, "character") |
21 | 21 | possible_names <- ls(get_env(x), all.names = TRUE) |
22 | | - names_warn <- setdiff(names, possible_names) |
23 | | - names <- intersect(names, possible_names) |
24 | | - if (!length(names)) { |
25 | | - warning( |
26 | | - sprintf( |
27 | | - "None of 'names' elements exist in '%s'. Returning empty '%s'.", |
28 | | - class(x)[1], |
29 | | - class(x)[1] |
| 22 | + names_corrected <- intersect(names, possible_names) |
| 23 | + env <- if (length(names_corrected)) { |
| 24 | + names_missing <- setdiff(names, possible_names) |
| 25 | + if (length(names_missing)) { |
| 26 | + warning( |
| 27 | + sprintf( |
| 28 | + "Some elements of 'names' do not exist in the environment of the '%s'. Skipping those: %s.", |
| 29 | + class(x)[1], |
| 30 | + paste(names_missing, collapse = ", ") |
| 31 | + ) |
30 | 32 | ) |
31 | | - ) |
32 | | - return(qenv()) |
33 | | - } |
34 | | - |
35 | | - if (length(names_warn)) { |
| 33 | + } |
| 34 | + list2env(as.list(x, all.names = TRUE)[names_corrected], parent = parent.env(.GlobalEnv)) |
| 35 | + } else { |
36 | 36 | warning( |
37 | 37 | sprintf( |
38 | | - "Some elements of 'names' do not exist in '%s'. Skipping those: %s.", |
39 | | - class(x)[1], |
40 | | - paste(names_warn, collapse = ", ") |
41 | | - ) |
| 38 | + "None of 'names' exist in the environment of the '%1$s'. Returning empty '%1$s'.", |
| 39 | + class(x)[1] |
| 40 | + ), |
| 41 | + call. = FALSE |
42 | 42 | ) |
| 43 | + new.env(parent = parent.env(.GlobalEnv)) |
43 | 44 | } |
| 45 | + lockEnvironment(env) |
| 46 | + x@.xData <- env |
44 | 47 |
|
45 | | - x@.xData <- list2env(as.list(x, all.names = TRUE)[names]) |
46 | | - names <- gsub("^`(.*)`$", "\\1", names) |
47 | | - x@code <- get_code_dependency(x@code, names = names, ...) |
| 48 | + normalized_names <- gsub("^`(.*)`$", "\\1", names) |
| 49 | + x@code <- get_code_dependency(x@code, names = normalized_names, ...) |
48 | 50 |
|
49 | 51 | x |
50 | 52 | } |
0 commit comments