Search code examples
rnse

How to properly address covariates in functions with their names listed into strings?


I have a long list of covariates that I want to store as a string in order to work with it more conveniently in several functions.

I think the issue is related to this question, but I can't apply it to cases different to regression formulas.

xvar <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")

with(iris, iris[order(Sepal.Length, Sepal.Width, 
                      Petal.Length, Petal.Width), ])  # working

with(iris, iris[order(xvar), ])  # not working

Or an other case

with(iris, ave(Sepal.Length, Sepal.Width, 
           Petal.Length, Petal.Width))  # working

with(iris, ave(xvar))  # not working
with(iris, ave(noquote(paste(xvar, collapse = ", "))))  # not working as well

Especially the paste-version I don't understand why it's not working, though it yields exactly the same string as the one in the parentheses:

> noquote(paste(xvar, collapse = ", "))
[1] Sepal.Length, Sepal.Width, Petal.Length, Petal.Width

What am I missing?


Solution

  • It's important to note that character values and actual code are very different. Just compare

     with(iris, order("Sepal.Length"))
     with(iris, order(Sepal.Length))
    

    Functions like paste() only make strings and noquote() also just returns strings, it just doesn't print the quotes. If you are trying to dynamically create code, you are generally going to have to parse string into proper R language objects. Sometimes you can do some sneaky alternatives.

    One possible work around is to use do.call to pass the columns that you want to sort by as separate parameters to order. You can do

    iris[do.call("order", iris[xvar]), ]
    

    the dplyr package has tidyevels methods to also help with stuff

    library(dplyr)
    iris %>% arrange(!!!rlang::syms(xvar))