File: globals%2Cmanual.R

package info (click to toggle)
r-cran-future.batchtools 0.12.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 528 kB
  • sloc: sh: 82; makefile: 2
file content (119 lines) | stat: -rw-r--r-- 2,204 bytes parent folder | download | duplicates (3)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
source("incl/start.R")

plan(batchtools_local)

## CRAN processing times:
## On Windows 32-bit, don't run these tests on batchtools
if (!fullTest && isWin32) plan(sequential)

message("*** Globals - manually ...")

message("*** Globals manually specified as named list ...")

globals <- list(
  a = 1,
  b = 2,
  sumtwo = function(x) x[1] + x[2]
)

## Assign 'globals' globally
attach_locally(globals)

## Truth
v0 <- local({
  x <- 1:10
  sumtwo(a + b * x)
})


message("*** Globals - automatic ...")

attach_locally(globals)
f <- future({
  x <- 1:10
  sumtwo(a + b * x)
}, globals = TRUE)
rm(list = names(globals))
y <- value(f)
print(y)
stopifnot(all.equal(y, v0))

attach_locally(globals)
y %<-% {
  x <- 1:10
  sumtwo(a + b * x)
} %globals% TRUE
rm(list = names(globals))
print(y)
stopifnot(all.equal(y, v0))

## No need to search for globals
y %<-% { 1 } %globals% FALSE
print(y)
stopifnot(identical(y, 1))

## Exception - missing global
attach_locally(globals)
f <- future({
  x <- 1:10
  sumtwo(a + b * x)
}, globals = FALSE)
rm(list = names(globals))
y <- tryCatch(value(f), error = identity)
if (!inherits(f, c("SequentialFuture", "MulticoreFuture"))) {
  stopifnot(inherits(y, "simpleError"))
}

message("*** Globals - automatic ... DONE")


message("*** Globals manually specified as named list ...")

## Make sure globals do not exist
rm(list = names(globals))

f <- future({
  x <- 1:10
  sumtwo(a + b * x)
}, globals = globals)
v <- value(f)
print(v)
stopifnot(all.equal(v, v0))

y %<-% {
  x <- 1:10
  sumtwo(a + b * x)
} %globals% globals
print(y)
stopifnot(all.equal(y, v0))

message("*** Globals manually specified as named list ... DONE")


message("*** Globals manually specified by their names ...")

attach_locally(globals)
f <- future({
  x <- 1:10
  sumtwo(a + b * x)
}, globals = c("a", "b", "sumtwo"))
rm(list = names(globals))
v <- value(f)
print(v)
stopifnot(all.equal(v, v0))

attach_locally(globals)
y %<-% {
  x <- 1:10
  sumtwo(a + b * x)
} %globals% c("a", "b", "sumtwo")
rm(list = names(globals))
print(y)
stopifnot(all.equal(y, v0))

message("*** Globals manually specified by their names ... DONE")


message("*** Globals - manually ... DONE")

source("incl/end.R")