Simple with functions. Similar to or from withr implementations.
Usage
gwith_options(new, code)
gwith_package(package, code, pos = 2L)
gwith_seed(seed = 1234, code, ..., substitute = TRUE, envir = parent.frame())
gwith_awake(code)Arguments
- new
new option to set and its value
- code
R code to execute with temporary settings
- package
character. The name of a package
- pos
integer. position in search to attach at. Default = 2
- seed
numeric. seed to set
- ...
additional params to pass. See details.
- substitute
If
TRUE, argumentexprissubstitute():ed, otherwise not.- envir
The
environmentin which the expression should be evaluated.
Details
gwith_seed() : ... passes to set.seed()
gwith_awake() : holds a sleep assertion for the duration of code, then
releases it. A system sleep part-way through a long computation inflates its
wall-clock time while leaving CPU time, memory and I/O counters looking
normal, so an affected measurement is indistinguishable from a slow one
without this. See keep_awake(), which this wraps, for platform support and
for the giotto.prevent_sleep kill switch.
Functions
gwith_options(): Eval with temporary option settinggwith_package(): Eval with temporarily attached packagegwith_seed(): Eval with temporary specifiable seedgwith_awake(): Eval while preventing the machine from sleeping
Examples
# options ###################################
gwith_options(list(gutils.temp = "found"), {
print(getOption("gutils.temp", default = "not_found"))
})
#> [1] "found"
getOption("gutils.temp", default = "not_found")
#> [1] "not_found"
# packages ##################################
# temporarily attach at end of search (right before "base")
gwith_package("data.table", print(search()), pos = length(search()))
#> [1] ".GlobalEnv" "package:GiottoUtils" "package:stats"
#> [4] "package:graphics" "package:grDevices" "package:utils"
#> [7] "package:datasets" "package:methods" "Autoloads"
#> [10] "package:data.table" "package:base"
search()
#> [1] ".GlobalEnv" "package:GiottoUtils" "package:stats"
#> [4] "package:graphics" "package:grDevices" "package:utils"
#> [7] "package:datasets" "package:methods" "Autoloads"
#> [10] "package:base"
# seed #####################################
start_seed <- .Random.seed
# identical generation
a <- gwith_seed(runif(10), seed = 1234)
b <- gwith_seed(runif(10), seed = 1234)
identical(a, b)
#> [1] TRUE
# does not alter pre-existing seed
end_seed <- .Random.seed
identical(start_seed, end_seed)
#> [1] TRUE
# sleep prevention ##########################
if (FALSE) { # \dontrun{
gwith_awake({
g <- runPCA(g, feats_to_use = "hvf", ncp = 30)
g <- createNearestNetwork(g, dim_reduction_to_use = "pca")
})
} # }
