File: tweak_OP.R

package info (click to toggle)
r-cran-future 1.21.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,544 kB
  • sloc: sh: 14; makefile: 2
file content (29 lines) | stat: -rw-r--r-- 974 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
#' Temporarily tweaks the arguments of the current strategy
#'
#' @usage fassignment \%tweak\% tweaks
#'
#' @param fassignment The future assignment, e.g.
#'        `x %<-% { expr }`.
#' @param tweaks A named list (or vector) with arguments that
#' should be changed relative to the current strategy.
#'
#' @export
`%tweak%` <- 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, .cleanup = FALSE, .init = FALSE))

  ## Tweak current strategy and apply
  plans <- oplan
  args <- c(list(plans[[1]], penvir = envir), tweaks)
  plans[[1]] <- do.call(tweak, args = args)
  plan(plans, substitute = FALSE, .call = NULL, .cleanup = FALSE, .init = FALSE)

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