foreach v1.5.1

0

Monthly downloads

0th

Percentile

Provides Foreach Looping Construct

Support for the foreach looping construct. Foreach is an idiom that allows for iterating over elements in a collection, without the use of an explicit loop counter. This package in particular is intended to be used for its return value, rather than for its side effects. In that sense, it is similar to the standard lapply function, but doesn't require the evaluation of a function. Using foreach without side effects also facilitates executing the loop in parallel.

Readme

foreach

CRAN Downloads R-CMD-check

This package provides support for the foreach looping construct. Foreach is an idiom that allows for iterating over elements in a collection, without the use of an explicit loop counter. The main reason for using this package is that it supports parallel execution, that is, it can execute repeated operations on multiple processors/cores on your computer, or on multiple nodes of a cluster. Many different adapters exist to use foreach with a variety of computational backends, including:

  • doParallel: execute foreach loops on clusters created with base R's parallel package
  • doFuture: using the future framework
  • doRedis: on a Redis database
  • doAzureParallel: on a compute cluster in Azure
  • and more.

Example

A basic for loop in R that fits a set of models:

dat_list <- split(iris, iris$Species)
mod_list <- vector("list", length(dat_list))
for(i in seq_along(dat_list)) {
    mod_list[[i]] <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data=dat_list[[i]])
}

The same using foreach:

library(foreach)
mod_list2 <- foreach(dat=dat_list) %do% {
    lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data=dat)
}

The same, but fit in parallel on a background cluster. We change the %do% operator to %dopar% to indicate parallel processing.

library(doParallel)
registerDoParallel(3)
mod_list2 <- foreach(dat=dat_list) %dopar% {
    lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data=dat)
}

stopImplicitCluster()

Functions in foreach

Name Description
getDoSeqWorkers Functions Providing Information on the doSeq Backend
foreach-ext foreach extension functions
getDoParWorkers Functions Providing Information on the doPar Backend
setDoSeq setDoSeq
registerDoSEQ registerDoSEQ
foreach-package The Foreach Package
setDoPar setDoPar
foreach foreach
No Results!

Vignettes of foreach

Name
foreach.Rmd
nested.Rmd
No Results!

Last month downloads

Details

Type Package
License Apache License (== 2.0)
URL https://github.com/RevolutionAnalytics/foreach
BugReports https://github.com/RevolutionAnalytics/foreach/issues
VignetteBuilder knitr
RoxygenNote 7.0.2.9000
Collate 'callCombine.R' 'foreach.R' 'do.R' 'foreach-ext.R' 'foreach-pkg.R' 'getDoPar.R' 'getDoSeq.R' 'getsyms.R' 'iter.R' 'nextElem.R' 'onLoad.R' 'setDoPar.R' 'setDoSeq.R' 'times.R' 'utils.R'
NeedsCompilation no
Packaged 2020-10-12 16:22:02 UTC; hongo
Repository CRAN
Date/Publication 2020-10-15 05:00:13 UTC

Include our badge in your README

[![Rdoc](http://www.rdocumentation.org/badges/version/foreach)](http://www.rdocumentation.org/packages/foreach)