File: formulas.R

package info (click to toggle)
r-cran-globals 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 300 kB
  • sloc: sh: 14; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 2,321 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
source("incl/start.R")

message("findGlobals() with formula ...")

g <- findGlobals(. ~ x + y : z, substitute = TRUE)
print(g)
assert_identical_sets(g, c("~", ".", "+", "x", ":", "y", "z"))

g <- findGlobals(map(1L, ~ typeof(.x)), substitute = TRUE)
print(g)
assert_identical_sets(g, c("map", "~", "typeof", ".x"))


message("- findGlobals() with NULL in the formula ...")
## BUG: https://github.com/HenrikBengtsson/globals/issues/59
for (substitute in c(TRUE, FALSE)) {
  message("- substitute = ", substitute)
  
  g <- findGlobals(. ~ NULL, substitute = substitute)
  print(g)
  assert_identical_sets(g, c(".", "~"))

  g <- findGlobals(NULL ~ NULL, substitute = substitute)
  print(g)
  assert_identical_sets(g, c("~"))

  g <- findGlobals(~ NULL, substitute = substitute)
  print(g)
  assert_identical_sets(g, c("~"))

  g <- findGlobals(NULL ~ ., substitute = substitute)
  print(g)
  assert_identical_sets(g, c("~", "."))
}

# ## substitute=FALSE
# Browse[2]> str(expr)
#  language ~NULL
# 
# ## substitute=TRUE
# Browse[2]> str(expr)
# Class 'formula'  language ~NULL
#   ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 


message("- findGlobals() with ellipsis in formulas ...")
## BUG: https://github.com/HenrikBengtsson/globals/issues/62

g <- findGlobals(list(..., ..3) ~ list(., .x, ..., ..1, ..2))
print(g)
assert_identical_sets(g, c("~", "list", "...", "..3", ".", ".x", "..1", "..2"))

message("- findGlobals() with NULL in formulas ...")
## BUG: https://github.com/HenrikBengtsson/globals/issues/64

env <- new.env(parent = globalenv())
env$`~` <- function(...) "OVERRIDE!"

x <- ~ NULL
g <- eval(quote(findGlobals(x)), env)
assert_identical_sets(g, "~")

x <- list(~ NULL)
g <- eval(quote(findGlobals(x)), env)
assert_identical_sets(g, "~")

x <- list(NULL ~ NULL)
g <- eval(quote(findGlobals(x)), env)
assert_identical_sets(g, "~")

x <- list(NULL ~ b)
g <- eval(quote(findGlobals(x)), env)
assert_identical_sets(g, c("~", "b"))


message("findGlobals() with formula ... DONE")


message("globalsOf() with formula ...")

foo <- function(x) {
  map(1L, ~ typeof(x + .x))
}

g <- globalsOf(foo(1L), substitute = TRUE, mustExist = FALSE)
str(g)
assert_identical_sets(names(g), c("foo", "map", "{", "~", "typeof", "+", "x", ".x"))

message("globalsOf() with formula ... DONE")

source("incl/end.R")