Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error handling for guess_max #590

Merged
merged 4 commits into from
Feb 9, 2017
Merged

Conversation

jimhester
Copy link
Collaborator

Fixes #588

I added another example showing how to read a dataset as character and using type_convert() to infer types based on the full dataset as well.

R/col_types.R Outdated
guess_types <- function(datasource, tokenizer, locale, n = 1000) {
guess_types_(datasource, tokenizer, locale, n = n)
guess_types <- function(datasource, tokenizer, locale, guess_max = 1000, max_limit = .Machine$integer.max %/% 100) {
if (is.na(guess_max) || guess_max < 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should check it's a numeric of length 1 first

And then you probably should pull out into check_guess_max()

#'
#' # Type convert can be used to infer types from an entire dataset
#' type_convert(
#' read_csv(readr_example("mtcars.csv"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indenting is mildly wack

@jimhester
Copy link
Collaborator Author

Ok addressed comments at 5f848cc

R/col_types.R Outdated
guess_types_(datasource, tokenizer, locale, n = n)

check_guess_max <- function(guess_max, max_limit = .Machine$integer.max %/% 100) {
if (!(length(guess_max) == 1 && is.numeric(guess_max) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic scares me a little

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it is a bit confusing, any suggestions on a clearer way to express this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two options:

stopifnot(is.numeric(guess_max), length(guess_max) == 1)

Or De Morgan's the whole thing, which I think gives you

if (length(guess_max) != 1 || !is.numeric(guess_max) ||
    !is_integerish(guess_max)) || is.na(guess_max) || guess_max <= 0) {

Copy link
Collaborator Author

@jimhester jimhester Feb 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this, the assert definition is slightly complicated because we want to evaluate the ... lazily.

assert <- function(msg, ...) {
  dots <- eval(substitute(alist(...)))
  for (x in dots) {
    if (!isTRUE(eval(x, parent.frame()))) {
      stop(msg, call. = FALSE)
    }
  }
  invisible(TRUE)
}

assert("`guess_max` must be a positive integer",
  length(guess_max) == 1, is.numeric(guess_max), is_integerish(guess_max),
  !is.na(guess_max), guess_max > 0)

The assert definition is basically identical to what is in assertthat, not sure if this makes me feel good or bad 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmmm, I think we can just stick to an if statement instead of introducing a new syntactic form 😉

@jimhester jimhester merged commit 8e7febc into tidyverse:master Feb 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants