Version 4.0.0 is long gone, and – except when merge() was slower than dust – it was really stable, at least for me (I use it every day on MacOS, Linux and Windows). Of course it came with some feature/header updates, but I started to see what other useful nuggets might be in the changelog and decided to blog them as I found them.

Today’s nugget is a respectable stopifnot() function, which has been significantly improved by this PR by Neil Fultz.

If you wanted to use the stopifnot() function to check the input data for version 4.0.0 (in this case [claims]) (https://en.wikipedia.org/wiki/Assertion_(software_development)), you would have done something similar for version 4.0.0 (I’ll take Neil as an example):

some_ƒ <- function(alpha, gradtol, steptol, interlim) { stopifnot((is.numeric(alpha))), (length(alpha) == 1), (alpha > 0), (alpha < 1), (is.numeric(gradtol)), (length(gradtol) == 1), (is.).numeric (steptol)), (length(steptol) == 1), (steptol > 0), (is.numeric (interlim)), (length(interlim) == 1), (interlim > 0) ) Message. (Do something big).

If we work with acceptable inputs, we get

some_ƒ(0.5, 3, 10, 100)## Do something amazing…

Navigate to these guys 24×7 Outsource Support.

But if you’re walking around with something coming out of the oven:

some_ƒ(a, 3, 10, 100)## Error in some_ƒ(a, 3, 10, 100): (is.numerical(alpha)) not WHERE

we get a half-hearted, but somewhat unfriendly message. Of course this gives the right expression, but we need to be a more user-friendly, more welcoming language of data science (and general use) that cares a little more about our users. That’s why a lot of people turn to something like this:

some_ƒ <- function(alpha, gradtol, steptol, interlim) { as (!is.numeric(alpha)). { stop(‘Error: alpha must be numeric’) } as (length(alpha)! = 1) { stop(‘Error: alpha must be a single value’); }. as (alpha < 0) { stop(‘Error: alpha is negative’); } as (alpha > 1) { stop(‘Error: alpha is more than one’); } as (!is.numeric(gradtol)). { stop(‘Error: gradtol must be a numerical value’) } as (length(gradtol)! = 1) { stop(‘Error: gradtol must be a single value’); }. if (gradtol <= 0) { stop(‘Error: gradtol must be positive’); } if (! is.numeric(steptol)) { stop(‘Error: steptol must be a number’) } as (length(steptol)! = 1) { stop(‘Error: steptol must be a single value’); }. as (steptol <= 0) { stop(‘Error: steptol must be positive’); } as (! is.numeric(iterlim)). { stop(‘Error: iterlim must be a number’) } as (length(iterlim)! = 1) { stop(‘error: iterlim must be a scalar value’); }; }. if (iterlim <= 0) { stop(‘error: iterlim must be positive’); } } message(Do something big)}.

leading to the fact that

someƒ(a, 3, 10, 100)## Error in someƒ(a, 3, 10, 100) : Wrong: Alpha must be numeric

(You can make even better error messages).

Neal thought there had to be a better way, so he did! Phrases… …can be named, and those names turn into an error message:

some_ƒ <- function(alpha, gradtol, steptol, interlim) { stopifnot( ‘alpha must be numeric’ = (is.numeric(alpha)), ‘alpha must be a value’ = (length(alpha) == 1), ‘alpha is negative’ = (alpha > 0), ‘alpha is greater than one’ = (alpha < 1), ‘gradtol must be numeric’ = (is.numeric (gradtol), gradtol must be a scalar value = (length (gradtol) == 1), gradtol must be positive = (gradtol > 0), steptol must be numeric = (is.numeric (steptol), steptol must be a scalar value = (length (steptol) == 1), steptol must be positive = (steptol > 0), iterlim must be numeric = (est.numeric(interlim)), ‘iterlim must be a scalar value’ = (length(interlim) == 1), ‘iterlim must be positive’ = (interlim > 0) ) message(Do something great)}some_ƒ(a, 3, 10, 100)## Error in some_ƒ(a, 3, 10, 100): alpha must be numeric

It is much easier to write and much more respectful of the interlocutor.

**Eligible statistics**

The CRAN has about 2600 packets using the stopifnot() function in its /R/ code packet with the following selected distributions (all log10 scale diagrams)

Here are packages of 50 or more files using the stopifnot() function:

pkg n 1 spatstat 252 2 practicema 145 3 QuACN 80 4 grid 74 5 spdep 61 6 lizard 54 7 surveillance 53 8 copula 50

These are packages with one or more files that have 100 or more stopifnot() calls:

pkg wire ct 1 ff ordermerge.R 278 2 OneArmPhaseTwoStudy zzz.R 142 3 bit64 integer64.R 137 4 updog rflexdog.R 124 5 RNetCDF RNetCDF.R 123 6 Rlda rlda.R 105 7 aster2 transform.R 105 8 ads fads.R 104 9 georob georob_exported_functions.R 10410 bit64 highlevel64.R 101

What a test!

**FIN**

If you are working on a transition to R 4.0.0 or have already upgraded to R 4.0.0, these and other new features are waiting for you! Send a comment with your favorite new feature (or better yet, a link to a blog post about it!).

As soon as I have time to dig up more nuggets, I will add more news to the series.

*** This is a syndicated blog from a network of security bloggers from Rud. was written by Hrbrmstr. The original message can be found at the following address: https://rud.is/b/2020/05/19/mining-r-4-0-0-changelog-for-nuggets-of-gold-1-stopifnot/.