Search code examples
rfunctionvariablesphyloseq

Using an r variable in a parameter that expects a function


I have a loop through column names which returns a variable character. I would like to use the variable in a parameter that expects a function.

Not Working

i = "Cooling"
temp = phyloseq_to_deseq2(p2, ~i)}

Works

temp = phyloseq_to_deseq2(p2, ~Cooling)

Not Working

temp = phyloseq_to_deseq2(p2, ~"Cooling")}

The question below highlights the problem the str reference is not the r object in the function. Phyloseq_to_deseq2 requires the phyloseq package. How can I use a variable in a function that expects a function in a parameter?

R: specifying variable name in function parameter for a function of general (universal) use


Solution

  • Thanks to @joran in the comments. Using the function as.formula on a string containing the ~ passed the function as a parameter.

    my_formula = as.formula("~Cooling")
    temp = phyloseq_to_deseq2(p2, my_formula}