Skip to content

Assert column types #3

Open
Open

Description

@FlukeAndFeather

It would be great to assert column types as well as names. E.g. something like

assertable::assert_coltypes(data, c(a = numeric(0), b = character(0)))

Activity

  1. gnguy commented on Sep 11, 2019

    @gnguy
    Owner

    Thanks for the suggestion -- to have this be as flexible as possible for different conditions, I could see this looking something like this:

    data <- data.table(a = c(1:3), b = c("a", "b", "c"))
    assert_coltypes(data, list(a = is.numeric, b = is.character))
    
    assert_coltypes <- function(data, col_list) {
      message_list <- c()
      for(col_name in names(col_list)) {
        col_function<- col_list[[col_name]]
        if(!col_function(data[[col_name]])) {
          message_list <- c(message_list, paste(col_name, "failed", deparse(substitute(col_function)), "assertion"))
         }
      }
      if(length(message_list) > 0) stop(paste(message_list, collapse = ", "))
    }
    

    The display of the functions on the failure condition isn't ideal, so it would require some work to figure out how to make that part a bit cleaner. But otherwise seems feasible.

  2. gnguy commented on Sep 11, 2019

    @gnguy
    Owner

    I'm a bit tight for time in the upcoming weeks -- if you have time and ability to add something based on the above, feel free to go for it. Otherwise I can take a stab in a month or so.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions