execute_if: Conditional execution in a pipeline

Description Usage Arguments Value Examples

View source: R/dplyr-like.R

Description

A verb for a magrittr pipeline: execute_if_else: The language is executed only if .predicate is true.

Usage

1
2
3
execute_if(.data, .predicate, .language)

execute_if_else(.data, .predicate, .language_true, .language_false)

Arguments

.data

Data argument, typical "first" argument in dplyr verbs

.predicate

Evaluated to boolean. If true, executes and returns language; otherwise, returns untouched .data

.language

Language call to execute. Write is just as if you would without the execute_if: Will be used as the right-hand side of "%>%" with all possible options of magrittr.

.language_true

Execute if predicate it TRUE

.language_false

Execute if predicate it FALSE

Value

Result of .language

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
library(magrittr)
library(dplyr)
library(tibble)
library(stringr)
convert_to_quartiles <- TRUE
tibble(score=c(1,2,3,4,1,2,3,4,2,3,2,3,4,3,3)) %>%
  mutate(do_something=1) %>%
  execute_if(convert_to_quartiles,
             mutate(score = cut(score, 4, labels = str_c("Quartile ", 1:4)))) %>%
  filter(score > 2)

Example output

Attaching package:dplyrThe following objects are masked frompackage:stats:

    filter, lag

The following objects are masked frompackage:base:

    intersect, setdiff, setequal, union

# A tibble: 0 x 2
# … with 2 variables: score <fct>, do_something <dbl>
Warning message:
In Ops.factor(score, 2) :>not meaningful for factors

tidytidbits documentation built on March 8, 2021, 5:07 p.m.