Skip to content

Commit 9fad8dc

Browse files
committed
use [. instead of subset for qenv
1 parent 069538f commit 9fad8dc

File tree

6 files changed

+67
-74
lines changed

6 files changed

+67
-74
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ Collate:
5858
'qenv-concat.R'
5959
'qenv-constructor.R'
6060
'qenv-eval_code.R'
61+
'qenv-extract.R'
6162
'qenv-get_code.R'
6263
'qenv-get_env.R'
6364
'qenv-get_var.R'
6465
'qenv-get_warnings.R'
6566
'qenv-join.R'
6667
'qenv-show.R'
67-
'qenv-subset.R'
6868
'qenv-within.R'
6969
'teal.code-package.R'
7070
'utils-get_code_dependency.R'

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method("[",qenv)
34
S3method("[[",qenv.error)
45
S3method(within,qenv)
56
S3method(within,qenv.error)
@@ -13,7 +14,6 @@ export(get_warnings)
1314
export(join)
1415
export(new_qenv)
1516
export(qenv)
16-
export(subset)
1717
exportClasses(qenv)
1818
exportMethods(show)
1919
importFrom(lifecycle,badge)

R/qenv-extract.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#' @rdname qenv
2+
#' @order 2
3+
#'
4+
#' @section Subsetting:
5+
#' `x[names]` subsets objects in `qenv` environment and limit the code to the necessary to build limited objects.
6+
#'
7+
#' @param names (`character`) names of objects included in `qenv` to subset
8+
#'
9+
#' @examples
10+
#'
11+
#' # Subsetting
12+
#' q <- qenv()
13+
#' q <- eval_code(q, "a <- 1;b<-2")
14+
#' q["a"]
15+
#' q[c("a", "b")]
16+
#'
17+
#' @export
18+
`[.qenv` <- function(x, names) {
19+
checkmate::assert_class(names, "character")
20+
names_in_env <- intersect(names, ls(get_env(x)))
21+
if (!length(names_in_env)) {
22+
return(qenv())
23+
}
24+
25+
limited_code <- get_code(x, names = names_in_env)
26+
indexes <- which(x@code %in% limited_code)
27+
28+
x@env <- list2env(mget(x = names_in_env, envir = get_env(x)))
29+
x@code <- limited_code
30+
x@id <- x@id[indexes]
31+
x@warnings <- x@warnings[indexes]
32+
x@messages <- x@messages[indexes]
33+
34+
x
35+
}
36+

R/qenv-subset.R

Lines changed: 0 additions & 46 deletions
This file was deleted.

man/qenv.Rd

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

tests/testthat/test-qenv_subset.R renamed to tests/testthat/test-qenv_extract.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
testthat::test_that("subset extract proper objects", {
1+
testthat::test_that("`[.` extracts proper objects", {
22
q <- qenv()
33
code <- c("x<-1", "a<-1;b<-2")
44
q <- eval_code(q, code)
55
object_names <- c("x", "a")
6-
qs <- subset(q, names = object_names)
6+
qs <- q[object_names]
77
testthat::expect_true(all(ls(get_env(qs)) %in% object_names))
88
})
99

10-
testthat::test_that("subset extract proper code", {
10+
testthat::test_that("`[.` extract proper code", {
1111
q <- qenv()
1212
code <- c("x<-1", "a<-1;b<-2")
1313
q <- eval_code(q, code)
1414
object_names <- c("x", "a")
15-
qs <- subset(q, names = object_names)
15+
qs <- q[object_names]
1616
testthat::expect_identical(
1717
qs@code,
1818
c("x <- 1", "a <- 1")
1919
)
2020
})
2121

22-
testthat::test_that("subset preservers comments in the code", {
22+
testthat::test_that("`[.` preservers comments in the code", {
2323
q <- qenv()
2424
code <- c("x<-1 #comment", "a<-1;b<-2")
2525
q <- eval_code(q, code)
26-
qs <- subset(q, names = c("x", "a"))
26+
qs <- q[c("x", "a")]
2727
testthat::expect_identical(
2828
qs@code,
2929
c("x <- 1 #comment", "a <- 1")
3030
)
3131
})
3232

33-
testthat::test_that("subset extract proper elements of @id, @warnings and @messages fiels", {
33+
testthat::test_that("`[.` extract proper elements of @id, @warnings and @messages fiels", {
3434
q <- qenv()
3535
code <-
3636
c("x<-1 #comment", "message('tiny message')", "a<-1;b<-2;warning('small warning')")
3737
q <- eval_code(q, code)
38-
qs <- subset(q, names = c("x", "a"))
38+
qs <- q[c("x", "a")]
3939

4040
testthat::expect_identical(qs@id, q@id[c(1, 3)])
4141
testthat::expect_identical(qs@code, q@code[c(1, 3)])

0 commit comments

Comments
 (0)