Skip to content

Instantly share code, notes, and snippets.

@jimhester
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save jimhester/a48b250d3a0a7e699d86 to your computer and use it in GitHub Desktop.

Select an option

Save jimhester/a48b250d3a0a7e699d86 to your computer and use it in GitHub Desktop.
More informative error messages for R
# missing variable
`$` <- function(x, y) { var = deparse(substitute(y)); if(!deparse(substitute(y)) %in% names(x)) { stop('missing variable: ', var) }; x[[var]] }
#> prob1<-as.data.frame(cbind(c(1,2,3),c(5,4,3)))
#> colnames(prob1)<-c("Education","Ethnicity")
#> table(prob1$Eduction, prob1$Ethnicity)
# Error in prob1$Eduction : wrong name: Eduction
# error when nrow is used on non data.frames or matrices
nrow <- function(x) { if (is.data.frame(x) || is.matrix(x)) { dim(x)[1L] } else { stop("nrow cannot be used on ", type(x)) } }
#> nrow(prob1$Education)
# Error in nrow(prob1$Education) : nrow cannot be used on double
@jimhester

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment