Skip to content

Commit

Permalink
Change Tips to callouts in 03-loops-R
Browse files Browse the repository at this point in the history
  • Loading branch information
ateucher committed Jan 27, 2015
1 parent 66762fa commit ec68547
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
31 changes: 22 additions & 9 deletions 03-loops-R.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ best_practice[-6]
print_words(best_practice[-6])
```

> **Tip:** R has has a special variable, `NA`, for designating missing values that are **N**ot **A**vailable in a data set.
> See `?NA` and [An Introduction to R][na] for more details.
> ## Tip {.callout}
>
> R has has a special variable, `NA`, for designating missing values that are
> **N**ot **A**vailable in a data set. See `?NA` and [An Introduction to R][na]
> for more details.
[na]: http://cran.r-project.org/doc/manuals/r-release/R-intro.html#Missing-values

Expand Down Expand Up @@ -244,9 +247,14 @@ list.files(path = "data", pattern = "inflammation")
```


> **Tip:**
For larger projects, it is recommended to organize separate parts of the analysis into multiple subdirectories, e.g. one subdirectory for the raw data, one for the code, and one for the results like figures. We have done that here to some extent, putting all of our data files into the subdirectory "data".
For more advice on this topic, you can read [A quick guide to organizing computational biology projects][Noble2009] by William Stafford Noble.
> ## Tip {.callout}
>
> For larger projects, it is recommended to organize separate parts of the
> analysis into multiple subdirectories, e.g. one subdirectory for the raw data,
> one for the code, and one for the results like figures. We have done that here
> to some extent, putting all of our data files into the subdirectory "data".
> For more advice on this topic, you can read [A quick guide to organizing
> computational biology projects][Noble2009] by William Stafford Noble.
[Noble2009]: http://www.ploscompbiol.org/article/info%3Adoi%2F10.1371%2Fjournal.pcbi.1000424

Expand Down Expand Up @@ -278,10 +286,15 @@ for (f in filenames) {

Sure enough, the maxima of these data sets show exactly the same ramp as the first, and their minima show the same staircase structure.

> **Tip:** In this lesson we saw how to use a simple `for` loop to repeat an operation.
As you progress with R, you will learn that there are multiple ways to accomplish this.
Sometimes the choice of one method over another is more a matter of personal style, but other times it can have consequences for the speed of your code.
For instruction on best practices, see this supplementary [lesson](03-supp-loops-in-depth.html) that demonstrates how to properly repeat operations in R.
> ## Tip {.callout}
>
> In this lesson we saw how to use a simple `for` loop to repeat an operation.
> As you progress with R, you will learn that there are multiple ways to
> accomplish this. Sometimes the choice of one method over another is more a
> matter of personal style, but other times it can have consequences for the
> speed of your code. For instruction on best practices, see this supplementary
> [lesson](03-supp-loops-in-depth.html) that demonstrates how to properly repeat
> operations in R.
> ## Challenges {.challenge}
>
Expand Down
21 changes: 12 additions & 9 deletions 03-loops-R.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ <h3 id="for-loops">For Loops</h3>
[1] &quot;the&quot;
[1] NA
</code></pre>
<blockquote>
<p><strong>Tip:</strong> R has has a special variable, <code>NA</code>, for designating missing values that are <strong>N</strong>ot <strong>A</strong>vailable in a data set. See <code>?NA</code> and <a href="http://cran.r-project.org/doc/manuals/r-release/R-intro.html#Missing-values">An Introduction to R</a> for more details.</p>
</blockquote>
<div id="tip" class="callout">
<h2>Tip</h2>
<p>R has has a special variable, <code>NA</code>, for designating missing values that are <strong>N</strong>ot <strong>A</strong>vailable in a data set. See <code>?NA</code> and <a href="http://cran.r-project.org/doc/manuals/r-release/R-intro.html#Missing-values">An Introduction to R</a> for more details.</p>
</div>
<p>Here's a better approach:</p>
<pre class="sourceCode r"><code class="sourceCode r">print_words &lt;-<span class="st"> </span>function(sentence) {
for (word in sentence) {
Expand Down Expand Up @@ -215,9 +216,10 @@ <h3 id="processing-multiple-files">Processing Multiple Files</h3>
[7] &quot;inflammation-07.csv&quot; &quot;inflammation-08.csv&quot; &quot;inflammation-09.csv&quot;
[10] &quot;inflammation-10.csv&quot; &quot;inflammation-11.csv&quot; &quot;inflammation-12.csv&quot;
</code></pre>
<blockquote>
<p><strong>Tip:</strong> For larger projects, it is recommended to organize separate parts of the analysis into multiple subdirectories, e.g. one subdirectory for the raw data, one for the code, and one for the results like figures. We have done that here to some extent, putting all of our data files into the subdirectory &quot;data&quot;. For more advice on this topic, you can read <a href="http://www.ploscompbiol.org/article/info%3Adoi%2F10.1371%2Fjournal.pcbi.1000424">A quick guide to organizing computational biology projects</a> by William Stafford Noble.</p>
</blockquote>
<div id="tip-1" class="callout">
<h2>Tip</h2>
<p>For larger projects, it is recommended to organize separate parts of the analysis into multiple subdirectories, e.g. one subdirectory for the raw data, one for the code, and one for the results like figures. We have done that here to some extent, putting all of our data files into the subdirectory &quot;data&quot;. For more advice on this topic, you can read <a href="http://www.ploscompbiol.org/article/info%3Adoi%2F10.1371%2Fjournal.pcbi.1000424">A quick guide to organizing computational biology projects</a> by William Stafford Noble.</p>
</div>
<p>As these examples show, <code>list.files</code> result is a vector of strings, which means we can loop over it to do something with each filename in turn. In our case, the &quot;something&quot; we want is our <code>analyze</code> function.</p>
<p>Because we have put our data in separate subdirectory, if we want to access these files using the output of <code>list.files</code> we also need to include the &quot;path&quot; portion of the file name. We can do that by using the argument <code>full.names = TRUE</code>.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">list.files</span>(<span class="dt">path =</span> <span class="st">&quot;data&quot;</span>, <span class="dt">pattern =</span> <span class="st">&quot;csv&quot;</span>, <span class="dt">full.names =</span> <span class="ot">TRUE</span>)</code></pre>
Expand Down Expand Up @@ -256,9 +258,10 @@ <h3 id="processing-multiple-files">Processing Multiple Files</h3>
</code></pre>
<p><img src="fig/03-loops-R-loop-analyze-7.png" title="plot of chunk loop-analyze" alt="plot of chunk loop-analyze" style="display: block; margin: auto;" /><img src="fig/03-loops-R-loop-analyze-8.png" title="plot of chunk loop-analyze" alt="plot of chunk loop-analyze" style="display: block; margin: auto;" /><img src="fig/03-loops-R-loop-analyze-9.png" title="plot of chunk loop-analyze" alt="plot of chunk loop-analyze" style="display: block; margin: auto;" /></p>
<p>Sure enough, the maxima of these data sets show exactly the same ramp as the first, and their minima show the same staircase structure.</p>
<blockquote>
<p><strong>Tip:</strong> In this lesson we saw how to use a simple <code>for</code> loop to repeat an operation. As you progress with R, you will learn that there are multiple ways to accomplish this. Sometimes the choice of one method over another is more a matter of personal style, but other times it can have consequences for the speed of your code. For instruction on best practices, see this supplementary <a href="03-supp-loops-in-depth.html">lesson</a> that demonstrates how to properly repeat operations in R.</p>
</blockquote>
<div id="tip-2" class="callout">
<h2>Tip</h2>
<p>In this lesson we saw how to use a simple <code>for</code> loop to repeat an operation. As you progress with R, you will learn that there are multiple ways to accomplish this. Sometimes the choice of one method over another is more a matter of personal style, but other times it can have consequences for the speed of your code. For instruction on best practices, see this supplementary <a href="03-supp-loops-in-depth.html">lesson</a> that demonstrates how to properly repeat operations in R.</p>
</div>
<div id="challenges-1" class="challenge">
<h2>Challenges</h2>
<ol style="list-style-type: decimal">
Expand Down
31 changes: 22 additions & 9 deletions 03-loops-R.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ print_words(best_practice[-6])
~~~

> **Tip:** R has has a special variable, `NA`, for designating missing values that are **N**ot **A**vailable in a data set.
> See `?NA` and [An Introduction to R][na] for more details.
> ## Tip {.callout}
>
> R has has a special variable, `NA`, for designating missing values that are
> **N**ot **A**vailable in a data set. See `?NA` and [An Introduction to R][na]
> for more details.
[na]: http://cran.r-project.org/doc/manuals/r-release/R-intro.html#Missing-values

Expand Down Expand Up @@ -398,9 +401,14 @@ list.files(path = "data", pattern = "inflammation")
~~~


> **Tip:**
For larger projects, it is recommended to organize separate parts of the analysis into multiple subdirectories, e.g. one subdirectory for the raw data, one for the code, and one for the results like figures. We have done that here to some extent, putting all of our data files into the subdirectory "data".
For more advice on this topic, you can read [A quick guide to organizing computational biology projects][Noble2009] by William Stafford Noble.
> ## Tip {.callout}
>
> For larger projects, it is recommended to organize separate parts of the
> analysis into multiple subdirectories, e.g. one subdirectory for the raw data,
> one for the code, and one for the results like figures. We have done that here
> to some extent, putting all of our data files into the subdirectory "data".
> For more advice on this topic, you can read [A quick guide to organizing
> computational biology projects][Noble2009] by William Stafford Noble.
[Noble2009]: http://www.ploscompbiol.org/article/info%3Adoi%2F10.1371%2Fjournal.pcbi.1000424

Expand Down Expand Up @@ -489,10 +497,15 @@ for (f in filenames) {

Sure enough, the maxima of these data sets show exactly the same ramp as the first, and their minima show the same staircase structure.

> **Tip:** In this lesson we saw how to use a simple `for` loop to repeat an operation.
As you progress with R, you will learn that there are multiple ways to accomplish this.
Sometimes the choice of one method over another is more a matter of personal style, but other times it can have consequences for the speed of your code.
For instruction on best practices, see this supplementary [lesson](03-supp-loops-in-depth.html) that demonstrates how to properly repeat operations in R.
> ## Tip {.callout}
>
> In this lesson we saw how to use a simple `for` loop to repeat an operation.
> As you progress with R, you will learn that there are multiple ways to
> accomplish this. Sometimes the choice of one method over another is more a
> matter of personal style, but other times it can have consequences for the
> speed of your code. For instruction on best practices, see this supplementary
> [lesson](03-supp-loops-in-depth.html) that demonstrates how to properly repeat
> operations in R.
> ## Challenges {.challenge}
>
Expand Down

0 comments on commit ec68547

Please sign in to comment.