Content-Length: 287872 | pFad | http://github.com/tidyverse/ggplot2/pull/6215/commits/eaeee6d75cc60118919ca4a06a04cd4549af123c

4D Vignette: discuss optional string input by teunbrand · Pull Request #6215 · tidyverse/ggplot2 · GitHub
Skip to content

Vignette: discuss optional string input #6215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add piece about character-based optional arguments
  • Loading branch information
teunbrand committed Dec 4, 2024
commit eaeee6d75cc60118919ca4a06a04cd4549af123c
20 changes: 20 additions & 0 deletions vignettes/ggplot2-in-packages.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ col_summary <- function(df, col, by) {
col_summary(mpg, "drv", "year")
```

When using this strategy, you may need some extra care in passing optional arguments.
You can use optional arguments by setting `NULL` as a default value, and treating non-null input with `rlang::data_syms`.
Thereafter, these arguments can be spliced using the `!!!` operation.

```{r}
col_summary <- function(df, col, by = NULL, fill = NULL, ...) {

optional <- list(fill = fill, ...)
is_symbol <- lengths(optional) > 0
optional <- c(data_syms(optional[is_symbol]), optional[!is_symbol])

by <- if (!is.null(by)) facet_wrap(vars(.data[[by]]))

ggplot(df) +
geom_bar(aes(y = .data[[col]], !!!optional)) +
by
}

col_summary(mpg, "drv", colour = "class")
```
### Non-standard evaluation

If the column name or expression is supplied by the user, you can also pass it to `aes()` or `vars()` using `{{ col }}`. This tidy eval operator captures the expression supplied by the user and forwards it to another tidy eval-enabled function such as `aes()` or `vars()`.
Expand Down
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/tidyverse/ggplot2/pull/6215/commits/eaeee6d75cc60118919ca4a06a04cd4549af123c

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy