I have a crazy idea where I'd like to construct dplyr
logic and THEN apply it to a pipeline. Here's an example of what I'm trying to do:
# create a single function for input data
# mutated column
# then select columns
eval_data <- function(data, mutated_column_name, mutation_logic, select_vector) {
data %>%
mutate(
mutation_logic
) %>%
select(
select_vector
)
}
# user sets name of mutated column
col_name <- "col_y"
# user creates some mutation logic
my_mutation <- function(mutated_column_name) {
mutated_column_name = case_when(
Sepal.Length > 7 ~'Big',
Sepal.Length > 6 ~ 'Medium',
TRUE ~ 'Small'
)
}
# user selects columns they want
selects <- c("col_y", "Species")
eval_data(iris, my_mutation(col_name), selects)
Desired output:
iris %>%
mutate(
col_y = case_when(
Sepal.Length > 7 ~'Big',
Sepal.Length > 6 ~ 'Medium',
TRUE ~ 'Small'
)
) %>%
select("col_y", "Species")
I've played with turning the mutation logic into a string to be evaluated in the pipeline but that seems fragile and is a little - I was wondering if people had better ideas or suggestions for this?