I'm trying to conditionally execute logic within a pipeline. Any ideas how to do this with the basepipe?
library(tidyverse)
qc <- TRUE
cars_mag <- rownames_to_column(mtcars, var="model") %>%
mutate(
mpg_cat=case_when(
mpg <= 15 ~ "terrible",
mpg <= 20 ~ "okay",
mpg <= 25 ~ "pretty good",
TRUE ~ "awesome"
)
) %>%
{if(qc) assign("inspect1", ., envir=globalenv()) else .} %>%
group_by(mpg_cat) %>%
summarize(
across(c(mpg, disp, hp), list(mean=mean, sum=sum))
)
cars_bp <- rownames_to_column(mtcars, var="model") |>
mutate(
mpg_cat=case_when(
mpg <= 15 ~ "terrible",
mpg <= 20 ~ "okay",
mpg <= 25 ~ "pretty good",
TRUE ~ "awesome"
)
) |>
{if(qc) assign("inspect1basepipe", value=_, envir=globalenv()) else _} |>
group_by(mpg_cat) |>
summarize(
across(c(mpg, disp, hp), list(mean=mean, sum=sum))
)
#> Error in {: function '{' not supported in RHS call of a pipe (<text>:29:3)
Created on 2025-08-22 with reprex v2.1.1