addition <- function(x, y){
if(!is_numeric(c(x, y))) stop("One of your inputs is not a number.")
x + y
}
addition(3, "abc")
Error in is_numeric(c(x, y)): could not find function "is_numeric"
Lecture 15
Cornell University
INFO 5001 - Fall 2024
October 22, 2024
Image credit: The Computer History Museum
An error, flaw, failure or fault in a computer program or system that causes it to produce an incorrect or unexpected result, or to behave in unintended ways.
Image credit: @allison_horst
An error, flaw, failure or fault in a computer program or system that causes it to produce an incorrect or unexpected result, or to behave in unintended ways.
Image credit: @allison_horst
reprex
reprex::reprex()
Easier to talk about code that:
```r
# add code here
```
reprex()
session_info = TRUE
f <- function(a) g(a)
g <- function(b) h(b)
h <- function(c) i(c)
i <- function(d) {
if (!is.numeric(d)) {
stop("`d` must be numeric", call. = FALSE)
}
d + 10
}
f("a")
Error: `d` must be numeric
5: stop("`d` must be numeric", call. = FALSE) at #3
4: i(c) at #1
3: h(b) at #1
2: g(a) at #1
1: f("a")
Expected to use the “plasma” color palette from viridis. What went wrong?
Source: Andrew Heiss
safely()
safely()
result
error
safely()
List of 2
$ result: num 3
$ error : NULL
List of 2
$ result: NULL
$ error :List of 2
..$ message: chr "non-numeric argument to mathematical function"
..$ call : language .Primitive("sqrt")(x)
..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
safely()
and map()
Error in `map()`:
ℹ In index: 1.
Caused by error:
! non-numeric argument to mathematical function
List of 3
$ :List of 2
..$ result: NULL
..$ error :List of 2
.. ..$ message: chr "non-numeric argument to mathematical function"
.. ..$ call : language .Primitive("sqrt")(x)
.. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
$ :List of 2
..$ result: num 2
..$ error : NULL
$ :List of 2
..$ result: num 2.24
..$ error : NULL
transpose()
List of 2
$ result:List of 3
..$ : NULL
..$ : num 2
..$ : num 2.24
$ error :List of 3
..$ :List of 2
.. ..$ message: chr "non-numeric argument to mathematical function"
.. ..$ call : language .Primitive("sqrt")(x)
.. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
..$ : NULL
..$ : NULL
ae-13
ae-13
(repo name will be suffixed with your GitHub name).renv::restore()
to install the required packages, open the Quarto document in the repo, and follow along and complete the exercises.