Skip to content
Back to skills

Foreach

ASecurity

R foreach package for parallel loops. Use for parallel iteration with various backends.

  • 5 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added June 4, 2026
databackend

Security analysis

A100/100

Scanned June 4, 2026

npx -y skills add LeoLin990405/r-analytics-skill --skill foreach --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Foreach?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Foreach
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/leolin990405-foreach/badge)](https://www.skillsdirectory.com/skills/leolin990405-foreach)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: foreach
description: R foreach package for parallel loops. Use for parallel iteration with various backends.
---

# foreach Package

Parallel loops with pluggable backends.

## Setup

```r
library(foreach)
library(doParallel)

# Register parallel backend
cl <- makeCluster(4)
registerDoParallel(cl)

# Check registration
getDoParWorkers()
```

## Basic Loop

```r
# Sequential
result <- foreach(i = 1:100) %do% {
  expensive_function(i)
}

# Parallel
result <- foreach(i = 1:100) %dopar% {
  expensive_function(i)
}
```

## Combine Results

```r
# As vector (c)
result <- foreach(i = 1:100, .combine = c) %dopar% {
  i^2
}

# As data frame (rbind)
result <- foreach(i = 1:100, .combine = rbind) %dopar% {
  data.frame(x = i, y = i^2)
}

# Sum
result <- foreach(i = 1:100, .combine = `+`) %dopar% {
  i
}

# Custom combine
result <- foreach(i = 1:100, .combine = function(a, b) c(a, b)) %dopar% {
  i
}
```

## Multiple Iterators

```r
result <- foreach(a = 1:10, b = 11:20) %dopar% {
  a + b
}

# Nested (all combinations)
result <- foreach(a = 1:3) %:%
  foreach(b = 1:3) %dopar% {
    a * b
  }
```

## Options

```r
result <- foreach(
  i = 1:100,
  .combine = rbind,
  .packages = c("dplyr", "ggplot2"),
  .export = c("my_function", "my_data"),
  .errorhandling = "pass"  # or "remove", "stop"
) %dopar% {
  # code
}
```

## Cleanup

```r
stopCluster(cl)
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…