<commit_before><!doctype html>
<html lang="en">

    <head>
        <meta charset="utf-8">

        <title>Quirks of R</title>

        <meta name="description" content="Quirks of R: Non-standard evaluation (NSE) and vector indexing.">
        <meta name="author" content="<NAME>">

        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">

        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

        <!-- Font Awesome -->
        <script src="https://use.fontawesome.com/3e5bdbb0cf.js"></script>

        <link rel="stylesheet" href="../../revealjs/css/reveal.css">
        <link rel="stylesheet" href="../../revealjs/css/theme/white.css" id="theme">

        <!-- Theme used for syntax highlighting of code -->
        <link rel="stylesheet" href="../../revealjs/lib/css/trio.css">

        <!-- Custom CSS -->
        <link rel="stylesheet" href="custom.css">


        <!-- Printing and PDF exports -->
        <script>
            var link = document.createElement( 'link' );
            link.rel = 'stylesheet';
            link.type = 'text/css';
            link.href = window.location.search.match( /print-pdf/gi ) ? '../../revealjs/css/print/pdf.css' : '../../revealjs/css/print/paper.css';
            document.getElementsByTagName( 'head' )[0].appendChild( link );
        </script>

        <!--[if lt IE 9]>
        <script src="../../revealjs/lib/js/html5shiv.js"></script>
        <![endif]-->
    </head>

    <body>

        <div class="reveal">

            <!-- Any section element inside of this container is displayed as a slide -->
            <div class="slides">
                <!-- Front Slide -->
                <section>
                    <h1>Quirks of R</h1>
                    <h3>Bay Area useR Group Meetup</h3>
                    <h6>September 13, 2016</h6>
                    <small>
                    <a href="http://www.perfectlyrandom.org"><NAME></a> |
                        <a href="http://twitter.com/ankurio"><i class="fa fa-twitter" aria-hidden="true"></i> @ankurio</a>
                    </small>
                </section>

                <section id="fragments">
                        <h2>Which package loads?</h2>
                        <pre><code class="lang-r hljs" data-trim contenteditable>
                        > library(ggplot2)
                        </code></pre>
                        <pre><code class="lang-r hljs fragment" data-trim contenteditable>
                        > # ggplot2 loads
                        </code></pre>
                </section>

                <section id="fragments">
                        <h2>Which package loads?</h2>
                        <pre><code class="lang-r hljs" data-trim contenteditable>
                        > library("ggplot2")
                        </code></pre>
                        <pre><code class="lang-r hljs fragment" data-trim contenteditable>
                        > # ggplot2 loads
                        </code></pre>
                </section>

                <section id="fragments">
                        <h2>Which package loads?</h2>
                        <pre><code class="lang-r hljs" data-trim contenteditable>
dplyr <- "ggplot2"
> library(dplyr)
                        </code></pre>
                        <pre><code class="lang-r hljs fragment" data-trim contenteditable>

Attaching package: ‘dplyr’

The following object is masked from ‘package:stats’:

    filter

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union
> # dplyr loads!
                        </code></pre>
                <p><span class="fragment"> R parses unquoted text. </span>
                <br/>
                <span class="fragment"> This is
                <span class="highlight-red"> non-standard evaluation</span>
                or
                <span class="highlight-red"> NSE </span>. </span></p>
                </section>


                <section>
                <h3>Non-standard evaluation (NSE) happens everywhere</h3>
<table class="tg">
  <tr>
    <td class="tg-031e">base</td>
    <td class="tg-031e">rm, ls, subset</td>
  </tr>
  <tr>
    <td class="tg-031e">utils</td>
    <td class="tg-031e">demo, example</td>
  </tr>
  <tr>
    <td class="tg-031e">graphics</td>
    <td class="tg-031e">plot</td>
  </tr>
  <tr>
    <td class="tg-yw4l">ggplot2</td>
    <td class="tg-yw4l">aes</td>
  </tr>
  <tr>
    <td class="tg-yw4l">plyr</td>
    <td class="tg-yw4l">summarize</td>
  </tr>
  <tr>
    <td class="tg-yw4l">dplyr</td>
    <td class="tg-yw4l">filter, select, arrange, summarize</td>
  </tr>
</table>
                <p> We will focus on <code class="lang-r hljs" style="display:inline"> subset()</code>.</p>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        ### `subset()` uses NSE

                        ```r
                        > pop.df
  state year pop
1    CA 2005  37
2    WI 2005   6
3    CA 2015  39
4    WI 2015   6
```

```r
> subset(pop.df, year == 2015)
  state year pop
3    CA 2015  39
4    WI 2015   6
```

`year` is the column name (in dataframe scope)
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        ### `subset()` uses NSE with mixed scoping

                        ```r
                        > pop.df
  state year pop
1    CA 2005  37
2    WI 2005   6
3    CA 2015  39
4    WI 2015   6
```

```r
> x <- 2015
> subset(pop.df, year == x)
  state year pop
3    CA 2015  39
4    WI 2015   6
```
`year` is the column name (in dataframe scope)
<br/>
`x` is the global variable (global scope)

                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        ### `subset()` can't differentiate scopes

                        ```r
                        > pop.df
  state year pop
1    CA 2005  37
2    WI 2005   6
3    CA 2015  39
4    WI 2015   6
```

```r
results.by.year.1 <- function(df, year) {
    df.subset <- subset(df, year == year)
    # Do some computation
    return_value <- nrow(df.subset)
    return(return_value)
}
```

```r
> results.by.year.1(pop.df, 2015)
[1] 4
> # Incorrect result. Silent mistake! Correct result is 2.
```
<p>Using `year` as an argument name is a bad idea. <br/> Let's change it.</p>
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        ### Apparent fix

                        ```r
                        > pop.df
  state year pop
1    CA 2005  37
2    WI 2005   6
3    CA 2015  39
4    WI 2015   6
```

```r
results.by.year.2 <- function(df, yr) {
    df.subset <- subset(df, year == yr)
    # Do some computation
    return_value <- nrow(df.subset)
    return(return_value)
}
```

```r
> results.by.year.2(pop.df, 2015)
[1] 2
> # Correct result
```
<p>This doesn't solve the problem. <br/> It only hides the problem.</p>
                    </script>
                </section>



                <section data-markdown>
                    <script type="text/template">
                        ### Hidden problem with `subset()`

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
results.by.year.2 <- function(df, yr) {
    df.subset <- subset(df, year == yr)
    # Do some computation
    return_value <- nrow(df.subset)
    return(return_value)
}
```

```r
> results.by.year.2(pop.df, 2015)
[1] 1
> # Incorrect result. Silent mistake! Correct result is 2.
```
<p>How do we choose the second argument name for `results.by.year.x()` ?</p>
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        ### Dirty Fix

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
results.by.year.3 <- function(df, yr) {
    testthat::expect_false("yr" %in% names(df))
    df.subset <- subset(df, year == yr)
    # Do some computation
    return_value <- nrow(df.subset)
    return(return_value)
}
```

```r
> results.by.year.3(pop.df, 2015)
Error: "yr" %in% names(df) is not false
> # Throws error. At least it is not a silent mistake.
```
<p>This will stop code execution but it won't make a silent mistake.</p>
                    </script>
                </section>

                <section data-markdown>
                    <script type="text/template">
                        ### Better Fix

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
results.by.year.4 <- function(df, yr) {
    df.subset <- df[df[["year"]] == yr, , drop = FALSE]
    # Do some computation
    return_value <- nrow(df.subset)
    return(return_value)
}
```

```r
> results.by.year.4(pop.df, 2015)
[1] 2
> # Correct result
```
<p>Don't use `subset()`. Use logical indexing.
<br/><small>(Remember to use `drop = FALSE`)</small></p>
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        #### What about `filter` functions in `dplyr` ?

                        `dplyr` contains two functions that do the job of `subset()`:
                        <br/>

                        - `filter()` - uses <span style="color: blue">NSE</span>
                        - `filter_()` - uses <span style="color: red">SE</span>

                        <br/>
                        Do these solve the problem?

                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        #### `filter()` with Single Scope

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
> # NSE function, single scope
> dplyr::filter(pop.df, year == 2015)
  state year pop   yr rate
1    CA 2015  39 3000    6
2    WI 2015   6 5000   10
> # Correct result

```
                    </script>
                </section>

                <section data-markdown>
                    <script type="text/template">
                        #### `filter()` with Mixed Scope

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
> # NSE function, mixed scope
> year <- 2015
> dplyr::filter(pop.df, year == year)
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
> # Incorrect result

```
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        #### `filter()_` with Single Scope

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
> # SE function, single scope
> dplyr::filter_(pop.df, "year == 2015")
  state year pop   yr rate
1    CA 2015  39 3000    6
2    WI 2015   6 5000   10
> # Correct result

```
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        #### `filter()_` with Mixed Scope

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
> # SE function, mixed scope
> year <- 2015
> dplyr::filter_(pop.df, "year == year")
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
> # Incorrect result

```
<p>Using a SE function by itself does not solve the problem of mixed scope.</p>

                    </script>
                </section><commit_msg>Fixing typo<commit_after><!doctype html>
<html lang="en">

    <head>
        <meta charset="utf-8">

        <title>Quirks of R</title>

        <meta name="description" content="Quirks of R: Non-standard evaluation (NSE) and vector indexing.">
        <meta name="author" content="<NAME>">

        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">

        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

        <!-- Font Awesome -->
        <script src="https://use.fontawesome.com/3e5bdbb0cf.js"></script>

        <link rel="stylesheet" href="../../revealjs/css/reveal.css">
        <link rel="stylesheet" href="../../revealjs/css/theme/white.css" id="theme">

        <!-- Theme used for syntax highlighting of code -->
        <link rel="stylesheet" href="../../revealjs/lib/css/trio.css">

        <!-- Custom CSS -->
        <link rel="stylesheet" href="custom.css">


        <!-- Printing and PDF exports -->
        <script>
            var link = document.createElement( 'link' );
            link.rel = 'stylesheet';
            link.type = 'text/css';
            link.href = window.location.search.match( /print-pdf/gi ) ? '../../revealjs/css/print/pdf.css' : '../../revealjs/css/print/paper.css';
            document.getElementsByTagName( 'head' )[0].appendChild( link );
        </script>

        <!--[if lt IE 9]>
        <script src="../../revealjs/lib/js/html5shiv.js"></script>
        <![endif]-->
    </head>

    <body>

        <div class="reveal">

            <!-- Any section element inside of this container is displayed as a slide -->
            <div class="slides">
                <!-- Front Slide -->
                <section>
                    <h1>Quirks of R</h1>
                    <h3>Bay Area useR Group Meetup</h3>
                    <h6>September 13, 2016</h6>
                    <small>
                    <a href="http://www.perfectlyrandom.org"><NAME></a> |
                        <a href="http://twitter.com/ankurio"><i class="fa fa-twitter" aria-hidden="true"></i> @ankurio</a>
                    </small>
                </section>

                <section id="fragments">
                        <h2>Which package loads?</h2>
                        <pre><code class="lang-r hljs" data-trim contenteditable>
                        > library(ggplot2)
                        </code></pre>
                        <pre><code class="lang-r hljs fragment" data-trim contenteditable>
                        > # ggplot2 loads
                        </code></pre>
                </section>

                <section id="fragments">
                        <h2>Which package loads?</h2>
                        <pre><code class="lang-r hljs" data-trim contenteditable>
                        > library("ggplot2")
                        </code></pre>
                        <pre><code class="lang-r hljs fragment" data-trim contenteditable>
                        > # ggplot2 loads
                        </code></pre>
                </section>

                <section id="fragments">
                        <h2>Which package loads?</h2>
                        <pre><code class="lang-r hljs" data-trim contenteditable>
dplyr <- "ggplot2"
> library(dplyr)
                        </code></pre>
                        <pre><code class="lang-r hljs fragment" data-trim contenteditable>

Attaching package: ‘dplyr’

The following object is masked from ‘package:stats’:

    filter

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union
> # dplyr loads!
                        </code></pre>
                <p><span class="fragment"> R parses unquoted text. </span>
                <br/>
                <span class="fragment"> This is
                <span class="highlight-red"> non-standard evaluation</span>
                or
                <span class="highlight-red"> NSE </span>. </span></p>
                </section>


                <section>
                <h3>Non-standard evaluation (NSE) happens everywhere</h3>
<table class="tg">
  <tr>
    <td class="tg-031e">base</td>
    <td class="tg-031e">rm, ls, subset</td>
  </tr>
  <tr>
    <td class="tg-031e">utils</td>
    <td class="tg-031e">demo, example</td>
  </tr>
  <tr>
    <td class="tg-031e">graphics</td>
    <td class="tg-031e">plot</td>
  </tr>
  <tr>
    <td class="tg-yw4l">ggplot2</td>
    <td class="tg-yw4l">aes</td>
  </tr>
  <tr>
    <td class="tg-yw4l">plyr</td>
    <td class="tg-yw4l">summarize</td>
  </tr>
  <tr>
    <td class="tg-yw4l">dplyr</td>
    <td class="tg-yw4l">filter, select, arrange, summarize</td>
  </tr>
</table>
                <p> We will focus on <code class="lang-r hljs" style="display:inline"> subset()</code>.</p>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        ### `subset()` uses NSE

                        ```r
                        > pop.df
  state year pop
1    CA 2005  37
2    WI 2005   6
3    CA 2015  39
4    WI 2015   6
```

```r
> subset(pop.df, year == 2015)
  state year pop
3    CA 2015  39
4    WI 2015   6
```

`year` is the column name (in dataframe scope)
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        ### `subset()` uses NSE with mixed scoping

                        ```r
                        > pop.df
  state year pop
1    CA 2005  37
2    WI 2005   6
3    CA 2015  39
4    WI 2015   6
```

```r
> x <- 2015
> subset(pop.df, year == x)
  state year pop
3    CA 2015  39
4    WI 2015   6
```
`year` is the column name (in dataframe scope)
<br/>
`x` is the global variable (global scope)

                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        ### `subset()` can't differentiate scopes

                        ```r
                        > pop.df
  state year pop
1    CA 2005  37
2    WI 2005   6
3    CA 2015  39
4    WI 2015   6
```

```r
results.by.year.1 <- function(df, year) {
    df.subset <- subset(df, year == year)
    # Do some computation
    return_value <- nrow(df.subset)
    return(return_value)
}
```

```r
> results.by.year.1(pop.df, 2015)
[1] 4
> # Incorrect result. Silent mistake! Correct result is 2.
```
<p>Using `year` as an argument name is a bad idea. <br/> Let's change it.</p>
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        ### Apparent fix

                        ```r
                        > pop.df
  state year pop
1    CA 2005  37
2    WI 2005   6
3    CA 2015  39
4    WI 2015   6
```

```r
results.by.year.2 <- function(df, yr) {
    df.subset <- subset(df, year == yr)
    # Do some computation
    return_value <- nrow(df.subset)
    return(return_value)
}
```

```r
> results.by.year.2(pop.df, 2015)
[1] 2
> # Correct result
```
<p>This doesn't solve the problem. <br/> It only hides the problem.</p>
                    </script>
                </section>



                <section data-markdown>
                    <script type="text/template">
                        ### Hidden problem with `subset()`

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
results.by.year.2 <- function(df, yr) {
    df.subset <- subset(df, year == yr)
    # Do some computation
    return_value <- nrow(df.subset)
    return(return_value)
}
```

```r
> results.by.year.2(pop.df, 2015)
[1] 1
> # Incorrect result. Silent mistake! Correct result is 2.
```
<p>How do we choose the second argument name for `results.by.year.x()` ?</p>
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        ### Dirty Fix

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
results.by.year.3 <- function(df, yr) {
    testthat::expect_false("yr" %in% names(df))
    df.subset <- subset(df, year == yr)
    # Do some computation
    return_value <- nrow(df.subset)
    return(return_value)
}
```

```r
> results.by.year.3(pop.df, 2015)
Error: "yr" %in% names(df) is not false
> # Throws error. At least it is not a silent mistake.
```
<p>This will stop code execution but it won't make a silent mistake.</p>
                    </script>
                </section>

                <section data-markdown>
                    <script type="text/template">
                        ### Better Fix

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
results.by.year.4 <- function(df, yr) {
    df.subset <- df[df[["year"]] == yr, , drop = FALSE]
    # Do some computation
    return_value <- nrow(df.subset)
    return(return_value)
}
```

```r
> results.by.year.4(pop.df, 2015)
[1] 2
> # Correct result
```
<p>Don't use `subset()`. Use logical indexing.
<br/><small>(Remember to use `drop = FALSE`)</small></p>
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        #### What about `filter` functions in `dplyr` ?

                        `dplyr` contains two functions that do the job of `subset()`:
                        <br/>

                        - `filter()` - uses <span style="color: blue">NSE</span>
                        - `filter_()` - uses <span style="color: red">SE</span>

                        <br/>
                        Do these solve the problem?

                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        #### `filter()` with Single Scope

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
> # NSE function, single scope
> dplyr::filter(pop.df, year == 2015)
  state year pop   yr rate
1    CA 2015  39 3000    6
2    WI 2015   6 5000   10
> # Correct result

```
                    </script>
                </section>

                <section data-markdown>
                    <script type="text/template">
                        #### `filter()` with Mixed Scope

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
> # NSE function, mixed scope
> year <- 2015
> dplyr::filter(pop.df, year == year)
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
> # Incorrect result

```
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        #### `filter_()` with Single Scope

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
> # SE function, single scope
> dplyr::filter_(pop.df, "year == 2015")
  state year pop   yr rate
1    CA 2015  39 3000    6
2    WI 2015   6 5000   10
> # Correct result

```
                    </script>
                </section>


                <section data-markdown>
                    <script type="text/template">
                        #### `filter_()` with Mixed Scope

                        ```r
                        > pop.df
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
```

```r
> # SE function, mixed scope
> year <- 2015
> dplyr::filter_(pop.df, "year == year")
  state year pop   yr  rate
1    CA 2005  37 2005  4.01
2    WI 2005   6 1000  2.00
3    CA 2015  39 3000  6.00
4    WI 2015   6 5000 10.00
> # Incorrect result

```
<p>Using a SE function by itself does not solve the problem of mixed scope.</p>

                    </script>
                </section>
{'fraction_non_alphanumeric': 0.13295199814485584, 'fraction_numerical': 0.06168354332534591, 'mean_word_length': 1.4051868377021752, 'pattern_counts': {'":': 0, '<': 473, '<?xml version=': 0, '>': 523, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4804606', 'n_tokens_mistral': 9453, 'n_tokens_neox': 7670, 'n_words': 2306}