--5-Robust functions
#1-Robust functions
​Robust functions video​
# Three main problems:
# type unstable functions
# non standard evaluation
# hidden argument#2-An error is better than a surprise
# Add a call to stopifnot() to both_na() that checks arguments x and
# y have the same length.
# Run the call to both_na() to verify it returns an error.
# Define troublesome x and y
x <- c(NA, NA, NA)
y <- c( 1, NA, NA, NA)
both_na <- function(x, y) {
# Add stopifnot() to check length of x and y
stopifnot(length(x)==length(y))
sum(is.na(x) & is.na(y))
}
# Call both_na() on x and y
both_na(x, y)#3-An informative error is even better
#4-A different kind of surprise: side effects -X
#5-Unstable types
#6-sapply is another common culprit
#7-Using purrr solves the problem -X
#8-A type consistent solution -X
#9-Or fail early if something goes wrong
#10-Non-standard evaluation
#11-Programming with NSE functions
#12-When things go wrong
#13-What to do?
Last updated