ropensci / rnaturalearth

An R package to hold and facilitate interaction with natural earth map data :earth_africa:
http://ropensci.github.io/rnaturalearth/
Other
216 stars 23 forks source link

Extract out common check_scale function #6

Closed hadley closed 9 years ago

hadley commented 9 years ago

Something like this:

check_scale <- function(x) {
  if (is.numeric(x) && length(x) == 1) {
    if (x %in% c(110, 50, 10)) 
      return(x)
  } else if (is.character(x) && length(x) == 1) {
    x <- c(small = 110, medium = 50, large = 10)[tolower(x)]
    if (!is.na(x))
      return(unname(x))
  }

  stop("Invalid `scale`. Must be [110, 50, 10, 'small', 'medium', 'large']", 
    call. = FALSE)

}

check_scale(110)
check_scale("small")

I personal don't think you need to allow "110" as a valid input

andysouth commented 9 years ago

done 33229f148f7ed9d0316fca97004861b0f6f70f45