I am writing an R function, and I want to make sure that the argument of my R function is of a certain class (eg, \"matrix\").
What is the best way to do this?
S
You can either check that it's a matrix with is.matrix or else convert it with as.matrix after the parameter is passed:
foo <- function(x) { if(!is.matrix(x)) stop("x must be a matrix") # I want to make sure x is of type "matrix" solve(x) }