[Bioc-devel] condition tests on vectors with length greater than 1

Martin Morgan martin.morgan at roswellpark.org
Wed Mar 29 23:37:29 CEST 2017


Bioc developers,

R emits a warning when a condition test has length > 1

$ R --vanilla
 > if (letters == "a") TRUE
[1] TRUE
Warning message:
In if (letters == "a") TRUE :
   the condition has length > 1 and only the first element will be used

These are almost always programming errors.

R-3-4-branch and R-devel can be configured to report such errors, as 
described on the help page for, e.g., `?"if"`

$ _R_CHECK_LENGTH_1_CONDITION_=TRUE R --vanilla
 > if (letters=="a") TRUE
Error in if (letters == "a") TRUE : the condition has length > 1

The attached file (thanks to Tomas Kalibera) summarizes Bioconductor 
code that triggers this type of error.

If you maintain one of these packages (appearing in either column), 
please address the error. And of course as a developer, please avoid 
making the error in the future!

 > r = read.csv("long-conditional-bioc.csv")
 > r[, c("FailedPackage", "Srcref")]
    FailedPackage                                          Srcref
1     biovizBase               biovizBase/R/crunch-method.R#295
2  branchpointer               branchpointer/R/makeRegions.R#41
3       Cardinal                        Cardinal/R/spatial.R#57
4      debrowser                       debrowser/R/heatmap.R#38
5        DMRcate                        DMRcate/R/DMR.plot.R#13
6      exomePeak                          exomePeak/R/RMT.R#119
7          fabia                           fabia/R/fabia.R#1504
8   GenomeInfoDb             GenomicFeatures/R/TxDb-class.R#377
9         Glimma                           Glimma/R/hexcol.R#32
10     GOexpress                 VennDiagram/R/adjust.venn.R#42
11      GUIDEseq GUIDEseq/R/offTargetAnalysisOfPeakRegions.R#95
12      hapFabia  hapFabia/R/methods-IBDsegmentList-class.R#110
13     MassArray                   MassArray/R/convControl.R#26
14    methylPipe                methylPipe/R/Allfunctions.R#635
15        NOISeq               NOISeq/R/biodetection.plot.R#157
16      pathview                  pathview/R/geneannot.map.R#31
17      phyloseq              phyloseq/R/multtest-wrapper.R#101
18         rHVDM              rHVDM/R/measurementerrorHVDM.R#23
19          SEPA                  SEPA/R/truetimevisualize.R#28
20      SPLINTER                 SPLINTER/R/main_splinter.R#817

As an example the GenomeInfoDb package (row 8) has this complete record

FailedPackage "GenomeInfoDb"
IfPackage     "GenomicFeatures"
File          "GenomeInfoDb.Rcheck/GenomeInfoDb-Ex.Rout"
Function      "S4 Method seqlevels<-:GenomeInfoDb defined in namespace
                GenomicFeatures with signature TxDb has this body."
Srcref        "GenomicFeatures/R/TxDb-class.R#377 "

The problem was from

   GenomicFeatures/R/TxDb-class.R#377

which has

     mode <- GenomeInfoDb:::getSeqlevelsReplacementMode(value, x_seqlevels0)
     if (mode == -2L) {

I looked at

 > GenomeInfoDb:::getSeqlevelsReplacementMode
function (new_seqlevels, old_seqlevels)
{
     ...

and saw that its code returns a vector with length > 1 intentionally 
under some specific circumstances. Also, all other uses of the return 
value of this function (in the GenomeInfoDb and GenomicFeatures package) 
test for identity of the return value via `identical()`, which is always 
a scalar. This suggests the fix

GenomicFeatures$ svn diff R/TxDb-class.R
Index: R/TxDb-class.R
===================================================================
--- R/TxDb-class.R	(revision 127829)
+++ R/TxDb-class.R	(working copy)
@@ -374,7 +374,7 @@
      ## detect the situation where the user intention is to subset the 
"real"
      ## seqlevels.
      mode <- GenomeInfoDb:::getSeqlevelsReplacementMode(value, 
x_seqlevels0)
-    if (mode == -2L) {
+    if (identical(mode, -2L)) {
          ## "subsetting of the real seqlevels" mode
          x$user_seqlevels <- value
          x$user2seqlevels0 <- match(value, x_seqlevels0)

I did do some more digging around.

 From the report, the failure was detected while running the example 
page script generated while checking GenomeInfoDb.

   GenomeInfoDb.Rcheck/GenomeInfoDb-Ex.Rout

So I generated this

   R CMD build --no-build-vignettes GenomeInfoDb
   R CMD check GenomeInfoDb_1.27.11.tar.gz

and processed it to find the error

   _R_CHECK_LENGTH_1_CONDITION_=TRUE R -f 
GenomeInfoDb.Rcheck/GenomeInfoDb-Ex.R

It failed while running the example on the help page with name 
"seqlevels-wrapper"

 > ### Name: seqlevels-wrappers
 > ### Title: Convenience wrappers to the seqlevels() getter and setter
 > ### Aliases: seqlevels-wrappers keepSeqlevels dropSeqlevels 
renameSeqlevels
 > ###   restoreSeqlevels standardChromosomes keepStandardChromosomes
 > ### Keywords: methods utilities
 >
 > ### ** Examples
 >
...
 > renameSeqlevels(txdb, sub("chr", "CH", seqlevels(txdb)))
Error in if (mode == -2L) { : the condition has length > 1
Calls: renameSeqlevels -> seqlevels<- -> seqlevels<-
Execution halted

I then could get a relatively simple reproducible example in R with

$ _R_CHECK_LENGTH_1_CONDITION_=TRUE R --vanilla
 > suppressPackageStartupMessages(library(GenomeInfoDb))
 > example("seqlevels-wrappers")

After installing the updated GenomicFeatures package, the error did not 
occur. Likewise, running GenomeInfoDb-Ex.R did not generate any errors.

Martin


This email message may contain legally privileged and/or confidential information.  If you are not the intended recipient(s), or the employee or agent responsible for the delivery of this message to the intended recipient(s), you are hereby notified that any disclosure, copying, distribution, or use of this email message is prohibited.  If you have received this message in error, please notify the sender immediately by e-mail and delete this email message from your computer. Thank you.


More information about the Bioc-devel mailing list