R/filterResults.R

Defines functions filterResults

Documented in filterResults

#' Find all results where a specific condition is true.
#'
#' @template arg_reg
#' @param ids [\code{integer}]\cr
#'   Ids of jobs whose results you want to test for the condition.
#'   Default is all jobs for which results are available.
#' @param fun [\code{fun(job, res)}]\cr
#'   Predicate function that returns \code{TRUE} or \code{FALSE}.
#' @param ... [any]\cr
#'   Additional arguments to \code{fun}.
#' @return [\code{integer}]. Ids of jobs where \code{fun(job, result)} returns \code{TRUE}.
#' @export
#' @examples
#' reg = makeRegistry(id = "BatchJobsExample", file.dir = tempfile(), seed = 123)
#' f = function(x) x^2
#' batchMap(reg, f, 1:10)
#' submitJobs(reg)
#' waitForJobs(reg)
#'
#' # which square numbers are even:
#' filterResults(reg, fun = function(job, res) res %% 2 == 0)
filterResults = function(reg, ids, fun, ...) {
  checkRegistry(reg, writeable = FALSE)
  syncRegistry(reg)
  assertFunction(fun, c("job", "res"))
  if (missing(ids))
    ids = dbFindDone(reg)
  else
    ids = checkIds(reg, ids)

  Filter(function(id) {
    fun(job = getJob(reg, id, check.id = FALSE),
        res = getResult(reg, id),
        ...)
  }, ids)
}

Try the BatchJobs package in your browser

Any scripts or data that you put into this service are public.

BatchJobs documentation built on March 21, 2022, 5:05 p.m.