friendlyeval (version 0.1.1)

treat_string_as_col: treat_string_as_col

Description

Take the a string value and use it as a column name in a dplyr function.

Usage

treat_string_as_col(arg)

Arguments

arg

the argument that holds a value to be used as a column name.

Value

something that will resolve to a column name when prefixed with `!!`.

Details

This is used to take the string value of a variable and use it in place of a literal column name when calling a dplyr function. This ability is useful when the name of the column to operate on is determined at run-time from data.

Examples

Run this code
# NOT RUN {
## drop this run-time determined column.
b <- "cyl"
 select(mtcars, -!!treat_string_as_col(b))

## function double a column
double_col <- function(dat, arg) {
  dplyr::mutate(dat, result = !!rlang::sym(arg) * 2)
}
double_col(mtcars, arg = 'cyl')

# }

Run the code above in your browser using DataCamp Workspace