The foreach package provides a simple looping construct for R: the foreach
function, which you may be familiar with from other languages like Javascript or C#. It's basically a function-based version of a "for"
loop. But what makes foreach useful isn't iteration: it's the way it makes it easy to run those iterations in parallel, and save time on multi-CPU and distributed systems.
If you want to get familiar with the foreach
function, Parallelizing Loops at Microsoft Docs will introduce you to foreach loops (and the companion concept, iterators), and the various "backends" you can use to make the loops run in parallel without changing your loop code. Then, to make sure you've captured the concepts, you can try these 10 parallel computing exercises using foreach, from R-exercises.com. If you get stuck, the solutions are available here.
To get started with foreach, can install the foreach package from CRAN, or simply use any edition of Microsoft R including Microsoft R Open (all come with foreach preinstalled). The foreach package is maintained by Microsoft under the open-source Apache license.
R-exercises: Parallel Computing Exercises: Foreach and DoParallel (Part-2)
Nice post! I found your blog via R Programming Tutorials. Keep up great work for us!
Posted by: Albert | July 29, 2017 at 18:55
You have a typo:
"introduce you to foreach oops"
should be
"introduce you to foreach loops"
Posted by: Corey Levinson | August 09, 2017 at 05:34
Thanks Corey - fixed above.
Posted by: David Smith | August 10, 2017 at 08:04