Description Usage Arguments Examples
View source: R/switch_branch.R
Allows the user to perform a switch-like
branch without breaking out of a pipeline.
To maintain the flow of a pipeline, it is recommended
to use fseq
style arguments (i.e. pipelines) for the cases,
however any function can be used. If no cases
match, then the original data is passed unchanged
1 | switch_branch(data, case, ..., warn = F)
|
data |
the data being passed through the pipeline. |
case |
an expression to be evaluated in the context of |
... |
the list of alternatives. If |
warn |
whether or not to warn that no cases were chosen |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | tibble::tibble(
x = rnorm(10),
y = sample(c("red", "blue", "yellow"),
10,
replace = TRUE
)
) %>%
dplyr::arrange(x) %>%
switch_branch(. %>%
dplyr::slice(1) %>%
dplyr::pull(y),
red = . %>%
pipe_cat("top was red\n") %>%
dplyr::filter(y == "red"),
blue = . %>%
pipe_cat("top was blue\n") %>%
dplyr::filter(x < 0)
) %>%
dplyr::summarise(m.x = mean(x))
palmerpenguins::penguins %>%
dplyr::mutate(species = factor(species, levels = c("Gentoo", "Adelie", "Chinstrap"))) %>%
dplyr::sample_n(1) %>%
switch_branch(
. %>%
dplyr::pull(species) %>%
as.numeric(),
. %>%
pipe_cat("Selected row is Gentoo\n"),
. %>%
pipe_cat("Selected row is Adelie\n"),
. %>%
pipe_cat("Selected row is Chinstrap\n")
)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.