rformassspectrometry / Spectra

Low level infrastructure to handle MS spectra
https://rformassspectrometry.github.io/Spectra/
34 stars 24 forks source link

Bug in show,ProcessingStep #162

Closed lgatto closed 3 years ago

lgatto commented 3 years ago

From ?ProcessingStep:

> ps <- ProcessingStep(sum)
> ps
Object of class "ProcessingStep"
 Function: Error in cat(" Function: ", object@FUN, "\n", sep = "") : 
  argument 2 (type 'builtin') cannot be handled by 'cat'

This however works:

> executeProcessingStep(ProcessingStep("sum"), 1:10)
[1] 55
> ps <- ProcessingStep("sum")
> ps
Object of class "ProcessingStep"
 Function: sum
> executeProcessingStep(ps, 1:10)
[1] 55

The show method fails when FUN is a function given that is can't be coerced to a character.

But that's not all - the same issue happens when on of the arguments in @ARGS is itself a function, for example when calling filterIntensity(x, my_function).

Here's a suggestion:

setMethod("show", "ProcessingStep", function(object) {
    cat("Object of class \"", class(object), "\"\n", sep = "")
    if (is.function(object@FUN)) .FUN <- "FUN"
    if (is.character(object@FUN)) .FUN <- object@FUN
    cat(" Function: ", .FUN, "\n", sep = "")
    args <- object@ARGS
    if (length(args) > 0) {
        cat(" Arguments:\n")
        for (i in seq_along(args)) {
            if (is.function(args[[i]])) .ARG <- "FUN"
            else .ARG <- args[[i]]
            cat("  o ", names(args)[i], " = ",
                paste(.ARG, collapse = ", "), "\n", sep = "")
        }
    }
})

That would produce

Object of class "ProcessingStep"
 Function: FUN
 Arguments:
  o intensity = FUN
  o msLevel = 1

Not sure if this is very helpful though.

jorainer commented 3 years ago

Alternative suggestion:

> ps <- ProcessingStep(function(z) z + 4)
> show(ps)
Object of class "ProcessingStep"
 Function: function(z) z + 4
> ps <- ProcessingStep(sum, list(a = sqrt))
> show(ps)
Object of class "ProcessingStep"
 Function: function (..., na.rm = FALSE)  .Primitive("sum")
 Arguments:
  o  a  = function (x)  .Primitive("sqrt")
lgatto commented 3 years ago

Sure, that's better.