R Under development (unstable) (2024-03-06 r86056 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > source("incl/start.R") > > message("*** globalsByName() ...") *** globalsByName() ... > > globals_c <- globalsByName(c("{", "<-", "c", "d")) > str(globals_c) List of 4 $ { :.Primitive("{") $ <-:.Primitive("<-") $ c : num 3 $ d : NULL - attr(*, "where")=List of 4 ..$ { : ..$ <-: ..$ c : ..$ d : - attr(*, "class")= chr [1:2] "Globals" "list" > assert_identical_sets(names(globals_c), c("{", "<-", "c", "d")) > globals_c <- cleanup(globals_c) > str(globals_c) List of 2 $ c: num 3 $ d: NULL - attr(*, "where")=List of 2 ..$ c: ..$ d: - attr(*, "class")= chr [1:2] "Globals" "list" > assert_identical_sets(names(globals_c), c("c", "d")) > where <- attr(globals_c, "where") > stopifnot( + length(where) == length(globals_c), + identical(where$c, globalenv()), + identical(where$d, globalenv()) + ) > > foo <- globals::Globals > globals <- globalsByName(c("{", "foo", "list"), recursive = FALSE) > str(globals) List of 3 $ { :.Primitive("{") $ foo :function (object = list(), ...) $ list:function (...) - attr(*, "where")=List of 3 ..$ { : ..$ foo : ..$ list: - attr(*, "class")= chr [1:2] "Globals" "list" > assert_identical_sets(names(globals), c("{", "foo", "list")) > where <- attr(globals, "where") > stopifnot(length(where) == length(globals)) > if (!covr) stopifnot( + identical(where$`{`, baseenv()), + identical(where$foo, globalenv()), + identical(where$list, baseenv()) + ) > > globals <- cleanup(globals) > str(globals) List of 1 $ foo:function (object = list(), ...) - attr(*, "where")=List of 1 ..$ foo: - attr(*, "class")= chr [1:2] "Globals" "list" > assert_identical_sets(names(globals), c("foo")) > globals <- cleanup(globals, drop = "internals") > str(globals) List of 1 $ foo:function (object = list(), ...) - attr(*, "where")=List of 1 ..$ foo: - attr(*, "class")= chr [1:2] "Globals" "list" > assert_identical_sets(names(globals), c("foo")) > pkgs <- packagesOf(globals) > stopifnot(pkgs == "globals") > > > ## Also '...' > myGlobals <- function(x, ...) { + globalsByName(c("a", "x", "...")) + } > globals <- myGlobals(x = 2, y = 3, z = 4) > str(globals) List of 3 $ a : num 0 $ x : num 2 $ ...:List of 2 ..$ y: num 3 ..$ z: num 4 ..- attr(*, "class")= chr [1:2] "DotDotDotList" "list" - attr(*, "where")=List of 3 ..$ a : ..$ x : ..$ ...: - attr(*, "class")= chr [1:2] "Globals" "list" > assert_identical_sets(names(globals), c("a", "x", "...")) > assert_identical_sets(names(globals[["..."]]), c("y", "z")) > > ## And '..1', '..2', etc. > myGlobals <- function(x, ...) { + globalsByName(c("a", "x", "..1", "..2")) + } > globals <- myGlobals(x = 2, y = 3, 4) > str(globals) List of 4 $ a : num 0 $ x : num 2 $ ..1:List of 1 ..$ : num 3 ..- attr(*, "class")= chr [1:2] "DotDotDotList" "list" $ ..2:List of 1 ..$ : num 4 ..- attr(*, "class")= chr [1:2] "DotDotDotList" "list" - attr(*, "where")=List of 4 ..$ a : ..$ x : ..$ ..1: ..$ ..2: - attr(*, "class")= chr [1:2] "Globals" "list" > assert_identical_sets(names(globals), c("a", "x", "..1", "..2")) > stopifnot( + globals[["..1"]] == 3, + globals[["..2"]] == 4 + ) > > ## BUG FIX: Assert that '...' does not have to be specified at the end > myGlobals <- function(x, ...) { + globalsByName(c("a", "...", "x")) + } > globals <- myGlobals(x = 2, y = 3, z = 4) > str(globals) List of 3 $ a : num 0 $ ...:List of 2 ..$ y: num 3 ..$ z: num 4 ..- attr(*, "class")= chr [1:2] "DotDotDotList" "list" $ x : num 2 - attr(*, "where")=List of 3 ..$ a : ..$ ...: ..$ x : - attr(*, "class")= chr [1:2] "Globals" "list" > assert_identical_sets(names(globals), c("a", "x", "...")) > assert_identical_sets(names(globals[["..."]]), c("y", "z")) > > > ## Test with arguments defaulting to other arguments > myGlobals <- function(x, y, z = y) { + globalsByName(c("a", "x", "y", "z")) + } > globals <- myGlobals(x = 2, y = 3) > assert_identical_sets(names(globals), c("a", "x", "y", "z")) > stopifnot(globals$y == 3, identical(globals$z, globals$y)) > > globals <- myGlobals(x = 2, y = 3, z = 4) > assert_identical_sets(names(globals), c("a", "x", "y", "z")) > stopifnot(globals$y == 3, globals$z == 4) > > myGlobals <- function(x, ...) { + globalsByName(c("a", "x", "...")) + } > globals <- myGlobals(x = 2, y = 3) > assert_identical_sets(names(globals), c("a", "x", "...")) > assert_identical_sets(names(globals[["..."]]), c("y")) > stopifnot(globals[["..."]]$y == 3) > > globals <- myGlobals(x = 2, y = 3, z = 4) > assert_identical_sets(names(globals), c("a", "x", "...")) > assert_identical_sets(names(globals[["..."]]), c("y", "z")) > stopifnot(globals[["..."]]$y == 3, globals[["..."]]$z == 4) > > message("*** globalsByName() ... DONE") *** globalsByName() ... DONE > > source("incl/end.R") > > proc.time() user system elapsed 0.26 0.00 0.25