Skip to content

Commit 99ebe96

Browse files
committed
proposition of the implementation
1 parent b883c9a commit 99ebe96

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Collate:
6464
'qenv-get_warnings.R'
6565
'qenv-join.R'
6666
'qenv-show.R'
67+
'qenv-subset.R'
6768
'qenv-within.R'
6869
'teal.code-package.R'
6970
'utils-get_code_dependency.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export(get_warnings)
1313
export(join)
1414
export(new_qenv)
1515
export(qenv)
16+
export(subset)
1617
exportClasses(qenv)
1718
exportMethods(show)
1819
importFrom(lifecycle,badge)

R/qenv-subset.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#' Subset `qenv`
2+
#'
3+
#' @details
4+
#' Subset objects in `qenv` environment and limit the code to the necessary to build limited objects.
5+
#'
6+
#' @param object (`qenv`)
7+
#' @param names (`character`) names of objects included in `qenv` to subset
8+
#'
9+
#' @return
10+
#' `qenv` object
11+
#'
12+
#' @examples
13+
#' q <- qenv()
14+
#' q <- eval_code(q, "a <- 1;b<-2")
15+
#' q <- subset(q, "a")
16+
#'
17+
#' @name subset
18+
#' @rdname qenv
19+
#' @aliases subset,qenv-method
20+
#' @aliases subset,qenv.error,ANY-method
21+
#'
22+
#' @export
23+
setGeneric("subset", function(object, names) standardGeneric("subset"))
24+
25+
setMethod("subset", signature = c("qenv"), function(object, names) {
26+
# based on https://github.com/insightsengineering/teal/blob/a1087d2d3ff0c62393c3d5277cd5f184d543e2d9/R/teal_data_utils.R#L41-L64
27+
checkmate::assert_class(names, "character")
28+
names_in_env <- intersect(names, ls(get_env(object)))
29+
if (!length(names_in_env)) {
30+
return(qenv())
31+
}
32+
33+
new_qenv <- qenv()
34+
new_qenv@env <- list2env(mget(x = names_in_env, envir = get_env(object)))
35+
new_qenv@code <- get_code(object, names = names_in_env)
36+
# Question: what about @id, @warnings, @messages?
37+
# Currently:
38+
# > new_qenv@id
39+
# integer(0)
40+
# > new_qenv@warnings
41+
# character(0)
42+
# > new_qenv@messages
43+
# character(0)
44+
new_qenv
45+
46+
})
47+
48+
49+
setMethod("subset", signature = c("qenv.error", "ANY"), function(object, names) {
50+
object
51+
})
52+

man/qenv.Rd

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

0 commit comments

Comments
 (0)