Skip to content

Commit

Permalink
900_not function
Browse files Browse the repository at this point in the history
  • Loading branch information
jimrothstein committed Dec 5, 2024
1 parent 3f8866f commit 40b4da1
Show file tree
Hide file tree
Showing 16 changed files with 4,010 additions and 3 deletions.
14 changes: 13 additions & 1 deletion NSE_project/17_04_call2.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ f <- function(...) sum(...)
# create call, no evaluation (whether f, g exist or not)
call2("f", 1, 2, 3) # f(1, 2, 3)
call2("g", 1, 2, 3) # f(1, 2, 3)
call2("g", 1, 2, 3) # g(1, 2, 3)
call2("g", 1, 2 + 10, 3) # g(1, 12, 3)
## evaluates arguments, not function itself
x=-1
call2("g", 1, 2 + 10, 3 + x) # g(1, 12, 22)
call2("g", x, 2+x, 3+x) # g(-1, 1, 2)
rm(x)
lobstr::ast(call2("f", 1, 2, 3))
# █─call2
Expand All @@ -41,6 +48,9 @@ if (FALSE) call2("f", 1, 2, 3, x)
# but ast freezes (stops substitution here)
ast(call2("f", 1, 2, 3, x))
#
```


Expand All @@ -53,6 +63,8 @@ ast(call2("+", 1, call2("*", 2, 3)))
ast(call2("mean", x, na.rm = TRUE))
```



## Create template, or way to create more complicated functions from simpler
```{r}
cv <- function(var) {
Expand Down
2 changes: 0 additions & 2 deletions NSE_project/500_cheat.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ title: cheat
editor_options:
chunk_output_type: console
---


```{r}
library(rlang)
Expand Down
602 changes: 602 additions & 0 deletions NSE_project/900_not_function.html

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions NSE_project/900_not_function.md
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
```
68 changes: 68 additions & 0 deletions NSE_project/900_not_function.qmd
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)
```
Loading

0 comments on commit 40b4da1

Please sign in to comment.