File: checkDataTable.R

package info (click to toggle)
r-cran-checkmate 2.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,512 kB
  • sloc: ansic: 2,211; sh: 9; makefile: 8
file content (84 lines) | stat: -rw-r--r-- 2,749 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#' Check if an argument is a data table
#'
#' @templateVar fn DataTable
#' @template x
#' @inheritParams checkMatrix
#' @inheritParams checkList
#' @inheritParams checkDataFrame
#' @param key [\code{character}]\cr
#'   Expected primary key(s) of the data table.
#' @param index [\code{character}]\cr
#'   Expected secondary key(s) of the data table.
#' @template null.ok
#' @template checker
#' @family compound
#' @export
#' @examples
#' library(data.table)
#' dt = as.data.table(iris)
#' setkeyv(dt, "Species")
#' setkeyv(dt, "Sepal.Length", physical = FALSE)
#' testDataTable(dt)
#' testDataTable(dt, key = "Species", index = "Sepal.Length", any.missing = FALSE)
checkDataTable = function(x, key = NULL, index = NULL, types = character(0L), any.missing = TRUE, all.missing = TRUE, min.rows = NULL, max.rows = NULL, min.cols = NULL, max.cols = NULL, nrows = NULL, ncols = NULL, row.names = NULL, col.names = NULL, null.ok = FALSE) {
  if (!requireNamespace("data.table", quietly = TRUE))
    stop("Install package 'data.table' to perform checks of data tables")

  qassert(null.ok, "B1")
  if (is.null(x)) {
    if (null.ok)
      return(TRUE)
    return("Must be a data.table, not 'NULL'")
  }

  if (!data.table::is.data.table(x)) {
    return(paste0("Must be a data.table", if (null.ok) " (or 'NULL')" else "", sprintf(", not %s", guessType(x))))
  }

  checkDataFrame(x, types, any.missing, all.missing, min.rows, max.rows, min.cols, max.cols, nrows, ncols, row.names, col.names, null.ok = FALSE) %and%
    checkDataTableProps(x, key, index)
}

checkDataTableProps = function(x, key = NULL, index = NULL) {
  if (!is.null(key)) {
    qassert(key, "S")
    if (!setequal(data.table::key(x) %??% character(0L), key))
      return(sprintf("Must have primary keys: %s", paste0(key, collapse = ",")))
  }
  if (!is.null(index)) {
    qassert(index, "S")
    indices = strsplit(data.table::indices(x) %??% "", "__", fixed = TRUE)[[1L]]
    if (!setequal(indices, index))
      return(sprintf("Must have secondary keys (indices): %s", paste0(index, collapse = ",")))
  }
  return(TRUE)
}

#' @export
#' @rdname checkDataTable
check_data_table = checkDataTable

#' @export
#' @include makeAssertion.R
#' @template assert
#' @rdname checkDataTable
assertDataTable = makeAssertionFunction(checkDataTable, use.namespace = FALSE)

#' @export
#' @rdname checkDataTable
assert_data_table = assertDataTable

#' @export
#' @include makeTest.R
#' @rdname checkDataTable
testDataTable = makeTestFunction(checkDataTable)

#' @export
#' @rdname checkDataTable
test_data_table = testDataTable

#' @export
#' @include makeExpectation.R
#' @template expect
#' @rdname checkDataTable
expect_data_table = makeExpectationFunction(checkDataTable, use.namespace = FALSE)