469 lines
55 KiB
HTML
Raw Normal View History

2025-01-12 00:52:51 +08:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="Steve Weston" />
<title>Using the foreach package</title>
<script src="data:application/javascript;base64,Ly8gUGFuZG9jIDIuOSBhZGRzIGF0dHJpYnV0ZXMgb24gYm90aCBoZWFkZXIgYW5kIGRpdi4gV2UgcmVtb3ZlIHRoZSBmb3JtZXIgKHRvCi8vIGJlIGNvbXBhdGlibGUgd2l0aCB0aGUgYmVoYXZpb3Igb2YgUGFuZG9jIDwgMi44KS4KZG9jdW1lbnQuYWRkRXZlbnRMaXN0ZW5lcignRE9NQ29udGVudExvYWRlZCcsIGZ1bmN0aW9uKGUpIHsKICB2YXIgaHMgPSBkb2N1bWVudC5xdWVyeVNlbGVjdG9yQWxsKCJkaXYuc2VjdGlvbltjbGFzcyo9J2xldmVsJ10gPiA6Zmlyc3QtY2hpbGQiKTsKICB2YXIgaSwgaCwgYTsKICBmb3IgKGkgPSAwOyBpIDwgaHMubGVuZ3RoOyBpKyspIHsKICAgIGggPSBoc1tpXTsKICAgIGlmICghL15oWzEtNl0kL2kudGVzdChoLnRhZ05hbWUpKSBjb250aW51ZTsgIC8vIGl0IHNob3VsZCBiZSBhIGhlYWRlciBoMS1oNgogICAgYSA9IGguYXR0cmlidXRlczsKICAgIHdoaWxlIChhLmxlbmd0aCA+IDApIGgucmVtb3ZlQXR0cmlidXRlKGFbMF0ubmFtZSk7CiAgfQp9KTsK"></script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
for (var j = 0; j < rules.length; j++) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue;
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') continue;
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<link rel="stylesheet" href="data:text/css,body%20%7B%0Abackground%2Dcolor%3A%20%23fff%3B%0Amargin%3A%201em%20auto%3B%0Amax%2Dwidth%3A%20700px%3B%0Aoverflow%3A%20visible%3B%0Apadding%2Dleft%3A%202em%3B%0Apadding%2Dright%3A%202em%3B%0Afont%2Dfamily%3A%20%22Open%20Sans%22%2C%20%22Helvetica%20Neue%22%2C%20Helvetica%2C%20Arial%2C%20sans%2Dserif%3B%0Afont%2Dsize%3A%2014px%3B%0Aline%2Dheight%3A%201%2E35%3B%0A%7D%0A%23TOC%20%7B%0Aclear%3A%20both%3B%0Amargin%3A%200%200%2010px%2010px%3B%0Apadding%3A%204px%3B%0Awidth%3A%20400px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Aborder%2Dradius%3A%205px%3B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Afont%2Dsize%3A%2013px%3B%0Aline%2Dheight%3A%201%2E3%3B%0A%7D%0A%23TOC%20%2Etoctitle%20%7B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%2015px%3B%0Amargin%2Dleft%3A%205px%3B%0A%7D%0A%23TOC%20ul%20%7B%0Apadding%2Dleft%3A%2040px%3B%0Amargin%2Dleft%3A%20%2D1%2E5em%3B%0Amargin%2Dtop%3A%205px%3B%0Amargin%2Dbottom%3A%205px%3B%0A%7D%0A%23TOC%20ul%20ul%20%7B%0Amargin%2Dleft%3A%20%2D2em%3B%0A%7D%0A%23TOC%20li%20%7B%0Aline%2Dheight%3A%2016px%3B%0A%7D%0Atable%20%7B%0Amargin%3A%201em%20auto%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20%23DDDDDD%3B%0Aborder%2Dstyle%3A%20outset%3B%0Aborder%2Dcollapse%3A%20collapse%3B%0A%7D%0Atable%20th%20%7B%0Aborder%2Dwidth%3A%202px%3B%0Apadding%3A%205px%3B%0Aborder%2Dstyle%3A%20inset%3B%0A%7D%0Atable%20td%20%7B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dstyle%3A%20inset%3B%0Aline%2Dheight%3A%2018px%3B%0Apadding%3A%205px%205px%3B%0A%7D%0Atable%2C%20table%20th%2C%20table%20td%20%7B%0Aborder%2Dleft%2Dstyle%3A%20none%3B%0Aborder%2Dright%2Dstyle%3A%20none%3B%0A%7D%0Atable%20thead%2C%20table%20tr%2Eeven%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Ap%20%7B%0Amargin%3A%200%2E5em%200%3B%0A%7D%0Ablockquote%20%7B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Apadding%3A%200%2E25em%200%2E75em%3B%0A%7D%0Ahr%20%7B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%3A%20none%3B%0Aborder%2Dtop%3A%201px%20solid%20%23777%3B%0Amargin%3A%2028px%200%3B%0A%7D%0Adl%20%7B%0Amargin%2Dleft%3A%200%3B%0A%7D%0Adl%20dd%20%7B%0Amargin%2Dbottom%3A%2013px%3B%0Amargin%2Dleft%3A%2013px%3B%0A%7D%0Adl%20dt%20%7B%0Afont%2Dweight%3A%20bold%3B%0A%7D%0Aul%20%7B%0Amargin%2Dtop%3A%200%3B%0A%7D%0Aul%20li%20%7B%0Alist%2Dstyle%3A%20circle%20outside%3B%0A%7D%0Aul%20ul%20%7B%0Amargin%2Dbottom%3A%200%3B%0A%7D%0Apre%2C%20code%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0Aborder%2Dradius%3A%203px%3B%0Acolor%3A%20%23333%3B%0Awhite%2Dspace%3A%20pre%2Dwrap%3B%20%0A%7D%0Apre%20%7B%0Aborder%2Dradius%3A%203px%3B%0Amargin%3A%205px%200px%2010px%200px%3B%0Apadding%3A%2010px%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Acode%20%7B%0Afont%2Dfamily%3A%20Consolas%2C%20Monaco%2C%20%27Courier%20New%27%2C%20monospace%3B%0Afont%2Dsize%3A%2085%25%3B%0A%7D%0Ap%20%3E%20code%2C%20li%20%3E%20code%20%7B%0Apadding%3A%202px%200px%3B%0A%7D%0Adiv%2Efigure%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0Aimg%20%7B%0Abackground%2Dcolor%3A%20%23FFFFFF%3B%0Apadding%3A%202px%3B%0Aborder%3A%201px%20solid%20%23DDDDDD%3B%0Aborder%2Dradius%3A%203px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Amargin%3A%200%205px%3B%0A%7D%0Ah1%20%7B%0Amargin%2Dtop%3A%200%3B%0Afont%2Dsize%3A%2035px%3B%0Aline%2Dheight%3A%2040px%3B%0A%7D%0Ah2%20%7B%0Aborder%2Dbottom%3A%204px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Apadding%2Dbottom%3A%202px%3B%0Afont%2Dsize%3A%20145%25%3B%0A%7D%0Ah3%20%7B%0Aborder%2Dbottom%3A%202px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Ah4%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23f7f7f7%3B%0Amargin%2Dleft%3A%208px%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Ah5%2C%20h6%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23ccc%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Aa%20%7B%0Acolor%3A%20%230033dd%3B%0Atext%2Ddecoration%3A%20none%3B%0A%7D%0Aa%3Ahover%20%7B%0Acolor%3A%20%236666ff%3B%20%7D%0Aa%3Avisited%20%7B%0Acolor%3A%20%23800080%3B%20%7D%0Aa%3Avisited%3Ahover%20%7B%0Acolor%3A%20%23BB00BB%3B%20%7D%0Aa%5Bhref%5E%3D%22http%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%
</head>
<body>
<h1 class="title toc-ignore">Using the <code>foreach</code> package</h1>
<h4 class="author">Steve Weston</h4>
<p><em>Converted to RMarkdown by Hong Ooi</em></p>
<div id="introduction" class="section level2">
<h2>Introduction</h2>
<p>One of Rs most useful features is its interactive interpreter. This makes it very easy to learn and experiment with R. It allows you to use R like a calculator to perform arithmetic operations, display data sets, generate plots, and create models.</p>
<p>Before too long, new R users will find a need to perform some operation repeatedly. Perhaps they want to run a simulation repeatedly in order to find the distribution of the results. Perhaps they need to execute a function with a variety a different arguments passed to it. Or maybe they need to create a model for many different data sets.</p>
<p>Repeated executions can be done manually, but it becomes quite tedious to execute repeated operations, even with the use of command line editing. Fortunately, R is much more than an interactive calculator. It has its own built-in language that is intended to automate tedious tasks, such as repeatedly executing R calculations.</p>
<p>R comes with various looping constructs that solve this problem. The <code>for</code> loop is one of the more common looping constructs, but the <code>repeat</code> and <code>while</code> statements are also quite useful. In addition, there is the family of “apply” functions, which includes <code>apply</code>, <code>lapply</code>, <code>sapply</code>, <code>eapply</code>, <code>mapply</code>, <code>rapply</code>, and others.</p>
<p>The <code>foreach</code> package provides a new looping construct for executing R code repeatedly. With the bewildering variety of existing looping constructs, you may doubt that there is a need for yet another construct. The main reason for using the <code>foreach</code> package is that it supports <em>parallel execution</em>, that is, it can execute those repeated operations on multiple processors/cores on your computer, or on multiple nodes of a cluster. If each operation takes over a minute, and you want to execute it hundreds of times, the overall runtime can take hours. But using <code>foreach</code>, that operation can be executed in parallel on hundreds of processors on a cluster, reducing the execution time back down to minutes.</p>
<p>But parallel execution is not the only reason for using the <code>foreach</code> package. There are other reasons that you might choose to use it to execute quick executing operations, as we will see later in the document.</p>
</div>
<div id="getting-started" class="section level2">
<h2>Getting Started</h2>
<p>Lets take a look at a simple example use of the <code>foreach</code> package. Assuming that you have the <code>foreach</code> package installed, you first need to load it:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(foreach)</span></code></pre></div>
<p>Note that all of the packages that <code>foreach</code> depends on will be loaded as well.</p>
<p>Now I can use <code>foreach</code> to execute the <code>sqrt</code> function repeatedly, passing it the values 1 through 3, and returning the results in a list, called <code>x</code>. (Of course, <code>sqrt</code> is a vectorized function, so you would never really do this. But later, well see how to take advantage of vectorized functions with <code>foreach</code>.)</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>) <span class="sc">%do%</span> <span class="fu">sqrt</span>(i)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## [[1]]
## [1] 1
##
## [[2]]
## [1] 1.414214
##
## [[3]]
## [1] 1.732051</code></pre>
<p>This is a bit odd looking, because it looks vaguely like a <code>for</code> loop, but is implemented using a binary operator, called <code>%do%</code>. Also, unlike a <code>for</code> loop, it returns a value. This is quite important. The purpose of this statement is to compute the list of results. Generally, <code>foreach</code> with <code>%do%</code> is used to execute an R expression repeatedly, and return the results in some data structure or object, which is a list by default.</p>
<p>You will note in the previous example that we used a variable <code>i</code> as the argument to the <code>sqrt</code> function. We specified the values of the <code>i</code> variable using a named argument to the <code>foreach</code> function. We could have called that variable anything we wanted, for example, <code>a</code>, or <code>b</code>. We could also specify other variables to be used in the R expression, as in the following example:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">a=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="at">b=</span><span class="fu">rep</span>(<span class="dv">10</span>, <span class="dv">3</span>)) <span class="sc">%do%</span> (a <span class="sc">+</span> b)</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## [[1]]
## [1] 11
##
## [[2]]
## [1] 12
##
## [[3]]
## [1] 13</code></pre>
<p>Note that parentheses are needed here. We can also use braces:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">a=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="at">b=</span><span class="fu">rep</span>(<span class="dv">10</span>, <span class="dv">3</span>)) <span class="sc">%do%</span> {</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> a <span class="sc">+</span> b</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## [[1]]
## [1] 11
##
## [[2]]
## [1] 12
##
## [[3]]
## [1] 13</code></pre>
<p>We call <code>a</code> and <code>b</code> the <em>iteration variables</em>, since those are the variables that are changing during the multiple executions. Note that we are iterating over them in parallel, that is, they are both changing at the same time. In this case, the same number of values are being specified for both iteration variables, but that need not be the case. If we only supplied two values for <code>b</code>, the result would be a list of length two, even if we specified a thousand values for <code>a</code>:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">a=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">1000</span>, <span class="at">b=</span><span class="fu">rep</span>(<span class="dv">10</span>, <span class="dv">2</span>)) <span class="sc">%do%</span> {</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> a <span class="sc">+</span> b</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## [[1]]
## [1] 11
##
## [[2]]
## [1] 12</code></pre>
<p>Note that you can put multiple statements between the braces, and you can use assignment statements to save intermediate values of computations. However, if you use an assignment as a way of communicating between the different executions of your loop, then your code wont work correctly in parallel, which we will discuss later.</p>
<p>So far, all of our examples have returned a list of results. This is a good default, since a list can contain any R object. But sometimes wed like the results to be returned in a numeric vector, for example. This can be done by using the <code>.combine</code> option to <code>foreach</code>:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="at">.combine=</span><span class="st">&#39;c&#39;</span>) <span class="sc">%do%</span> <span class="fu">exp</span>(i)</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## [1] 2.718282 7.389056 20.085537</code></pre>
<p>The result is returned as a numeric vector, because the standard R <code>c</code> function is being used to concatenate all the results. Since the <code>exp</code> function returns numeric values, concatenating them with the <code>c</code> function will result in a numeric vector of length three.</p>
<p>What if the R expression returns a vector, and we want to combine those vectors into a matrix? One way to do that is with the <code>cbind</code> function:</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>, <span class="at">.combine=</span><span class="st">&#39;cbind&#39;</span>) <span class="sc">%do%</span> <span class="fu">rnorm</span>(<span class="dv">4</span>)</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## result.1 result.2 result.3 result.4
## [1,] 0.3840828 0.62740622 -1.2902022 -0.7882371
## [2,] 0.4766576 -0.20341217 0.6159496 -0.5188565
## [3,] -2.8284251 0.08052302 1.0873542 2.0558216
## [4,] -2.9056936 -0.45778792 -0.1596889 0.4339473</code></pre>
<p>This generates four vectors of four random numbers, and combines them by column to produce a 4 by 4 matrix.</p>
<p>We can also use the <code>&quot;+&quot;</code> or <code>&quot;*&quot;</code> functions to combine our results:</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>, <span class="at">.combine=</span><span class="st">&#39;+&#39;</span>) <span class="sc">%do%</span> <span class="fu">rnorm</span>(<span class="dv">4</span>)</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## [1] 0.2938563 -3.7247385 0.3259131 4.3481797</code></pre>
<p>You can also specify a user-written function to combine the results. Heres an example that throws away the results:</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>cfun <span class="ot">&lt;-</span> <span class="cf">function</span>(a, b) <span class="cn">NULL</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>, <span class="at">.combine=</span><span class="st">&#39;cfun&#39;</span>) <span class="sc">%do%</span> <span class="fu">rnorm</span>(<span class="dv">4</span>)</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## NULL</code></pre>
<p>Note that this <code>cfun</code> function takes two arguments. The <code>foreach</code> function knows that the functions <code>c</code>, <code>cbind</code>, and <code>rbind</code> take many arguments, and will call them with up to 100 arguments (by default) in order to improve performance. But if any other function is specified (such as <code>&quot;+&quot;</code>), it assumes that it only takes two arguments. If the function does allow many arguments, you can specify that using the <code>.multicombine</code> argument:</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>cfun <span class="ot">&lt;-</span> <span class="cf">function</span>(...) <span class="cn">NULL</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>, <span class="at">.combine=</span><span class="st">&#39;cfun&#39;</span>, <span class="at">.multicombine=</span><span class="cn">TRUE</span>) <span class="sc">%do%</span> <span class="fu">rnorm</span>(<span class="dv">4</span>)</span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## NULL</code></pre>
<p>If you want the combine function to be called with no more than 10 arguments, you can specify that using the <code>.maxcombine</code> option:</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>cfun <span class="ot">&lt;-</span> <span class="cf">function</span>(...) <span class="cn">NULL</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>, <span class="at">.combine=</span><span class="st">&#39;cfun&#39;</span>, <span class="at">.multicombine=</span><span class="cn">TRUE</span>, <span class="at">.maxcombine=</span><span class="dv">10</span>) <span class="sc">%do%</span> <span class="fu">rnorm</span>(<span class="dv">4</span>)</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## NULL</code></pre>
<p>The <code>.inorder</code> option is used to specify whether the order in which the arguments are combined is important. The default value is <code>TRUE</code>, but if the combine function is <code>&quot;+&quot;</code>, you could specify <code>.inorder</code> to be <code>FALSE</code>. Actually, this option is important only when executing the R expression in parallel, since results are always computed in order when running sequentially. This is not necessarily true when executing in parallel, however. In fact, if the expressions take very different lengths of time to execute, the results could be returned in any order. Heres a contrived example, that executes the tasks in parallel to demonstrate the difference. The example uses the <code>Sys.sleep</code> function to cause the earlier tasks to take longer to execute:</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">.combine=</span><span class="st">&#39;c&#39;</span>) <span class="sc">%dopar%</span> {</span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">Sys.sleep</span>(<span class="dv">3</span> <span class="sc">*</span> i)</span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a> i</span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<pre><code>## Warning: executing %dopar% sequentially: no parallel backend registered</code></pre>
<pre><code>## [1] 4 3 2 1</code></pre>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">4</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">.combine=</span><span class="st">&#39;c&#39;</span>, <span class="at">.inorder=</span><span class="cn">FALSE</span>) <span class="sc">%dopar%</span> {</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">Sys.sleep</span>(<span class="dv">3</span> <span class="sc">*</span> i)</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> i</span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<pre><code>## [1] 4 3 2 1</code></pre>
<p>The results of the first of these two examples is guaranteed to be the vector <code>c(4, 3, 2, 1)</code>. The second example will return the same values, but they will probably be in a different order.</p>
</div>
<div id="iterators" class="section level2">
<h2>Iterators</h2>
<p>The values for the iteration variables dont have to be specified with only vectors or lists. They can be specified with an <em>iterator</em>, many of which come with the <code>iterators</code> package. An iterator is an abstract source of data. A vector isnt itself an iterator, but the <code>foreach</code> function automatically creates an iterator from a vector, list, matrix, or data frame, for example. You can also create an iterator from a file or a data base query, which are natural sources of data. The <code>iterators</code> package supplies a function called <code>irnorm</code> which can return a specified number of random numbers for each time it is called. For example:</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(iterators)</span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">a=</span><span class="fu">irnorm</span>(<span class="dv">4</span>, <span class="at">count=</span><span class="dv">4</span>), <span class="at">.combine=</span><span class="st">&#39;cbind&#39;</span>) <span class="sc">%do%</span> a</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## result.1 result.2 result.3 result.4
## [1,] 2.4343632 2.2008835 1.4623745 -1.13398436
## [2,] -1.1027577 0.9405237 -1.1884264 0.55151957
## [3,] 0.8925718 1.1016687 0.2382548 -1.26881301
## [4,] 0.9431755 -0.6531459 -0.5754912 -0.06051963</code></pre>
<p>This becomes useful when dealing with large amounts of data. Iterators allow the data to be generated on-the-fly, as it is needed by your operations, rather than requiring all of the data to be generated at the beginning.</p>
<p>For example, lets say that we want to sum together a thousand random vectors:</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">a=</span><span class="fu">irnorm</span>(<span class="dv">4</span>, <span class="at">count=</span><span class="dv">1000</span>), <span class="at">.combine=</span><span class="st">&#39;+&#39;</span>) <span class="sc">%do%</span> a</span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## [1] 9.097676 -13.106472 14.076261 19.252750</code></pre>
<p>This uses very little memory, since it is equivalent to the following <code>while</code> loop:</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">numeric</span>(<span class="dv">4</span>)</span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a>i <span class="ot">&lt;-</span> <span class="dv">0</span></span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a><span class="cf">while</span> (i <span class="sc">&lt;</span> <span class="dv">1000</span>) {</span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a> x <span class="ot">&lt;-</span> x <span class="sc">+</span> <span class="fu">rnorm</span>(<span class="dv">4</span>)</span>
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a> i <span class="ot">&lt;-</span> i <span class="sc">+</span> <span class="dv">1</span></span>
<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb31-8"><a href="#cb31-8" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## [1] 9.097676 -13.106472 14.076261 19.252750</code></pre>
<p>This could have been done using the <code>icount</code> function, which generates the values from one to 1000:</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="fu">icount</span>(<span class="dv">1000</span>), <span class="at">.combine=</span><span class="st">&#39;+&#39;</span>) <span class="sc">%do%</span> <span class="fu">rnorm</span>(<span class="dv">4</span>)</span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## [1] 9.097676 -13.106472 14.076261 19.252750</code></pre>
<p>but sometimes its preferable to generate the actual data with the iterator (as well see later when we execute in parallel).</p>
<p>In addition to introducing the <code>icount</code> function from the <code>iterators</code> package, the last example also used an unnamed argument to the <code>foreach</code> function. This can be useful when were not intending to generate variable values, but only controlling the number of times that the R expression is executed.</p>
<p>Theres a lot more that I could say about iterators, but for now, lets move on to parallel execution.</p>
</div>
<div id="parallel-execution" class="section level2">
<h2>Parallel Execution</h2>
<p>Although <code>foreach</code> can be a useful construct in its own right, the real point of the <code>foreach</code> package is to do parallel computing. To make any of the previous examples run in parallel, all you have to do is to replace <code>%do%</code> with <code>%dopar%</code>. But for the kinds of quick running operations that weve been doing, there wouldnt be much point to executing them in parallel. Running many tiny tasks in parallel will usually take more time to execute than running them sequentially, and if it already runs fast, theres no motivation to make it run faster anyway. But if the operation that were executing in parallel takes a minute or longer, there starts to be some motivation.</p>
<div id="parallel-random-forest" class="section level3">
<h3>Parallel Random Forest</h3>
<p>Lets take random forest as an example of an operation that can take a while to execute. Lets say our inputs are the matrix <code>x</code>, and the factor <code>y</code>:</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">matrix</span>(<span class="fu">runif</span>(<span class="dv">500</span>), <span class="dv">100</span>)</span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a>y <span class="ot">&lt;-</span> <span class="fu">gl</span>(<span class="dv">2</span>, <span class="dv">50</span>)</span></code></pre></div>
<p>Weve already loaded the <code>foreach</code> package, but well also need to load the <code>randomForest</code> package:</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(randomForest)</span></code></pre></div>
<pre><code>## randomForest 4.6-14</code></pre>
<pre><code>## Type rfNews() to see new features/changes/bug fixes.</code></pre>
<p>If we want want to create a random forest model with a 1000 trees, and our computer has four cores in it, we can split up the problem into four pieces by executing the <code>randomForest</code> function four times, with the <code>ntree</code> argument set to 250. Of course, we have to combine the resulting <code>randomForest</code> objects, but the <code>randomForest</code> package comes with a function called <code>combine</code> that does just that.</p>
<p>Lets do that, but first, well do the work sequentially:</p>
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a>rf <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">ntree=</span><span class="fu">rep</span>(<span class="dv">250</span>, <span class="dv">4</span>), <span class="at">.combine=</span>combine) <span class="sc">%do%</span></span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">randomForest</span>(x, y, <span class="at">ntree=</span>ntree)</span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a>rf</span></code></pre></div>
<pre><code>##
## Call:
## randomForest(x = x, y = y, ntree = ntree)
## Type of random forest: classification
## Number of trees: 1000
## No. of variables tried at each split: 2</code></pre>
<p>To run this in parallel, we need to change <code>\%do\%</code>, but we also need to use another <code>foreach</code> option called <code>.packages</code> to tell the <code>foreach</code> package that the R expression needs to have the <code>randomForest</code> package loaded in order to execute successfully. Heres the parallel version:</p>
<div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>rf <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">ntree=</span><span class="fu">rep</span>(<span class="dv">250</span>, <span class="dv">4</span>), <span class="at">.combine=</span>combine, <span class="at">.packages=</span><span class="st">&#39;randomForest&#39;</span>) <span class="sc">%dopar%</span></span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">randomForest</span>(x, y, <span class="at">ntree=</span>ntree)</span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a>rf</span></code></pre></div>
<pre><code>##
## Call:
## randomForest(x = x, y = y, ntree = ntree)
## Type of random forest: classification
## Number of trees: 1000
## No. of variables tried at each split: 2</code></pre>
<p>If youve done any parallel computing, particularly on a cluster, you may wonder why I didnt have to do anything special to handle <code>x</code> and <code>y</code>. The reason is that the <code>dopar</code> function noticed that those variables were referenced, and that they were defined in the current environment. In that case <code>%dopar%</code> will automatically export them to the parallel execution workers once, and use them for all of the expression evaluations for that <code>foreach</code> execution. That is true for functions that are defined in the current environment as well, but in this case, the function is defined in a package, so we had to specify the package to load with the <code>.packages</code> option instead.</p>
</div>
<div id="parallel-apply" class="section level3">
<h3>Parallel Apply</h3>
<p>Now lets take a look at how to make a parallel version of the standard R <code>apply</code> function. The <code>apply</code> function is written in R, and although its only about 100 lines of code, its a bit difficult to understand on a first reading. However, it all really comes down two <code>for</code> loops, the slightly more complicated of which looks like:</p>
<div class="sourceCode" id="cb43"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>applyKernel <span class="ot">&lt;-</span> <span class="cf">function</span>(newX, FUN, d2, d.call, <span class="at">dn.call=</span><span class="cn">NULL</span>, ...) {</span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a> ans <span class="ot">&lt;-</span> <span class="fu">vector</span>(<span class="st">&quot;list&quot;</span>, d2)</span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>d2) {</span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a> tmp <span class="ot">&lt;-</span> <span class="fu">FUN</span>(<span class="fu">array</span>(newX[,i], d.call, dn.call), ...)</span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(<span class="sc">!</span><span class="fu">is.null</span>(tmp)) ans[[i]] <span class="ot">&lt;-</span> tmp</span>
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb43-7"><a href="#cb43-7" aria-hidden="true" tabindex="-1"></a> ans</span>
<span id="cb43-8"><a href="#cb43-8" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb43-9"><a href="#cb43-9" aria-hidden="true" tabindex="-1"></a><span class="fu">applyKernel</span>(<span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">16</span>, <span class="dv">4</span>), mean, <span class="dv">4</span>, <span class="dv">4</span>)</span></code></pre></div>
<pre><code>## [[1]]
## [1] 2.5
##
## [[2]]
## [1] 6.5
##
## [[3]]
## [1] 10.5
##
## [[4]]
## [1] 14.5</code></pre>
<p>Ive turned this into a function, because otherwise, R will complain that Im using <code>...</code> in an invalid context.</p>
<p>This could be executed using <code>foreach</code> as follows:</p>
<div class="sourceCode" id="cb45"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>applyKernel <span class="ot">&lt;-</span> <span class="cf">function</span>(newX, FUN, d2, d.call, <span class="at">dn.call=</span><span class="cn">NULL</span>, ...) {</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span>d2) <span class="sc">%dopar%</span></span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">FUN</span>(<span class="fu">array</span>(newX[,i], d.call, dn.call), ...)</span>
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb45-5"><a href="#cb45-5" aria-hidden="true" tabindex="-1"></a><span class="fu">applyKernel</span>(<span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">16</span>, <span class="dv">4</span>), mean, <span class="dv">4</span>, <span class="dv">4</span>)</span></code></pre></div>
<pre><code>## [[1]]
## [1] 2.5
##
## [[2]]
## [1] 6.5
##
## [[3]]
## [1] 10.5
##
## [[4]]
## [1] 14.5</code></pre>
<p>But this approach will cause the entire <code>newX</code> array to be sent to each of the parallel execution workers. Since each task needs only one column of the array, wed like to avoid this extra data communication.</p>
<p>One way to solve this problem is to use an iterator that iterates over the matrix by column:</p>
<div class="sourceCode" id="cb47"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a>applyKernel <span class="ot">&lt;-</span> <span class="cf">function</span>(newX, FUN, d2, d.call, <span class="at">dn.call=</span><span class="cn">NULL</span>, ...) {</span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">foreach</span>(<span class="at">x=</span><span class="fu">iter</span>(newX, <span class="at">by=</span><span class="st">&#39;col&#39;</span>)) <span class="sc">%dopar%</span></span>
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">FUN</span>(<span class="fu">array</span>(x, d.call, dn.call), ...)</span>
<span id="cb47-4"><a href="#cb47-4" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb47-5"><a href="#cb47-5" aria-hidden="true" tabindex="-1"></a><span class="fu">applyKernel</span>(<span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">16</span>, <span class="dv">4</span>), mean, <span class="dv">4</span>, <span class="dv">4</span>)</span></code></pre></div>
<pre><code>## [[1]]
## [1] 2.5
##
## [[2]]
## [1] 6.5
##
## [[3]]
## [1] 10.5
##
## [[4]]
## [1] 14.5</code></pre>
<p>Now were only sending any given column of the matrix to one parallel execution worker. But it would be even more efficient if we sent the matrix in bigger chunks. To do that, we use a function called <code>iblkcol</code> that returns an iterator that will return multiple columns of the original matrix. That means that the R expression will need to execute the users function once for every column in its submatrix.</p>
<div class="sourceCode" id="cb49"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a>iblkcol <span class="ot">&lt;-</span> <span class="cf">function</span>(a, chunks) {</span>
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a> n <span class="ot">&lt;-</span> <span class="fu">ncol</span>(a)</span>
<span id="cb49-3"><a href="#cb49-3" aria-hidden="true" tabindex="-1"></a> i <span class="ot">&lt;-</span> <span class="dv">1</span></span>
<span id="cb49-4"><a href="#cb49-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb49-5"><a href="#cb49-5" aria-hidden="true" tabindex="-1"></a> nextElem <span class="ot">&lt;-</span> <span class="cf">function</span>() {</span>
<span id="cb49-6"><a href="#cb49-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (chunks <span class="sc">&lt;=</span> <span class="dv">0</span> <span class="sc">||</span> n <span class="sc">&lt;=</span> <span class="dv">0</span>) <span class="fu">stop</span>(<span class="st">&#39;StopIteration&#39;</span>)</span>
<span id="cb49-7"><a href="#cb49-7" aria-hidden="true" tabindex="-1"></a> m <span class="ot">&lt;-</span> <span class="fu">ceiling</span>(n <span class="sc">/</span> chunks)</span>
<span id="cb49-8"><a href="#cb49-8" aria-hidden="true" tabindex="-1"></a> r <span class="ot">&lt;-</span> <span class="fu">seq</span>(i, <span class="at">length=</span>m)</span>
<span id="cb49-9"><a href="#cb49-9" aria-hidden="true" tabindex="-1"></a> i <span class="ot">&lt;&lt;-</span> i <span class="sc">+</span> m</span>
<span id="cb49-10"><a href="#cb49-10" aria-hidden="true" tabindex="-1"></a> n <span class="ot">&lt;&lt;-</span> n <span class="sc">-</span> m</span>
<span id="cb49-11"><a href="#cb49-11" aria-hidden="true" tabindex="-1"></a> chunks <span class="ot">&lt;&lt;-</span> chunks <span class="sc">-</span> <span class="dv">1</span></span>
<span id="cb49-12"><a href="#cb49-12" aria-hidden="true" tabindex="-1"></a> a[,r, drop<span class="ot">=</span><span class="cn">FALSE</span>]</span>
<span id="cb49-13"><a href="#cb49-13" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb49-14"><a href="#cb49-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb49-15"><a href="#cb49-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">structure</span>(<span class="fu">list</span>(<span class="at">nextElem=</span>nextElem), <span class="at">class=</span><span class="fu">c</span>(<span class="st">&#39;iblkcol&#39;</span>, <span class="st">&#39;iter&#39;</span>))</span>
<span id="cb49-16"><a href="#cb49-16" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb49-17"><a href="#cb49-17" aria-hidden="true" tabindex="-1"></a>nextElem.iblkcol <span class="ot">&lt;-</span> <span class="cf">function</span>(obj) obj<span class="sc">$</span><span class="fu">nextElem</span>()</span></code></pre></div>
<div class="sourceCode" id="cb50"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a>applyKernel <span class="ot">&lt;-</span> <span class="cf">function</span>(newX, FUN, d2, d.call, <span class="at">dn.call=</span><span class="cn">NULL</span>, ...) {</span>
<span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">foreach</span>(<span class="at">x=</span><span class="fu">iblkcol</span>(newX, <span class="dv">3</span>), <span class="at">.combine=</span><span class="st">&#39;c&#39;</span>, <span class="at">.packages=</span><span class="st">&#39;foreach&#39;</span>) <span class="sc">%dopar%</span> {</span>
<span id="cb50-3"><a href="#cb50-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">foreach</span>(<span class="at">i=</span><span class="dv">1</span><span class="sc">:</span><span class="fu">ncol</span>(x)) <span class="sc">%do%</span> <span class="fu">FUN</span>(<span class="fu">array</span>(x[,i], d.call, dn.call), ...)</span>
<span id="cb50-4"><a href="#cb50-4" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb50-5"><a href="#cb50-5" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb50-6"><a href="#cb50-6" aria-hidden="true" tabindex="-1"></a><span class="fu">applyKernel</span>(<span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">16</span>, <span class="dv">4</span>), mean, <span class="dv">4</span>, <span class="dv">4</span>)</span></code></pre></div>
<pre><code>## [[1]]
## [1] 2.5
##
## [[2]]
## [1] 6.5
##
## [[3]]
## [1] 10.5
##
## [[4]]
## [1] 14.5</code></pre>
<p>Note the use of the <code>%do%</code> inside the <code>%dopar%</code> to call the function on the columns of the submatrix <code>x</code>. Now that were using <code>%do%</code> again, it makes sense for the iterator to be an index into the matrix <code>x</code>, since <code>%do%</code> doesnt need to copy <code>x</code> the way that <code>%dopar%</code> does.</p>
</div>
</div>
<div id="list-comprehensions" class="section level2">
<h2>List Comprehensions</h2>
<p>If youre familiar with the Python programming language, it may have occurred to you that the <code>foreach</code> package provides something that is not too different from Pythons <em>list comprehensions</em>. In fact, the <code>foreach</code> package also includes a function called <code>when</code> which can prevent some of the evaluations from happening, very much like the “if” clause in Pythons list comprehensions. For example, you could filter out negative values of an iterator using <code>when</code> as follows:</p>
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">a=</span><span class="fu">irnorm</span>(<span class="dv">1</span>, <span class="at">count=</span><span class="dv">10</span>), <span class="at">.combine=</span><span class="st">&#39;c&#39;</span>) <span class="sc">%:%</span> <span class="fu">when</span>(a <span class="sc">&gt;=</span> <span class="dv">0</span>) <span class="sc">%do%</span> <span class="fu">sqrt</span>(a)</span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a>x</span></code></pre></div>
<pre><code>## [1] 0.4055020 1.0835713 0.8704032 0.3653185 1.4166866 0.8115083</code></pre>
<p>I wont say much on this topic, but I cant help showing how <code>foreach</code> with <code>when</code> can be used to write a simple quick sort function, in the classic Haskell fashion:</p>
<div class="sourceCode" id="cb54"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>qsort <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
<span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a> n <span class="ot">&lt;-</span> <span class="fu">length</span>(x)</span>
<span id="cb54-3"><a href="#cb54-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (n <span class="sc">==</span> <span class="dv">0</span>) {</span>
<span id="cb54-4"><a href="#cb54-4" aria-hidden="true" tabindex="-1"></a> x</span>
<span id="cb54-5"><a href="#cb54-5" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb54-6"><a href="#cb54-6" aria-hidden="true" tabindex="-1"></a> p <span class="ot">&lt;-</span> <span class="fu">sample</span>(n, <span class="dv">1</span>)</span>
<span id="cb54-7"><a href="#cb54-7" aria-hidden="true" tabindex="-1"></a> smaller <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">y=</span>x[<span class="sc">-</span>p], <span class="at">.combine=</span>c) <span class="sc">%:%</span> <span class="fu">when</span>(y <span class="sc">&lt;=</span> x[p]) <span class="sc">%do%</span> y</span>
<span id="cb54-8"><a href="#cb54-8" aria-hidden="true" tabindex="-1"></a> larger <span class="ot">&lt;-</span> <span class="fu">foreach</span>(<span class="at">y=</span>x[<span class="sc">-</span>p], <span class="at">.combine=</span>c) <span class="sc">%:%</span> <span class="fu">when</span>(y <span class="sc">&gt;</span> x[p]) <span class="sc">%do%</span> y</span>
<span id="cb54-9"><a href="#cb54-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">c</span>(<span class="fu">qsort</span>(smaller), x[p], <span class="fu">qsort</span>(larger))</span>
<span id="cb54-10"><a href="#cb54-10" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb54-11"><a href="#cb54-11" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb54-12"><a href="#cb54-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb54-13"><a href="#cb54-13" aria-hidden="true" tabindex="-1"></a><span class="fu">qsort</span>(<span class="fu">runif</span>(<span class="dv">12</span>))</span></code></pre></div>
<pre><code>## [1] 0.05671936 0.05986948 0.19082846 0.22652967 0.54588779 0.62601549
## [7] 0.66316703 0.68171436 0.74671367 0.80146286 0.80993460 0.82453758</code></pre>
<p>Not that I recommend this over the standard R <code>sort</code> function. But its a pretty interesting example use of <code>foreach</code>.</p>
</div>
<div id="conclusion" class="section level2">
<h2>Conclusion</h2>
<p>Much of parallel computing comes to doing three things: splitting the problem into pieces, executing the pieces in parallel, and combining the results back together. Using the <code>foreach</code> package, the iterators help you to split the problem into pieces, the <code>%dopar%</code> function executes the pieces in parallel, and the specified <code>.combine</code> function puts the results back together. Weve demonstrated how simple things can be done in parallel quite easily using the <code>foreach</code> package, and given some ideas about how more complex problems can be solved. But its a fairly new package, and we will continue to work on ways of making it a more powerful system for doing parallel computing.</p>
</div>
<!-- code folding -->
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>