Skip to content

Commit

Permalink
Add some more base R callouts
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Oct 30, 2023
1 parent 2c67054 commit bf4bcd5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions defaults-short-and-sweet.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ sample.int <- function (n, size = n, replace = FALSE, prob = NULL, useHash = NUL
}
```

This pattern is made more elegant with the infix `%||%` operator which you can use by importing it from rlang or copying and pasting it in to your `utils.R`:
This pattern is made more elegant with the infix `%||%` operator which is built in to R 4.4.
If you need it in an older version of R you can import it from rlang or copy and paste it in to your `utils.R`:

```{r}
`%||%` <- function(x, y) if (is.null(x)) y else x
Expand Down Expand Up @@ -170,7 +171,7 @@ Because it has a relatively complex explanation, it's nice to be able to documen
If the above techniques don't work for your case there are two other alternatives that we don't generally recommend but can be useful in limited situations.

::: {.callout-note collapse="true"}
#### Sentinel value {#sec-args-default-sentinel}
#### Sentinel value {#defaults-short-and-sweet-defaults-short-and-sweet-sec-args-default-sentinel}

Sometimes you'd like to use the `NULL` approach defined above, but `NULL` already has a specific meaning that you want to preserve.
For example, this comes up in ggplot2 scales functions which allow you to set the `name` of the scale which is displayed on the axis or legend.
Expand Down
6 changes: 6 additions & 0 deletions dots-after-required.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ mean3(x, , TRUE)
mean3(x, n = TRUE, t = 0.1)
```

::: {.callout-note collapse="true"}
## Base R

In base R you can use `base::chkDots()`, but it uses a slightly simpler technique which means it's not suitable for usage in S3 methods.
:::

## See also

- @sec-dots-data: if `` is a required argument because it's used to combine an arbitrary number of objects in a data structure.
Expand Down

0 comments on commit bf4bcd5

Please sign in to comment.