0

I've got the following code:

fit.list <- lapply(sample.names, function(sample.name) {
    lapply(f1(target), function(seq.name) {
        f2(arguments)
    })
})

f2 is returning sometimes:

warning("Warning message")
return(NULL)

How can I catch the warning message inside the second lapply? I'm a newbie to R, excuse me.

EDITED:

Could be one of these workarounds?

1

tryCatch({
        fit.list <- lapply(sample.names, function(sample.name) {
            print(sample.name);
            lapply(f1(target), function(seq.name) {
                f2(arguments)
            })
        })
    }, 
    warning=function(e) {
            message(paste("Warning caused by sample:", sample))
            return(NULL)
        })

2

fit.list <- lapply(sample.names, function(sample.name) {
    
    tryCatch({
        lapply(f1(target), function(seq.name) {
            f2(arguments)
        })
    }, 
    warning=function(e) {
            message(paste("Warning caused by sample:", sample))
            return(NULL)
        })
})

3

fit.list <- lapply(sample.names, function(sample.name) {

    lapply(f1(target), function(seq.name) {
    
        tryCatch({
            f2(arguments)
        }, 
        warning=function(e) {
            message(paste("Warning caused by sample:", sample))
            return(NULL)
        })  
    })
})

Thank you very much!

zx8754
  • 52,746
  • 12
  • 114
  • 209
Solar
  • 445
  • 1
  • 4
  • 12
  • If you want to stop warning from displaying you can use `suppressWarnings()`. – Ronak Shah Jun 29 '20 at 06:38
  • Hello Ronak, no I want to catch the warning and extract some info. For example what 'arguments' values caused the warning. Thanks! – Solar Jun 29 '20 at 06:47
  • Can you make this question reproducible and show expected output for it? – Ronak Shah Jun 29 '20 at 06:49
  • Hello again ronak, I'm afraid I can not. This piece of code is part of the user guide for an external R library (exomeCopy https://www.bioconductor.org/packages/release/bioc/vignettes/exomeCopy/inst/doc/exomeCopy.pdf) and needs several external input resources that I can't provide. Thank you very much! – Solar Jun 29 '20 at 06:51
  • Related post: https://stackoverflow.com/a/4952908/680068, see Martin's `factory` function. – zx8754 Jun 29 '20 at 07:29
  • First off, I'd check your argument names. You use `sample.name` as an argument in your first anonymous function but don't use it in its body. Similarly, `seq.name` in the second anonymous function. That strikes me as odd. Also, you haven't defined `target` or `arguments` in your code fragment. As @Ronak said, without a MWE, our options to help are limited. – Limey Jun 29 '20 at 07:31
  • I am going to test first options(warn=2) (or 1) before this workaround, only for simplicity. Thank you!. – Solar Jun 29 '20 at 07:39

0 Answers0