I wasn't aware of this eval magic :-D nice!
Hi Guys,
I have always some trouble when I have to translate my BASH strategy to R. I am using edgeR for differential analysis, and have 16 lrts, named lrt1 to lrt16. Now I want to make MA plot in a for loop, and want to use paste to direct to the lrt1 to lrt16, like this.
i=1
summaryDE<-summary(de <- decideTestsDGE(paste0("lrt",i), p=0.05))
Error in decideTestsDGE(paste0("lrt", i), p = 0.05) :
Need DGEExact or DGELRT object
When I use lrt1 instead of the paste0 stuff it does work :/
What did I forget? Thanks in advance!
Ben
1 answer
Error in decideTestsDGE(paste0("lrt", i), p = 0.05) :
Need DGEExact or DGELRT object
this is obvious, paste0("lrt", i) will work and outptu "lrt1" and use it as input of decideTestsDGE which require DGEExact or DGELRT object. But here the input "lrt1" is just a character object.
see the function definition in https://github.com/Bioconductor-mirror/edgeR/blob/master/R/decidetestsDGE.R
if(!is(object,"DGEExact") & !is(object,"DGELRT")) stop("Need DGEExact or DGELRT object") # Expects a DGEExact or DGELRT object
throw you the error.
This is not an issue of paste0, but the input required by decideTestsDGE.
What you want is to input lrt1 object. In your code, you are inputting "lrt1" character string, but not the object you are trying to refer.
You can use eval(parse(text=paste0("lrt", i))) to parse "lrt1" to get the corresponding object.
So you code should be:
summaryDE<-summary(de <- decideTestsDGE(eval(parse(text=paste0("lrt", i))), p=0.05))
Thanks for your help!
Log in to answer this question.
My first guess is that your paste returns a string, not the object. A real simple toy example shows you that:
This cat returns the string "a22" and not the value of the variable a22 ('foo')
In contrast, observe the following:
This cat returns 'foobarbaz' (no separator specified) and accesses the value behind the object.
Thanks Wouter, if I understand correct it is not possible to paste the object names together in a for loop? But I have to first make a list with lrt1 to lrt16, and loop thru it?
That would work I guess. How did you create the lrt objects? Perhaps you can just append each object, after it's created, to a vector and then loop over it. Or if you are working interactively, do some lazy stuff like
paste(paste0("lrt", 1:16), collapse = ', ')and copy paste that in your new vectorLooks stupid, might be a better way to do this but if it works it's no longer stupid right?
I found out that I have to put
getbefore it...Thanks for the help!
So there was a better way to do it! Good job ;-)