-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataBackendDplyr.R
320 lines (283 loc) · 10.5 KB
/
DataBackendDplyr.R
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#' @title DataBackend for dplyr/dbplyr
#'
#' @description
#' A [mlr3::DataBackend] using [dplyr::tbl()] from packages \CRANpkg{dplyr}/\CRANpkg{dbplyr}.
#' This includes [`tibbles`][tibble::tibble()] and abstract database connections interfaced by \CRANpkg{dbplyr}.
#' The latter allows [mlr3::Task]s to interface an out-of-memory database.
#'
#'
#' @param rows `integer()`\cr
#' Row indices.
#' @param cols `character()`\cr
#' Column names.
#' @param data_format (`character(1)`)\cr
#' Desired data format, e.g. `"data.table"` or `"Matrix"`.
#' @param na_rm `logical(1)`\cr
#' Whether to remove NAs or not.
#'
#' @template param_primary_key
#' @template param_strings_as_factors
#' @template param_connector
#'
#' @importFrom mlr3 DataBackend
#' @export
#' @examples
#' if (mlr3misc::require_namespaces(c("tibble", "RSQLite", "dbplyr"), quietly = TRUE)) {
#' # Backend using a in-memory tibble
#' data = tibble::as_tibble(iris)
#' data$Sepal.Length[1:30] = NA
#' data$row_id = 1:150
#' b = DataBackendDplyr$new(data, primary_key = "row_id")
#'
#' # Object supports all accessors of DataBackend
#' print(b)
#' b$nrow
#' b$ncol
#' b$colnames
#' b$data(rows = 100:101, cols = "Species")
#' b$distinct(b$rownames, "Species")
#'
#' # Classification task using this backend
#' task = mlr3::TaskClassif$new(id = "iris_tibble", backend = b, target = "Species")
#' print(task)
#' head(task)
#'
#' # Create a temporary SQLite database
#' con = DBI::dbConnect(RSQLite::SQLite(), ":memory:")
#' dplyr::copy_to(con, data)
#' tbl = dplyr::tbl(con, "data")
#'
#' # Define a backend on a subset of the database: do not use column "Sepal.Width"
#' tbl = dplyr::select_at(tbl, setdiff(colnames(tbl), "Sepal.Width"))
#' tbl = dplyr::filter(tbl, row_id %in% 1:120) # Use only first 120 rows
#' b = DataBackendDplyr$new(tbl, primary_key = "row_id")
#' print(b)
#'
#' # Query disinct values
#' b$distinct(b$rownames, "Species")
#'
#' # Query number of missing values
#' b$missings(b$rownames, b$colnames)
#'
#' # Note that SQLite does not support factors, column Species has been converted to character
#' lapply(b$head(), class)
#'
#' # Cleanup
#' rm(tbl)
#' DBI::dbDisconnect(con)
#' }
DataBackendDplyr = R6Class("DataBackendDplyr", inherit = DataBackend, cloneable = FALSE,
public = list(
#' @template field_levels
levels = NULL,
#' @template field_connector
connector = NULL,
#' @description
#'
#' Creates a backend for a [dplyr::tbl()] object.
#'
#' @param data ([dplyr::tbl()])\cr
#' The data object.
#'
#' Instead of calling the constructor yourself, you can call [mlr3::as_data_backend()]
#' on a [dplyr::tbl()].
#' Note that only objects of class `"tbl_lazy"` will be converted to a [DataBackendDplyr]
#' (this includes all connectors from \CRANpkg{dbplyr}).
#' Local `"tbl"` objects such as [`tibbles`][tibble::tibble()] will converted to a
#' [DataBackendDataTable][mlr3::DataBackendDataTable].
initialize = function(data, primary_key, strings_as_factors = TRUE, connector = NULL) {
loadNamespace("DBI")
loadNamespace("dbplyr")
if (!dplyr::is.tbl(data)) {
stop("Argument 'data' must be of class 'tbl'")
}
if (inherits(data, "tbl_sql")) {
requireNamespace("dbplyr")
}
super$initialize(data, primary_key)
assert_choice(primary_key, colnames(data))
if (isFALSE(strings_as_factors)) {
self$levels = list()
} else {
h = self$head(1L)
string_cols = setdiff(names(h)[map_lgl(h, is.character)], self$primary_key)
if (isTRUE(strings_as_factors)) {
strings_as_factors = string_cols
} else {
assert_subset(strings_as_factors, string_cols)
}
self$levels = self$distinct(rows = NULL, cols = strings_as_factors)
}
self$connector = assert_function(connector, args = character(), null.ok = TRUE)
},
#' @description
#' Finalizer which disconnects from the database.
#' This is called during garbage collection of the instance.
#' @return `logical(1)`, the return value of [DBI::dbDisconnect()].
finalize = function() {
if (isTRUE(self$valid)) {
DBI::dbDisconnect(private$.data$src$con)
}
},
#' @description
#' Returns a slice of the data.
#' Calls [dplyr::filter()] and [dplyr::select()] on the table and converts it to a [data.table::data.table()].
#'
#' The rows must be addressed as vector of primary key values, columns must be referred to via column names.
#' Queries for rows with no matching row id and queries for columns with no matching
#' column name are silently ignored.
#' Rows are guaranteed to be returned in the same order as `rows`, columns may be returned in an arbitrary order.
#' Duplicated row ids result in duplicated rows, duplicated column names lead to an exception.
data = function(rows, cols, data_format = "data.table") {
private$.reconnect()
rows = assert_integerish(rows, coerce = TRUE)
assert_names(cols, type = "unique")
assert_choice(data_format, self$data_formats)
cols = intersect(cols, colnames(private$.data))
res = setDT(dplyr::collect(dplyr::select_at(
dplyr::filter_at(private$.data, self$primary_key, dplyr::all_vars(. %in% rows)),
union(cols, self$primary_key))))
recode(res[list(rows), cols, nomatch = NULL, with = FALSE, on = self$primary_key],
self$levels)
},
#' @description
#' Retrieve the first `n` rows.
#'
#' @param n (`integer(1)`)\cr
#' Number of rows.
#'
#' @return [data.table::data.table()] of the first `n` rows.
head = function(n = 6L) {
private$.reconnect()
recode(setDT(dplyr::collect(head(private$.data, n))), self$levels)
},
#' @description
#' Returns a named list of vectors of distinct values for each column
#' specified. If `na_rm` is `TRUE`, missing values are removed from the
#' returned vectors of distinct values. Non-existing rows and columns are
#' silently ignored.
#'
#' @return Named `list()` of distinct values.
distinct = function(rows, cols, na_rm = TRUE) {
private$.reconnect()
# TODO: what does dplyr::disinct return for enums?
assert_names(cols, type = "unique")
cols = intersect(cols, self$colnames)
tbl = private$.data
if (!is.null(rows)) {
tbl = dplyr::filter_at(tbl, self$primary_key, dplyr::all_vars(. %in% rows))
}
get_distinct = function(col) {
x = dplyr::collect(dplyr::distinct(dplyr::select_at(tbl, col)))[[1L]]
if (is.factor(x)) {
x = as.character(x)
}
if (na_rm) {
x = x[!is.na(x)]
}
x
}
setNames(lapply(cols, get_distinct), cols)
},
#' @description
#' Returns the number of missing values per column in the specified slice
#' of data. Non-existing rows and columns are silently ignored.
#'
#' @return Total of missing values per column (named `numeric()`).
missings = function(rows, cols) {
private$.reconnect()
rows = assert_integerish(rows, coerce = TRUE)
assert_names(cols, type = "unique")
cols = intersect(cols, self$colnames)
if (length(cols) == 0L) {
return(setNames(integer(0L), character(0L)))
}
res = dplyr::collect(dplyr::summarize_at(
dplyr::filter_at(private$.data, self$primary_key, dplyr::all_vars(. %in% rows)),
cols, list(~ sum(is.na(.), na.rm = TRUE))))
if (nrow(res) == 0L) {
return(setNames(integer(length(cols)), cols))
}
unlist(res, recursive = FALSE)
}
),
active = list(
#' @field rownames (`integer()`)\cr
#' Returns vector of all distinct row identifiers, i.e. the contents of the primary key column.
rownames = function() {
private$.reconnect()
dplyr::collect(dplyr::select_at(private$.data, self$primary_key))[[1L]]
},
#' @field colnames (`character()`)\cr
#' Returns vector of all column names, including the primary key column.
colnames = function() {
private$.reconnect()
colnames(private$.data)
},
#' @field nrow (`integer(1)`)\cr
#' Number of rows (observations).
nrow = function() {
private$.reconnect()
dplyr::collect(dplyr::tally(private$.data))[[1L]]
},
#' @field ncol (`integer(1)`)\cr
#' Number of columns (variables), including the primary key column.
ncol = function() {
private$.reconnect()
ncol(private$.data)
},
#' @field valid (`logical(1)`)\cr
#' Returns `NA` if the data does not inherits from `"tbl_sql"` (i.e., it is not a real SQL data base).
#' Returns the result of [DBI::dbIsValid()] otherwise.
valid = function() {
if (!inherits(private$.data, "tbl_sql")) {
return(NA)
}
loadNamespace("DBI")
loadNamespace("dbplyr")
# workaround for https://github.com/r-dbi/DBI/issues/302
force(names(private$.data$src$con))
DBI::dbIsValid(private$.data$src$con)
}
),
private = list(
.calculate_hash = function() {
private$.reconnect()
calculate_hash(private$.data)
},
.reconnect = function() {
if (isFALSE(self$valid)) {
if (is.null(self$connector)) {
stop("Invalid connection. Provide a connector during construction to automatically reconnect", call. = FALSE)
}
con = self$connector()
if (!all(class(private$.data$src$con) == class(con))) {
stop(sprintf("Reconnecting failed. Expected a connection of class %s, but got %s",
paste0(class(private$.data$src$con), collapse = "/"), paste0(class(con), collapse = "/")), call. = FALSE)
}
private$.data$src$con = con
}
}
)
)
#' @importFrom mlr3 as_data_backend
#' @export
as_data_backend.tbl_SQLiteConnection = function(data, primary_key, strings_as_factors = TRUE, ...) { # nolint
b = DataBackendDplyr$new(data, primary_key)
path = data$src$con@dbname
if (!identical(path, ":memory:") && test_string(path) && file.exists(path)) {
b$connector = sqlite_reconnector(path)
}
return(b)
}
#' @importFrom mlr3 as_data_backend
#' @export
as_data_backend.tbl_lazy = function(data, primary_key, strings_as_factors = TRUE, ...) { # nolint
DataBackendDplyr$new(data, primary_key)
}
#' @rawNamespace if (getRversion() >= "3.6.0") S3method(dplyr::show_query, DataBackendDplyr)
show_query.DataBackendDplyr = function(x, ...) { # nolint
requireNamespace("dplyr")
requireNamespace("dbplyr")
dplyr::show_query(x$.__enclos_env__$private$.data)
}