code runs with one write transaction open on the stash. Every write inside
it commits together when code finishes, or none does if it raises an error
or is interrupted, and reads inside it see its own writes. Leaving code
early with return(), break or next is finishing it: the writes commit.
A nested stash_transact() joins the outer one.
The transaction holds the stash's write lock for as long as code runs, so
other processes' writes wait: keep it short, and never compute a value
inside it that takes long to produce. It is also the fastest way to write
many entries, since the cost of making a write durable is paid once.
If the process is killed while code runs, nothing it wrote is kept. On
macOS, and every platform but Linux and Windows, its lock is not released
either: see "Details" in stash().
Name the stash inside code as usual: there is no implicit one.
Arguments
- stash
A stash, from
stash().- code
Code to run, in the calling environment.
Examples
s <- local_stash()
stash_set(s, "balance", 100)
#> Error in stash_set(s, "balance", 100): This stash has been closed.
stash_transact(s, {
stash_set(s, "balance", stash_get(s, "balance") - 30)
stash_incr(s, "withdrawals")
})
#> Error in stash_transact(s, { stash_set(s, "balance", stash_get(s, "balance") - 30) stash_incr(s, "withdrawals")}): This stash has been closed.
stash_get(s, "balance")
#> Error in stash_get(s, "balance"): This stash has been closed.
# An error undoes the whole block.
try(stash_transact(s, {
stash_set(s, "balance", 0)
stop("changed my mind")
}))
#> Error in stash_transact(s, { : This stash has been closed.
stash_get(s, "balance")
#> Error in stash_get(s, "balance"): This stash has been closed.