Skip to content

Commit

Permalink
update substitution note
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMount committed Feb 1, 2020
1 parent 6de8b51 commit cbbc594
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 31 deletions.
30 changes: 18 additions & 12 deletions Examples/Substitution/Substitution.Rmd
Expand Up @@ -35,12 +35,15 @@ ops <- local_td(d) %.>%
cat(format(ops))
```

And we can execute the operations as follows.

```{r}
d %.>%
ops %.>%
knitr::kable(.)
```


This also works in the `wrap`/`ex` pattern.

```{r}
Expand Down Expand Up @@ -68,25 +71,28 @@ d %.>%
```



Note: `rquery` `1.3.9` has an issue with `bquote`/`.()` substitution in "imediate mode". We have fixed that in later versions.
We want to avoid using strings instead of names, as strings don't always give us the expected result. `rquery` tries to work around and catch many of these cases. For example:

```{r}
d %.>%
condition_name <- 'x'
ops2 <- local_td(d) %.>%
select_rows(.,
.(condition_variable) == 1) %.>%
extend(.,
.(new_value_variable) := .(old_value_variable) + 1) %.>%
knitr::kable(.)
.(condition_name) == 1)
cat(format(ops2))
```

New to `rquery` `1.4.3` we have a new "`-` to strip off quotes" feature. What this is: `.(-v)` converts `v` from a character type to a name. Meaning we don't have to use the `as.name()` calls if we don't want to.

```{r}
d %.>%
condition_name <- 'x'
ops3 <- local_td(d) %.>%
select_rows(.,
.(condition_variable) == 1) %.>%
project(.,
.(new_value_variable) := max(.(old_value_variable))) %.>%
knitr::kable(.)
.(-condition_name) == 1)
cat(format(ops3))
```


61 changes: 42 additions & 19 deletions Examples/Substitution/Substitution.md
Expand Up @@ -34,7 +34,11 @@ knitr::kable(d)

``` r
library("rquery")
```

## Loading required package: wrapr

``` r
condition_variable <- as.name('x')
new_value_variable <- as.name('y')
old_value_variable <- as.name('z')
Expand All @@ -57,6 +61,8 @@ cat(format(ops))
## extend(.,
## y := z + 1)

And we can execute the operations as follows.

``` r
d %.>%
ops %.>%
Expand Down Expand Up @@ -103,32 +109,49 @@ d %.>%
| -: |
| 7 |

Note: `rquery` `1.3.9` has an issue with `bquote`/`.()` substitution in
“imediate mode”. We have fixed that in later versions.
We want to avoid using strings instead of names, as strings don’t always
give us the expected result. `rquery` tries to work around and catch
many of these cases. For example:

``` r
d %.>%
condition_name <- 'x'

ops2 <- local_td(d) %.>%
select_rows(.,
.(condition_variable) == 1) %.>%
extend(.,
.(new_value_variable) := .(old_value_variable) + 1) %.>%
knitr::kable(.)
.(condition_name) == 1)
```

| x | y | z |
| -: | -: | -: |
| 1 | 7 | 6 |
| 1 | 8 | 7 |
## Warning in warn_about_filter_conditions(parsed): rquery::select_rows: expression
## "x" == 1 refers to no columns (so is a constant)

``` r
d %.>%
cat(format(ops2))
```

## mk_td("d", c(
## "x",
## "y",
## "z")) %.>%
## select_rows(.,
## "x" == 1)

New to `rquery` `1.4.3` we have a new “`-` to strip off quotes” feature.
What this is: `.(-v)` converts `v` from a character type to a name.
Meaning we don’t have to use the `as.name()` calls if we don’t want to.

``` r
condition_name <- 'x'

ops3 <- local_td(d) %.>%
select_rows(.,
.(condition_variable) == 1) %.>%
project(.,
.(new_value_variable) := max(.(old_value_variable))) %.>%
knitr::kable(.)
.(-condition_name) == 1)

cat(format(ops3))
```

| y |
| -: |
| 7 |
## mk_td("d", c(
## "x",
## "y",
## "z")) %.>%
## select_rows(.,
## x == 1)

0 comments on commit cbbc594

Please sign in to comment.