# Orchestrating Multiple Functions in Parallel and in Background

    library(bakerrr)
    #>
    #> Attaching package: 'bakerrr'
    #> The following object is masked from 'package:base':
    #>
    #>     summary

# Creating a List of Functions and Argument Sets

We define a list of ten functions with different mathematical and string operations. Each receives tailored arguments, including error-producing cases.

    fun_list <- list(
      function(x, y) {
        Sys.sleep(2)
        x + y
      },
      function(x, y) {
        Sys.sleep(2)
        x * y
      },
      function(x, y) {
        Sys.sleep(2)
        x - y
      },
      function(x, y) {
        Sys.sleep(2)
        x / y
      },
      function(x, y) {
        Sys.sleep(2)
        x^y
      },
      function(x, y) {
        Sys.sleep(2)
        x %% y
      },
      function(x, y) {
        Sys.sleep(2)
        paste0(x, "-", y)
      },
      function(x, y) {
        Sys.sleep(2)
        mean(c(x, y))
      },
      function(x, y) {
        Sys.sleep(2)
        max(x, y)
      },
      function(x, y) {
        Sys.sleep(2)
        min(x, y)
      }
    )

    args_list <- list(
      list(x = ceiling(rnorm(1) * 10), y = ceiling(rnorm(1) * 10)),
      list(x = "p", y = ceiling(rnorm(1) * 10)),             # type error
      list(x = ceiling(rnorm(1) * 10), y = ceiling(rnorm(1) * 10)),
      list(x = ceiling(rnorm(1) * 10), y = ceiling(rnorm(1) * 10)),
      list(x = ceiling(rnorm(1) * 10), y = ceiling(rnorm(1) * 5)),
      list(x = ceiling(rnorm(1) * 10), y = ceiling(rnorm(1) * 3)),
      list(x = sample(LETTERS, 1), y = ceiling(rnorm(1) * 10)), # likely type error
      list(x = ceiling(rnorm(1) * 10), y = ceiling(rnorm(1) * 10)),
      list(x = ceiling(rnorm(1) * 10), y = ceiling(rnorm(1) * 10)),
      list(x = ceiling(rnorm(1) * 10), y = ceiling(rnorm(1) * 10))
    )

# Running the Jobs

Create a {bakerrr} object with parallel execution using four daemons, then execute and collect results.

    new_baker <- bakerrr(
      fun = fun_list,
      args_list = args_list,
      n_daemons = 4
    ) |>
      run_jobs(wait_for_results = TRUE)

# Inspect Status and Results

Examine job outcomes and statuses. Errors and return values are captured per job.

    new_baker@results
    #> [[1]]
    #> [1] -8
    #>
    #> [[2]]
    #> Error in purrr::in_parallel: non-numeric argument to binary operator
    #>
    #> [[3]]
    #> [1] -13
    #>
    #> [[4]]
    #> [1] 0.375
    #>
    #> [[5]]
    #> [1] 36
    #>
    #> [[6]]
    #> [1] -1
    #>
    #> [[7]]
    #> [1] "X--13"
    #>
    #> [[8]]
    #> [1] 4
    #>
    #> [[9]]
    #> [1] 13
    #>
    #> [[10]]
    #> [1] -4

    new_baker |>
      print()
    #>
    #> ✅ bakerrr
    #> ├─ Status: COMPLETED
    #> ├─ Functions:
    #>    [01] function (x, y) { Sys.sleep(2) x + y }
    #>    [02] function (x, y) { Sys.sleep(2) x * y }
    #>    [03] function (x, y) { Sys.sleep(2) x - y }
    #>    [04] function (x, y) { Sys.sleep(2) x/y }
    #>    [05] function (x, y) { Sys.sleep(2) x^y }
    #>    [06] function (x, y) { Sys.sleep(2) x%%y }
    #>    [07] function (x, y) { Sys.sleep(2) paste0(x, "-", y) }
    #>    [08] function (x, y) { Sys.sleep(2) mean(c(x, y)) }
    #>    [09] function (x, y) { Sys.sleep(2) max(x, y) }
    #>    [10] function (x, y) { Sys.sleep(2) min(x, y) }
    #> ├─ Args: 10 sets
    #> ├─ Daemons: 4
    #> ├─ Cleanup: enabled
    #> ├─ Process alive: FALSE
    #> ├─ Result:
    #>      └─ List with 10 elements

    new_baker |>
      summary()
    #> ✅ bakerrr | Status: COMPLETED | Functions: 10 | Daemons: 4 | Jobs: 10

    new_baker |>
      status()
    #> [1] "done"

# Notes

- {bakerrr} runs each function/argument pair in the background, improving throughput for batch operations.
- Errors are reported in the results slot—you can test robustness for diverse function types and argument mismatches.
- For fine-grained control, adjust n_daemons or supply additional job parameters via bg_args.
- Check object summary and status for detailed job diagnostics.

For more information, see the package reference and extended vignettes.