Skip to content

Commit

Permalink
load sources & defensive style
Browse files Browse the repository at this point in the history
ref #6
  • Loading branch information
wibeasley committed Oct 8, 2019
1 parent cb52252 commit 058b795
Show file tree
Hide file tree
Showing 29 changed files with 175 additions and 102 deletions.
7 changes: 7 additions & 0 deletions ch-architecture.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ Consistency
1. Across Files {#consistency-files}
1. Across Languages
1. Across Projects

Defensive Style
------------------------------------

Some of these may not be not be an 'architecture', but it guides many of our architectural principles.

1. **Qualify functions**.{#qualify-functions} Try to prepend each function with its package. Write `dplyr::filter()` instead of `filter()`. When two packages contain public functions with the same name, the package that was most recently called with `library()` takes precedent. When multiple R files are executed, the packages' precedents may not be predictable. Specifying the package eliminates the ambiguity, while also making the code easier to follow. For this reason, we recommend that almost all R files contain a ['load-packages'](#load-packages) chunk.
32 changes: 20 additions & 12 deletions ch-file-prototype.Rmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Prototypical File {#file-prototype}
====================================

As stated before, in [Consistency Files](#consistency-files), using a consistent file structure can (a) improve the quality of the code because the structure has been proven over time to facilitate good practices and (b) allow your intentions to be more clear to teammates because they are familiar with the order and intentions of the chunks.
As stated before, in [Consistency across Files](#consistency-files), using a consistent file structure can (a) improve the quality of the code because the structure has been proven over time to facilitate good practices and (b) allow your intentions to be more clear to teammates because they are familiar with the order and intentions of the chunks.

We use the term "chunk" for a section of code because it corresponds with knitr terminology [@xie2015], and in many analysis files (as opposed to manipulation files), the chunk of our R file connects to a knitr Rmd file.

Expand Down Expand Up @@ -29,28 +29,36 @@ Keep the chunk, even if no files are sourced. An empty chunk is instructive to
base::source(file="./analysis/common/display-1.R") # Load common graphing functions.
```

Load Packages
Load Packages {#load-packages}
------------------------------------

qqq Discuss why `requireNamespace()` is preferable to `library()` for most packages. qqq
The 'load-packages' chunk declares required packages, and this is near the file's beginning for three reasons. First, a reader scanning the file can more easily determine its dependencies when located in a single chunk. Second, if your machine is lacking a required package, it is best to know early^[The error message "Error in library(foo) : there is no package called 'foo'" is easier to understand than "Error in bar() : could not find function 'bar'"; this also illuminate conflicts arising when two packages have a `bar()` function. See McConnell 2004 Section qqq for more about the 'fail early' principle.]. Third, this style mimics a requirement of other languages, such as declaring headers at the top of a C++ file.

The `source`d files above may load their own packages (by calling `library()`). It is important that `library()` calls in this file follow the 'load-sources' chunk so that identically-named functions (in different packages) are called with the correct precedent. Otherwise identically-named functions will conflict in the namespace with hard-to-predict results.
As discussed in the previous [qualify all functions](#qualify-functions) section, we recommend that functions are qualified with their package (*e.g.*, `foo::bar()` instead of merely `bar()`). Consequently, the 'load-packages' chunk calls `requireNamespace()` more frequently than `library()`. `requireNamespace()` verifies the package is available on the local machine, but does not load it into memory; `library()` verifies the package is available, and then loads it.

Attach these package(s) so their functions don't need to be qualified: http://r-pkgs.had.co.nz/namespace.html#search-path
`requireNamespace()` is not used in several scenarios.

Verify these packages are available on the machine, but their functions need to be qualified: http://r-pkgs.had.co.nz/namespace.html#search-path
1. Core packages (*e.g.*, 'base' and 'stats') are loaded by R in most default installations. We avoid unnecessary calls like `library(stats)` because they distract from more important features.
1. Obvious dependencies are not called by `reqiureNamespace()` or `library()` for similar reasons, especially if they are not called directly. For example 'tidyselect' is not listed when 'tidyr' is listed.
1. The "pipe" function (declared in the `magrittr' package , *i.e.*, `%>%`) is attached with `import::from(magrittr, "%>%")`. This frequently-used function is available without qualification.
1. Our analysis files tend to use many functions in a few concentrated packages (compared to manipulation files), so conflicting function names are less common. Typical packages used in analysis are 'ggplot2' and 'lme4'.


The `source`d files above may load their own packages (by calling `library()`). It is important that the `library()` calls in this file follow the 'load-sources' chunk so that identically-named functions (in different packages) are called with the correct precedent. Otherwise identically-named functions will conflict in the namespace with hard-to-predict results.

http://r-pkgs.had.co.nz/namespace.html#search-path about `library()`, `requireNamespace()`, and their siblings, as well as the larger concepts such as attaching functions into the search path.

```r
# ---- load-packages -----------------------------------------------------------
library(magrittr , quietly=TRUE)
import::from(magrittr, "%>%" )

requireNamespace("readr" )
requireNamespace("tidyr" )
requireNamespace("dplyr" ) # Avoid attaching dplyr, b/c its function names conflict many packages (esp base, stats, and plyr).
requireNamespace("rlang" ) # Language constucts, like quosures
requireNamespace("checkmate" ) # For asserting conditions meet expected requireNamespace("OuhscMunge" ) # remotes::install_github(repo="OuhscBbmc/OuhscMunge")
requireNamespace("readr" )
requireNamespace("tidyr" )
requireNamespace("dplyr" ) # Don't attach. Its function names conflict many packages (esp base, stats, and plyr)
requireNamespace("rlang" ) # Language constucts, like quosures
requireNamespace("checkmate" ) # Asserts expected conditions requireNamespace("OuhscMunge") # remotes::install_github(repo="OuhscBbmc/OuhscMunge")
```

Declare Globals
------------------------------------

Expand Down
10 changes: 9 additions & 1 deletion docs/architecture.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<meta name="author" content="Will Beasley" />


<meta name="date" content="2019-10-06" />
<meta name="date" content="2019-10-07" />

<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
Expand Down Expand Up @@ -137,6 +137,7 @@
<li class="chapter" data-level="2.2" data-path="architecture.html"><a href="architecture.html#leverage-team-members-strenghts-avoid-weaknesses"><i class="fa fa-check"></i><b>2.2</b> Leverage team member’s strenghts &amp; avoid weaknesses</a></li>
<li class="chapter" data-level="2.3" data-path="architecture.html"><a href="architecture.html#scales"><i class="fa fa-check"></i><b>2.3</b> Scales</a></li>
<li class="chapter" data-level="2.4" data-path="architecture.html"><a href="architecture.html#consistency"><i class="fa fa-check"></i><b>2.4</b> Consistency</a></li>
<li class="chapter" data-level="2.5" data-path="architecture.html"><a href="architecture.html#defensive-style"><i class="fa fa-check"></i><b>2.5</b> Defensive Style</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="file-prototype.html"><a href="file-prototype.html"><i class="fa fa-check"></i><b>3</b> Prototypical File</a><ul>
<li class="chapter" data-level="3.1" data-path="file-prototype.html"><a href="file-prototype.html#clear-memory"><i class="fa fa-check"></i><b>3.1</b> Clear Memory</a></li>
Expand Down Expand Up @@ -319,6 +320,13 @@ <h2><span class="header-section-number">2.4</span> Consistency</h2>
<li>Across Languages</li>
<li>Across Projects</li>
</ol>
</div>
<div id="defensive-style" class="section level2">
<h2><span class="header-section-number">2.5</span> Defensive Style</h2>
<p>Some of these may not be not be an ‘architecture’, but it guides many of our architectural principles.</p>
<ol style="list-style-type: decimal">
<li><strong>Qualify functions</strong>.{#qualify-functions} Try to prepend each function with its package. Write <code>dplyr::filter()</code> instead of <code>filter()</code>. When two packages contain public functions with the same name, the package that was most recently called with <code>library()</code> takes precedent. When multiple R files are executed, the packages’ precedents may not be predictable. Specifying the package eliminates the ambiguity, while also making the code easier to follow. For this reason, we recommend that almost all R files contain a <a href="file-prototype.html#load-packages">‘load-packages’</a> chunk.</li>
</ol>

</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion docs/automation.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<meta name="author" content="Will Beasley" />


<meta name="date" content="2019-10-06" />
<meta name="date" content="2019-10-07" />

<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
Expand Down Expand Up @@ -137,6 +137,7 @@
<li class="chapter" data-level="2.2" data-path="architecture.html"><a href="architecture.html#leverage-team-members-strenghts-avoid-weaknesses"><i class="fa fa-check"></i><b>2.2</b> Leverage team member’s strenghts &amp; avoid weaknesses</a></li>
<li class="chapter" data-level="2.3" data-path="architecture.html"><a href="architecture.html#scales"><i class="fa fa-check"></i><b>2.3</b> Scales</a></li>
<li class="chapter" data-level="2.4" data-path="architecture.html"><a href="architecture.html#consistency"><i class="fa fa-check"></i><b>2.4</b> Consistency</a></li>
<li class="chapter" data-level="2.5" data-path="architecture.html"><a href="architecture.html#defensive-style"><i class="fa fa-check"></i><b>2.5</b> Defensive Style</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="file-prototype.html"><a href="file-prototype.html"><i class="fa fa-check"></i><b>3</b> Prototypical File</a><ul>
<li class="chapter" data-level="3.1" data-path="file-prototype.html"><a href="file-prototype.html#clear-memory"><i class="fa fa-check"></i><b>3.1</b> Clear Memory</a></li>
Expand Down
3 changes: 2 additions & 1 deletion docs/collaboration.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<meta name="author" content="Will Beasley" />


<meta name="date" content="2019-10-06" />
<meta name="date" content="2019-10-07" />

<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
Expand Down Expand Up @@ -137,6 +137,7 @@
<li class="chapter" data-level="2.2" data-path="architecture.html"><a href="architecture.html#leverage-team-members-strenghts-avoid-weaknesses"><i class="fa fa-check"></i><b>2.2</b> Leverage team member’s strenghts &amp; avoid weaknesses</a></li>
<li class="chapter" data-level="2.3" data-path="architecture.html"><a href="architecture.html#scales"><i class="fa fa-check"></i><b>2.3</b> Scales</a></li>
<li class="chapter" data-level="2.4" data-path="architecture.html"><a href="architecture.html#consistency"><i class="fa fa-check"></i><b>2.4</b> Consistency</a></li>
<li class="chapter" data-level="2.5" data-path="architecture.html"><a href="architecture.html#defensive-style"><i class="fa fa-check"></i><b>2.5</b> Defensive Style</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="file-prototype.html"><a href="file-prototype.html"><i class="fa fa-check"></i><b>3</b> Prototypical File</a><ul>
<li class="chapter" data-level="3.1" data-path="file-prototype.html"><a href="file-prototype.html#clear-memory"><i class="fa fa-check"></i><b>3.1</b> Clear Memory</a></li>
Expand Down
3 changes: 2 additions & 1 deletion docs/data-at-rest.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<meta name="author" content="Will Beasley" />


<meta name="date" content="2019-10-06" />
<meta name="date" content="2019-10-07" />

<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
Expand Down Expand Up @@ -137,6 +137,7 @@
<li class="chapter" data-level="2.2" data-path="architecture.html"><a href="architecture.html#leverage-team-members-strenghts-avoid-weaknesses"><i class="fa fa-check"></i><b>2.2</b> Leverage team member’s strenghts &amp; avoid weaknesses</a></li>
<li class="chapter" data-level="2.3" data-path="architecture.html"><a href="architecture.html#scales"><i class="fa fa-check"></i><b>2.3</b> Scales</a></li>
<li class="chapter" data-level="2.4" data-path="architecture.html"><a href="architecture.html#consistency"><i class="fa fa-check"></i><b>2.4</b> Consistency</a></li>
<li class="chapter" data-level="2.5" data-path="architecture.html"><a href="architecture.html#defensive-style"><i class="fa fa-check"></i><b>2.5</b> Defensive Style</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="file-prototype.html"><a href="file-prototype.html"><i class="fa fa-check"></i><b>3</b> Prototypical File</a><ul>
<li class="chapter" data-level="3.1" data-path="file-prototype.html"><a href="file-prototype.html#clear-memory"><i class="fa fa-check"></i><b>3.1</b> Clear Memory</a></li>
Expand Down
Binary file modified docs/data-science-practices-1.epub
Binary file not shown.
Binary file modified docs/data-science-practices-1.pdf
Binary file not shown.
Loading

0 comments on commit 058b795

Please sign in to comment.