File: resources_OP.R

package info (click to toggle)
r-cran-future.batchtools 0.10.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 436 kB
  • sloc: sh: 54; makefile: 2
file content (32 lines) | stat: -rw-r--r-- 1,072 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

#' Temporarily tweaks the resources for the current batchtools strategy
#'
#' @usage fassignment \%resources\% tweaks
#'
#' @param fassignment The future assignment, e.g.
#'        \code{x \%<-\% \{ expr \}}.
#' @param tweaks A named list (or vector) of resource \pkg{batchtools}
#' parameters (see Section 'Resources' in [batchtools::submitJobs()])
#' that should be changed relative to the current strategy.
#'
#' @export
#' @importFrom future plan tweak
#' @keywords internal
`%resources%` <- function(fassignment, tweaks) {
  fassignment <- substitute(fassignment)
  envir <- parent.frame(1)
  stop_if_not(is.vector(tweaks))
  tweaks <- as.list(tweaks)
  stop_if_not(!is.null(names(tweaks)))

  ## Temporarily use a different plan
  oplan <- plan("list")
  on.exit(plan(oplan, substitute = FALSE, .call = NULL))

  ## Tweak current strategy and apply
  args <- list(plan("next"), resources = tweaks, penvir = envir)
  strategy <- do.call(tweak, args = args)
  plan(strategy, substitute = FALSE, .call = NULL)

  eval(fassignment, envir = envir, enclos = baseenv())
}