I have a wrapper function which uses dplyr::select(), however when I attempt to use it to select columns by name, it throws an object * not found error. I know it has something to do with the way select() uses quasiquotation but I don't know exactly why.
Here is my attempt using as_mapper:
fun1 = as_mapper(~select(.x, .y)) fun1(iris, Species) Error in .f(.x[[i]], ...) : object 'Species' not found Using base function notation:
fun2 = function(dat, x) {select(substitute(dat), substitute(x))} fun2(iris, Species:Sepal.Length) Error in UseMethod("select_") : no applicable method for 'select_' applied to an object of class "name" I would be grateful if someone could shed some light on why these errors are occurring.
as_mapperexpects a character vector (or numeric for number-based indexing). Callingfun1(iris, "Species")orfun1(iris, 5)works, but not whenSpeciesis supplied as anameof an object (that might not exist in the environment).substitute()fix that?dplyryou would usefun3 = function(dat, x) {select({{dat}}, {{x}})}; fun3(iris, Species:Sepal.Length). The problem with substitute is it happens too late. The problem withas_mapperis it doesn't support non-standard evaluation.