File: batchtools_local.R

package info (click to toggle)
r-cran-future.batchtools 0.12.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 528 kB
  • sloc: sh: 82; makefile: 2
file content (72 lines) | stat: -rw-r--r-- 1,625 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
source("incl/start.R")
library("listenv")

message("*** batchtools_local() ...")

message("*** batchtools_local() without globals")

f <- batchtools_local({
  42L
})
stopifnot(inherits(f, "BatchtoolsFuture"))

## Check whether a batchtools_local future is resolved
## or not will force evaluation
print(is_resolved <- resolved(f))
stopifnot(is_resolved)

y <- value(f)
print(y)
stopifnot(y == 42L)


message("*** batchtools_local() with globals")
## A global variable
a <- 0
f <- batchtools_local({
  b <- 3
  c <- 2
  a * b * c
})

## Although 'f' is a batchtools_local future and therefore
## resolved/evaluates the future expression only
## when the value is requested, any global variables
## identified in the expression (here 'a') are
## "frozen" at the time point when the future is
## created.  Because of this, 'a' preserved the
## zero value although we reassign it below
a <- 7  ## Make sure globals are frozen
v <- value(f)
print(v)
stopifnot(v == 0)


message("*** batchtools_local() with globals (tricky)")
x <- listenv()
for (ii in 1:2) x[[ii]] <- batchtools_local({ ii }, globals = TRUE)
v <- unlist(value(x))
stopifnot(all(v == 1:2))  ## Make sure globals are frozen


message("*** batchtools_local() and errors")
f <- batchtools_local({
  stop("Whoops!")
  1
})
v <- value(f, signal = FALSE)
print(v)
stopifnot(inherits(v, "simpleError"))

res <- try({ v <- value(f) }, silent = TRUE)
print(res)
stopifnot(inherits(res, "try-error"))

## Error is repeated
res <- try(value(f), silent = TRUE)
print(res)
stopifnot(inherits(res, "try-error"))

message("*** batchtools_local() ... DONE")

source("incl/end.R")