2 min read

[Bug Fixes on beginr] if() function with conditions of length > 1

原文链接: steemit, cnsteem, busy, chainbb, steemdb, steemd, busy, markdown



The issue

CRAN sent a notice to me, mentioning:

(The packages) fail when checking with env var R_CHECK_LENGTH_1_CONDITION set to true, which turns using if() with conditions of length greater than one from a warning into an error.

We really would like to have (the current development version of) R move from giving a warning to giving an error by default in the near future, but would prefer not breaking too many CRAN (and BioC) packages.

For example, if you run the codes:

x <- 1:3
if(x > 2) help()

the current version of R (3.5) returns a warning:

Warning message: In if (x > 2) help() : the condition has length > 1 and only the first element will be used

with such a warning, the program is still working. In the future, the new version of R will return an error instead, which would terminate the program.

Although it is not a bug in the current version of R, it will be a bug in the future in the new version of R.

My fixes of the bug is documented in the latest commit:


The fixes have been proved by CRAN and the new version of beginr (v.0.1.3) has been accepted by CRAN:


The solution

I added a condition check outside every if() function which has the possibility that the users could use a parameter with length > 1. If the parameter has a length > 1, the program will give the user a message ‘one of the parameters must have a length = 1.’

A mini example is as follows:

x <- 1:3
if(length(x) == 1) {
  if(x > 2) help()
} else {
  message('one of the parameters must have a length = 1.')

R returns:

one of the parameters must have a length = 1.