-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f8866f
commit 40b4da1
Showing
16 changed files
with
4,010 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ title: cheat | |
editor_options: | ||
chunk_output_type: console | ||
--- | ||
|
||
|
||
```{r} | ||
library(rlang) | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# not function | ||
|
||
|
||
Operator Precedence came up in today’s discussion of this code from | ||
Advanced R 18.4.1 <a | ||
href="https://adv-r.hadley.nz/expressions.html?q=%in%#operator-precedence" | ||
class="uri">https://adv-r.hadley.nz/expressions.html?q=%in%#operator-precedence</a> | ||
|
||
``` r | ||
knitr::opts_chunk$set(echo = TRUE, | ||
comment=" ##", | ||
collapse=TRUE) | ||
|
||
lobstr::ast(!x %in% y) | ||
``` | ||
|
||
█─`!` | ||
└─█─`%in%` | ||
├─x | ||
└─y | ||
|
||
The reason this works, if I followed the discussion correctly, is due to | ||
“operator precedence”, %in% is evaluated before ! | ||
|
||
SEE <https://rdrr.io/r/base/Syntax.html> | ||
|
||
I want to suggest another way to see this. | ||
|
||
`!` is a function and, of course, functions take arguments, in this case | ||
exactly one argument. **If you mentally parse functions as lists, the | ||
function is first, followed by arguments.** lobstr::ast shows this. | ||
|
||
If you remember `!` is a function, then it’s argument must be what | ||
remains: x %in% y. | ||
|
||
# Background !not function | ||
|
||
``` r | ||
library(lobstr) | ||
|
||
## usage examples | ||
x = TRUE | ||
!x | ||
## [1] FALSE | ||
! x # spacing not important | ||
## [1] FALSE | ||
|
||
`!` (x) | ||
## [1] FALSE | ||
`!`(x) | ||
## [1] FALSE | ||
|
||
## class | ||
`!` |> class() # function | ||
## [1] "function" | ||
|
||
## structure | ||
lobstr::ast(!x) | ||
## █─`!` | ||
## └─x | ||
|
||
## source code | ||
`!` | ||
## function (x) .Primitive("!") | ||
|
||
#error | ||
# `!` x | ||
|
||
``` | ||
|
||
``` r | ||
|
||
## first argument, as string | ||
## 2nd arugment as expression (otherwise, call will try to evaluate this argument) | ||
call("!", quote(x %in% y)) | ||
## !x %in% y | ||
|
||
call("`!`", quote(x %in% y)) | ||
## `\`!\``(x %in% y) | ||
call("`!`", "x %in% y") | ||
## `\`!\``("x %in% y") | ||
|
||
|
||
rlang::call2(`!`, quote(x)) | ||
## .Primitive("!")(x) | ||
``` | ||
|
||
``` r | ||
lobstr::ast(! x) | ||
## █─`!` | ||
## └─x | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
title: "not function" | ||
format: html | ||
--- | ||
|
||
Operator Precedence came up in today's discussion of this code from Advanced R 18.4.1 [https://adv-r.hadley.nz/expressions.html?q=%in%#operator-precedence](https://adv-r.hadley.nz/expressions.html?q=%in%#operator-precedence){.uri} | ||
|
||
```{r} | ||
knitr::opts_chunk$set(echo = TRUE, | ||
comment=" ##", | ||
collapse=TRUE) | ||
lobstr::ast(!x %in% y) | ||
``` | ||
|
||
The reason this works, if I followed the discussion correctly, is due to "operator precedence", %in% is evaluated before ! | ||
|
||
SEE <https://rdrr.io/r/base/Syntax.html> | ||
|
||
I want to suggest another way to see this. | ||
|
||
`!` is a function and, of course, functions take arguments, in this case exactly one argument. **If you mentally parse functions as lists, the function is first, followed by arguments.** lobstr::ast shows this. | ||
|
||
If you remember `!` is a function, then it's argument must be what remains: x %in% y. | ||
|
||
# Background !not function | ||
|
||
```{r two} | ||
library(lobstr) | ||
## usage examples | ||
x = TRUE | ||
!x | ||
! x # spacing not important | ||
`!` (x) | ||
`!`(x) | ||
## class | ||
`!` |> class() # function | ||
## structure | ||
lobstr::ast(!x) | ||
## source code | ||
`!` | ||
#error | ||
# `!` x | ||
``` | ||
|
||
```{r call} | ||
## first argument, as string | ||
## 2nd arugment as expression (otherwise, call will try to evaluate this argument) | ||
call("!", quote(x %in% y)) | ||
call("`!`", quote(x %in% y)) | ||
call("`!`", "x %in% y") | ||
rlang::call2(`!`, quote(x)) | ||
``` | ||
|
||
```{r} | ||
lobstr::ast(! x) | ||
``` |
Oops, something went wrong.