Does R have an assert statement as in python?

RAssertLanguage DesignAssertions

R Problem Overview


a statement that checks if something is true and if not prints a given error message and exits

R Solutions


Solution 1 - R

stopifnot()

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

Solution 2 - R

@Nick:

You can control your error message if you write a function with a descriptive name to test the condition that will throw an error in your program. Here's 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 through 8, then print a message that says

Error: Less_Than_8(i) is not TRUE

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

If you need anything fancier than that, look into Runit and testthat as Harlan suggested.

Solution 3 - R

This can be achieved with the stop command. This command will halt the execution of a function and print the error message. For example, we can test if the variable something is FALSE:

if(something == FALSE){
   stop("error message to print")   
}

Similarly, the warning command will print a warning (but continue executing the code).

if(something == FALSE){
   warning("error message to print")   
}

These are both provided by base R and require no packages to run or include in writing your own functions. I prefer this approach to write code with fewer dependancies and this syntax is widely used in package development. However, similar functionality is supported by the "assertthat" package with the assert_that function that has recently been released as part of Hadley's "tidyverse".

Solution 4 - R

The testit-package

The testit-package provides the assert-function as a simple solution (https://github.com/yihui/testit)

Examples:

assert('one equals one', 1==1)
assert('seq and : produce equal sequences', seq(1L, 10L) == 1L:10L)
assert('seq and : produce identical sequences', identical(seq(1L, 10L), 1L:10L))

# multiple tests
T=FALSE; F=TRUE
assert('T is bad for TRUE, and so is F for FALSE', T!=TRUE, F!=FALSE)

# a mixture of tests
assert("Let's pray all of them will pass", 1==1, 1!=2, letters[4]=='d', rev(rev(letters))==letters)

Source: https://github.com/yihui/testit/blob/master/R/testit.R

Solution 5 - R

You could use the checkmate-package for that. From one of their examples:

require(checkmate)

fact <- function(n, method = "stirling") {
  assertCount(n)
  assertChoice(method, c("stirling", "factorial"))

  if (method == "factorial")
    factorial(n)
  else
    sqrt(2 * pi * n) * (n / exp(1))^n
}

fact(2, method = 'blub')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanView Question on Stackoverflow
Solution 1 - RHarlanView Answer on Stackoverflow
Solution 2 - RCCCView Answer on Stackoverflow
Solution 3 - RTom KellyView Answer on Stackoverflow
Solution 4 - RRasmus LarsenView Answer on Stackoverflow
Solution 5 - RandscharView Answer on Stackoverflow