RGLab / CytoML

A GatingML Interface for Cross Platform Cytometry Data Sharing
GNU Affero General Public License v3.0
29 stars 14 forks source link

try(eval(substitute(subset)), silent = TRUE) in flowjo_to_gatingset causes error when the subset argument is passed by lapply #136

Closed Close-your-eyes closed 2 years ago

Close-your-eyes commented 3 years ago

I ran into a strange error when calling

lapply(smps, function(i) { gs <- CytoML::flowjo_to_gatingset(wsp, name = wsp.groups[x], path = FCS.file.folder, truncate_max_range = F, subset = i) }) Error in CytoML::flowjo_to_gatingset(wsp, name = wsp.groups[x], path = FCS.file.folder, : invalid 'subset' argument!

I was 100 % certain that subset i (a sample name) existed in wsp. Also using an index (seq_along(smps)) caused the same error.

What I noticed:

When i is not defined in the parent environment the line try(eval(substitute(subset)), silent = TRUE) in CytoML::flowjo_to_gatingset() does not work out. See this reprex (i not set in parent environment first):

evaltest <- function(y) { print(substitute(y)) print(eval(substitute(y))) }

sapply(c(1,2,3), function (i) { evaltest(i) }) for (i in c(1,2,3)) { evaltest(i) }

The for-loop works but sapply fails. When you set i <- 1 in the parent environment though also the sapply-approach works.

Something similar may have been discussed before elsewhere: https://stackoverflow.com/questions/18508790/deparsesubstitutex-in-lapply

apply and the loop seem to pass the argument differently.

Can this be fixed in flowjo_to_gatingset() or do you have another advice how to pass the subset argument by lapply or sapply? Okay, I already provided a solution (setting the variable in parent environment) but this seems not very handy.

Close-your-eyes commented 3 years ago

Honestly, I fiddled a bit more and I am not sure if I nailed the problem to its root with the observation of for-loop vs lapply that I mentioned above. In general passing the subset argument does not give expected results, also when passed by a for-loop. In another case (for-loop) always the first sample in the gs was returned - the iterator i was set to 1 in the parent environment.

I removed the whole part that checks the subset argument in flowjo_to_gatingset. Now it is working for me.