-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathberlin.R
More file actions
469 lines (369 loc) · 9.04 KB
/
Copy pathberlin.R
File metadata and controls
469 lines (369 loc) · 9.04 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# Demo for presentation at the 16th Berlin R meetup
# {dm} facilitates working with multiple tables
options(tibble.print_min = 6)
options(tibble.print_max = 6)
##
##
##
## Why?
## --------------------------------------------------------------------
##
##
##
library(nycflights13)
# Example dataset: tables linked with each other
?flights
?airports
?airlines
?planes
?weather
library(tidyverse)
flights_base <-
flights %>%
select(year, month, day, carrier, tailnum, origin, dest)
flights_base
# carrier column also present in `airlines`, this table contains
# additional information
airlines
flights_base %>%
left_join(airlines)
# single source of truth: updating in one single location
airlines[airlines$carrier == "UA", "name"] <- "United broke my guitar"
# ...propagates to related records
flights_base %>%
left_join(airlines)
# Same for airplanes?
planes
flights_base %>%
left_join(planes)
flights_base %>%
left_join(planes) %>%
count(is.na(type))
# Take a closer look at the join
flights_base %>%
left_join(planes, by = "tailnum")
# Same for airports?
airports
try(
flights_base %>%
left_join(airports)
)
# Need to specify join variables!
flights_base %>%
left_join(airports, by = c("origin" = "faa"))
# cleanup
rm(airlines)
##
##
##
## Keys
## --------------------------------------------------------------------
##
##
##
# Row identifiers
t1 <- tibble(a = 1, b = letters[1:3])
t1
t2 <- tibble(a = 1, c = 1:2)
t2
# What happens here?
left_join(t1, t2)
# When joining, the column(s) must be unique in at least one
# participating table!
# Ensure uniqueness:
airlines %>%
count(carrier)
airlines %>%
count(carrier) %>%
count(n)
planes %>%
count(tailnum) %>%
count(n)
# dm shortcut:
planes %>%
dm::check_key(tailnum)
try(
planes %>%
dm::check_key(engines)
)
airports %>%
dm::check_key(faa)
# FIXME: add dm function that explains why not key candidate
# Why is name not a key candidate for airports?
try(
airports %>%
dm::check_key(name)
)
airports %>%
add_count(name) %>%
filter(n > 1) %>%
arrange(name)
# Cleanup
rm(t1, t2)
##
##
##
## Data model
## --------------------------------------------------------------------
##
##
##
library(dm)
# Compound object: tables, relationships, data
dm_nycflights13(cycle = TRUE)
dm_nycflights13(cycle = TRUE) %>%
dm_draw()
# Selection of tables
dm_nycflights13(cycle = TRUE) %>%
dm_select_tbl(flights, airlines) %>%
dm_draw()
dm_nycflights13(cycle = TRUE) %>%
dm_select_tbl(airports, airlines) %>%
dm_draw()
try(
dm_nycflights13() %>%
dm_select_tbl(bogus)
)
# Accessing tables
dm_nycflights13() %>%
tbl("airlines")
# Table names
dm_nycflights13() %>%
src_tbls()
# NB: [, $, [[ and names() also work
# Analogy: parallel vectors in the global environment
x <- 1:5
y <- x + 1
z <- x * y
w <- map(z, ~ runif(.))
tibble(x, y, z, w)
tibble(x = 1:5) %>%
mutate(y = x + 1) %>%
mutate(z = x * y) %>%
mutate(w = map(z, ~ runif(.)))
##
##
##
## Filtering for data models
## --------------------------------------------------------------------
##
##
##
dm_nycflights13()
# Filtering on a table returns a dm object with the filter condition(s) stored
(dm_nyc_filtered <-
dm_nycflights13() %>%
dm_filter(airlines, carrier == "AA"))
# Apply all filters and retrieve an "updated" `dm`
dm_nyc_filtered %>%
dm_apply_filters()
# If a filter condition is phrased wrongly it will only fail, once the filter is being applied
(dm_nyc_fail <-
dm_nycflights13() %>%
dm_filter(airports, origin == "EWR"))
try(
tbl(dm_nyc_fail, "flights")
)
# Mind: when accessing table from a `dm` (using one of: `tbl()`, `[[.dm()`, `$.dm()`),
# only the necessary filter conditions are applied:
tbl(dm_nyc_fail, "weather")
dm_nycflights13() %>%
dm_filter(flights, origin == "EWR") %>%
dm_apply_filters()
# ... which then can be filtered on another table
dm_nycflights13() %>%
dm_filter(airlines, name == "American Airlines Inc.") %>%
dm_filter(airports, name != "John F Kennedy Intl") %>%
dm_apply_filters()
aa_non_jfk_january <-
dm_nycflights13() %>%
dm_filter(airlines, name == "American Airlines Inc.") %>%
dm_filter(airports, name != "John F Kennedy Intl") %>%
dm_filter(flights, month == 1) %>%
dm_apply_filters()
aa_non_jfk_january
# ... and processed further
aa_non_jfk_january %>%
tbl("planes")
##
##
##
## Copy to database
## --------------------------------------------------------------------
##
##
##
# FIXME: SQLite with many rows
# FIXME: Make work with planes table
# FIXME: remove all_connected = TRUE for now, redo example above
# All operations are designed to work locally and on the database
nycflights13_sqlite <-
dm_nycflights13() %>%
dm_select_tbl(-planes) %>%
dm_filter(flights, month == 1) %>%
dm_apply_filters() %>%
copy_dm_to(dbplyr::src_memdb(), ., unique_table_names = TRUE)
nycflights13_sqlite
nycflights13_sqlite %>%
dm_draw()
nycflights13_sqlite %>%
dm_get_tables() %>%
map(dbplyr::sql_render)
# Filtering on the database
nycflights13_sqlite %>%
dm_filter(airlines, name == "American Airlines Inc.") %>%
dm_filter(airports, name != "John F Kennedy Intl") %>%
dm_filter(flights, day == 1) %>%
tbl("flights")
# ... and the corresponding SQL statement
nycflights13_sqlite %>%
dm_filter(airlines, name == "American Airlines Inc.") %>%
dm_filter(airports, name != "John F Kennedy Intl") %>%
dm_filter(flights, day == 1) %>%
tbl("flights") %>%
dbplyr::sql_render()
##
##
##
## Joining tables
## --------------------------------------------------------------------
##
##
##
dm_nycflights13() %>%
dm_join_to_tbl(airlines, flights, join = left_join)
dm_nycflights13() %>%
dm_join_to_tbl(flights, airlines, join = left_join)
nycflights13_sqlite %>%
dm_join_to_tbl(airlines, flights, join = left_join)
aa_non_jfk_january %>%
dm_join_to_tbl(flights, airlines, join = left_join)
# FIXME: Multi-joins
try(
dm_nycflights13() %>%
dm_join_to_tbl(airports, airlines, join = left_join)
)
try(
dm_nycflights13() %>%
dm_join_to_tbl(flights, airports, airlines, join = left_join)
)
##
##
##
## Build up data model from scratch
## --------------------------------------------------------------------
##
##
##
# Linking the weather table
# Determine key candidates
weather %>%
enum_pk_candidates()
weather %>%
enum_pk_candidates() %>%
count(candidate)
# It's tricky:
weather %>%
unite("slot_id", origin, year, month, day, hour, remove = FALSE) %>%
count(slot_id) %>%
filter(n > 1)
weather %>%
count(origin, time_hour) %>%
filter(n > 1)
weather %>%
count(origin, format(time_hour)) %>%
filter(n > 1)
# This looks like a good candidate:
weather %>%
count(origin, format(time_hour, tz = "UTC")) %>%
filter(n > 1)
# FIXME: Support compound keys (#3)
# Currently, we need to create surrogate keys:
weather_link <-
weather %>%
mutate(time_hour_fmt = format(time_hour, tz = "UTC")) %>%
unite("origin_slot_id", origin, time_hour_fmt, remove = FALSE)
flights_link <-
flights %>%
mutate(time_hour_fmt = format(time_hour, tz = "UTC")) %>%
unite("origin_slot_id", origin, time_hour_fmt, remove = FALSE)
# one option to create a `dm` is to use `as_dm()`:
nycflights13_dm <- as_dm(list(
airlines = airlines,
airports = airports,
flights = flights_link,
planes = planes,
weather = weather_link
))
# Copy to this environment
airlines_global <- airlines
airports_global <- airports
planes_global <- planes
global <-
dm_from_src(src_df(env = .GlobalEnv))
global
global %>%
dm_rename_tbl(
airlines = airlines_global,
airports = airports_global,
planes = planes_global,
flights = flights_link,
weather = weather_link
) %>%
dm_select_tbl(airlines, airports, planes, flights, weather)
# or better:
nycflights13_tbl <-
global %>%
dm_select_tbl(
airlines = airlines_global,
airports = airports_global,
planes = planes_global,
flights = flights_link,
weather = weather_link
)
nycflights13_tbl
nycflights13_tbl %>%
dm_draw()
# Adding primary keys
nycflights13_pk <-
nycflights13_tbl %>%
dm_add_pk(weather, origin_slot_id) %>%
dm_add_pk(planes, tailnum) %>%
dm_add_pk(airports, faa) %>%
dm_add_pk(airlines, carrier)
nycflights13_pk %>%
dm_draw()
# FIXME: Model weak constraints, show differently in diagram (#4)
# Adding foreign keys
nycflights13_fk <-
nycflights13_pk %>%
dm_add_fk(flights, origin_slot_id, weather, check = FALSE) %>%
dm_add_fk(flights, tailnum, planes, check = FALSE) %>%
dm_add_fk(flights, origin, airports) %>%
dm_add_fk(flights, dest, airports, check = FALSE) %>%
dm_add_fk(flights, carrier, airlines)
nycflights13_fk %>%
dm_draw()
# Color it!
dm_get_available_colors()
nycflights13_fk %>%
dm_set_colors(airlines = , planes = , weather = , airports = "blue") %>%
dm_draw()
##
##
##
## Import a dm from a database, including key constraints
## --------------------------------------------------------------------
##
##
##
try({
# Import
dm_pq <-
dm_nycflights13() %>%
dm_select_tbl(-planes) %>%
dm_filter(flights, month == 1) %>%
copy_dm_to(src_postgres(), ., temporary = FALSE)
dm_from_pq <-
dm_learn_from_db(src_postgres())
})