SummerEla SummerEla - 1 month ago 20
R Question

R: pass user arg to DESeq2 function

I'm trying to run DESeq in an RScript using parameters input from the command line. I used optparse to parse user arguments and am trying to pass the design argument into the DESeqDataSetFromMatrix() function.

I tested the function directly and it works perfectly:

DESeq_tbl <- DESeqDataSetFromMatrix(countData=counts_tbl,
colData=coldata, design=~taxonomy)


However, if I try to pass the variable 'opt$design' (which is a character string = "~taxonomy"), I get the following error:

DESeq_tbl <- DESeqDataSetFromMatrix(countData=counts_tbl,
colData=coldata, design=opt$design)

Error: $ operator is invalid for atomic vectors
Execution halted


I've tried noquote(), various combinations of cat/paste and creating the entire command as a string to pass to the DESeqDataSetFromMatrix() function, but nothing has worked. Any advice would be greatly appreciated. Thanks!

THE SOLUTION:
Thanks to Ben Bolker's answer below, the following worked:

DESeq_tbl <- DESeqDataSetFromMatrix(countData=counts_tbl,
colData=coldata, design=as.formula(opt$design))

Answer

I think you need as.formula(opt$design).

x <- "~taxonomy"
f <- ~taxonomy
str(f)
## Class 'formula'  language ~taxonomy
## ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
identical(f,as.formula(x)) ## TRUE
Comments