Imagine a function that causes stop:

f <- function(x) {
  if (x == 42) stop("x cannot be 42, x is ", x, " instead")
  x
}

Imagine a process that calls that function:

xs <- c(1, 2, 4, 8, 16, 32, 42, 64)
for (x in xs) {
  print(f(x))
}

Running this code fill stop the program. Here is to check for this:

xs <- c(1, 2, 4, 8, 16, 32, 42, 64)
for (x in xs) {
  tryCatch(
    print(f(x)),
    error = function(e) print(e)
  )
}


richelbilderbeek/ribir documentation built on March 19, 2021, 3:55 a.m.