Does R have an assert statement like in python?

an instruction that checks that something is true and if it does not print this error message and exits

+96
assert r language-design assertions
Feb 10 2018-10-10T00 00Z
source share
2 answers

stopifnot()

You may also be interested in packages like Runit and testthat for unit testing.

+121
Feb 10 2018-10-10T00
source share

@Nick:

You can control your error message if you write a function with a descriptive name to check for a condition that will cause an error in your program. Here is an example:

 Less_Than_8 = function(x) return(x < 8) for (i in 1:10) { print(i) stopifnot(Less_Than_8(i)) } 

This will print the numbers 1 to 8, and then print a message saying

 Error: Less_Than_8(i) is not TRUE 

It would be nice if the "i" in parentheses was replaced with a value that did not pass the test, but you get what you pay for.

If you need something more interesting, look at Runit and testthat, as Harlan suggested.

+12
Aug 25 2018-11-22T00:
source share



All Articles