-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathopspec.R
799 lines (752 loc) · 29 KB
/
opspec.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#' @importFrom methods setGeneric
NULL
#' Create a row records to block records transform specification.
#'
#' Create a row records to block records transform specification object that holds the pivot control table, specification of
#' extra row keys, and control table keys.
#'
#' @param controlTable an all character data frame or cdata pivot control.
#' @param ... not used, force later arguments to bind by name.
#' @param recordKeys vector of columns identifying records.
#' @param controlTableKeys vector of keying columns of the controlTable.
#' @param checkNames passed to rowrecs_to_blocks.
#' @param checkKeys passed to rowrecs_to_blocks.
#' @param strict passed to rowrecs_to_blocks.
#' @param allow_rqdatatable logical, if TRUE allow rqdatatable shortcutting on simple conversions.
#' @return a record specification object
#'
#' @examples
#'
#' d <- wrapr::build_frame(
#' "id" , "AUC", "R2" |
#' 1 , 0.7 , 0.4 |
#' 2 , 0.8 , 0.5 )
#'
#' transform <- rowrecs_to_blocks_spec(
#' wrapr::qchar_frame(
#' "measure", "value" |
#' "AUC" , AUC |
#' "R2" , R2 ),
#' recordKeys = "id")
#'
#' print(transform)
#'
#' d %.>% transform
#'
#' inv_transform <- t(transform)
#' print(inv_transform)
#'
#' # identity (in structure)
#' d %.>% transform %.>% inv_transform
#'
#' # identity again (using .() "immediate" notation)
#' d %.>% transform %.>% .(t(transform))
#'
#' @export
#'
rowrecs_to_blocks_spec <- function(controlTable,
...,
recordKeys = character(0),
controlTableKeys = colnames(controlTable)[[1]],
checkNames = TRUE,
checkKeys = FALSE,
strict = FALSE,
allow_rqdatatable = FALSE) {
wrapr::stop_if_dot_args(substitute(list(...)), "cdata::rowrecs_to_blocks_spec")
controlTable <- as.data.frame(controlTable)
rownames(controlTable) <- NULL
ck <- checkControlTable(controlTable = controlTable, controlTableKeys = controlTableKeys, strict = FALSE)
if(!is.null(ck)) {
stop(paste("cdata::rowrecs_to_blocks_spec", ck))
}
if(length(intersect(recordKeys, colnames(controlTable)))>0) {
stop("cdata::rowrecs_to_blocks_spec recordKeys intersected control table columns")
}
r <- list(controlTable = controlTable,
recordKeys = recordKeys,
controlTableKeys = controlTableKeys,
checkNames = checkNames,
checkKeys = checkKeys,
strict = strict,
allow_rqdatatable = allow_rqdatatable)
class(r) <- "rowrecs_to_blocks_spec"
r
}
#' Create a block records to row records transform specification.
#'
#' Create a block records to row records transform specification object that holds the pivot control table, specification of
#' extra row keys, and control table keys.
#'
#' @param controlTable an all character data frame or cdata pivot control.
#' @param ... not used, force later arguments to bind by name.
#' @param recordKeys vector of columns identifying records.
#' @param controlTableKeys vector of keying columns of the controlTable.
#' @param checkNames passed to blocks_to_rowrecs.
#' @param checkKeys passed to blocks_to_rowrecs.
#' @param strict passed to blocks_to_rowrecs.
#' @param allow_rqdatatable logical, if TRUE allow rqdatatable shortcutting on simple conversions.
#' @return a record specification object
#'
#' @examples
#'
#' d <- wrapr::build_frame(
#' "id", "measure", "value" |
#' 1 , "AUC" , 0.7 |
#' 1 , "R2" , 0.4 |
#' 2 , "AUC" , 0.8 |
#' 2 , "R2" , 0.5 )
#'
#' transform <- blocks_to_rowrecs_spec(
#' wrapr::qchar_frame(
#' "measure", "value" |
#' "AUC" , AUC |
#' "R2" , R2 ),
#' recordKeys = "id")
#'
#' print(transform)
#'
#' d %.>% transform
#'
#' inv_transform <- t(transform)
#' print(inv_transform)
#'
#' # identity (in structure)
#' d %.>% transform %.>% inv_transform
#'
#' # identity again (using .() "immediate" notation)
#' d %.>% transform %.>% .(t(transform))
#'
#'
#' @export
#'
blocks_to_rowrecs_spec <- function(controlTable,
...,
recordKeys = character(0),
controlTableKeys = colnames(controlTable)[[1]],
checkNames = TRUE,
checkKeys = TRUE,
strict = FALSE,
allow_rqdatatable = FALSE) {
wrapr::stop_if_dot_args(substitute(list(...)), "cdata::blocks_to_rowrecs_spec")
controlTable <- as.data.frame(controlTable)
rownames(controlTable) <- NULL
ck <- checkControlTable(controlTable = controlTable, controlTableKeys = controlTableKeys, strict = FALSE)
if(!is.null(ck)) {
stop(paste("cdata::blocks_to_rowrecs_spec", ck))
}
if(length(intersect(recordKeys, colnames(controlTable)))>0) {
stop("cdata::blocks_to_rowrecs_spec recordKeys intersected control table columns")
}
r <- list(controlTable = controlTable,
recordKeys = recordKeys,
controlTableKeys = controlTableKeys,
checkNames = checkNames,
checkKeys = checkKeys,
strict = strict,
allow_rqdatatable = allow_rqdatatable)
class(r) <- "blocks_to_rowrecs_spec"
r
}
#' Upack details of a cdata record transform.
#'
#' Unpack details, especially: generate data frames representing both sides of a transform.
#'
#' @param x blocks_to_rowrecs_spec or rowrecs_to_blocks_spec
#' @return detailed fields
#'
#' @export
#' @keywords internal
#'
get_transform_details <- function(x) {
controlTable = x$controlTable
controlTableKeys = x$controlTableKeys
recordKeys = x$recordKeys
# build a of both sides of transform
kf <- data.frame(x = 1)
kf$x <- NULL
for(cn in recordKeys) {
kf[[cn]] <- "."
}
row <- blocks_to_rowrecs(controlTable,
keyColumns = character(0),
controlTable = controlTable,
controlTableKeys = controlTableKeys)
row <- cbind(kf, row)
block <- cbind(kf, controlTable)
args <- x[setdiff(names(x), c("controlTable", "recordKeys", "controlTableKeys"))]
list(block_record = block,
block_keys = c(recordKeys, controlTableKeys),
row_record = row,
row_keys = recordKeys,
args = args,
class = class(x))
}
#' @export
#'
format.rowrecs_to_blocks_spec <- function(x, ...) {
sides <- get_transform_details(x)
row_record <- sides$row_record
block_record <- sides$block_record
row_str <- wrapr::draw_framec(row_record, unquote_cols = colnames(row_record))
block_str <- wrapr::draw_framec(block_record, unquote_cols = setdiff(colnames(block_record), x$controlTableKeys))
args <- x[setdiff(names(x), c("controlTable", "recordKeys", "controlTableKeys"))]
fmt_str <- paste0("{\n ",
row_str,
" row_keys <- ", wrapr::map_to_char(x$recordKeys),
"\n\n # becomes\n\n " ,
block_str,
" block_keys <- ", wrapr::map_to_char(c(x$recordKeys, x$controlTableKeys)),
"\n\n # args: ", gsub("['\"]+", "", wrapr::map_to_char(args)),
"\n}\n\n")
fmt_str
}
#' @export
#'
print.rowrecs_to_blocks_spec <- function(x, ...) {
fmt_str <- format(x)
cat(fmt_str)
invisible(fmt_str)
}
#' @export
#'
format.blocks_to_rowrecs_spec <- function(x, ...) {
sides <- get_transform_details(x)
row_record <- sides$row_record
block_record <- sides$block_record
row_str <- wrapr::draw_framec(row_record, unquote_cols = colnames(row_record))
block_str <- wrapr::draw_framec(block_record, unquote_cols = setdiff(colnames(block_record), x$controlTableKeys))
args <- x[setdiff(names(x), c("controlTable", "recordKeys", "controlTableKeys"))]
fmt_str <- paste0("{\n ",
block_str,
" block_keys <- ", wrapr::map_to_char(c(x$recordKeys, x$controlTableKeys)),
"\n\n # becomes\n\n " ,
row_str,
" row_keys <- ", wrapr::map_to_char(x$recordKeys),
"\n\n # args: ", gsub("['\"]+", "", wrapr::map_to_char(args)),
"\n}\n\n")
fmt_str
}
#' @export
#'
print.blocks_to_rowrecs_spec <- function(x, ...) {
fmt_str <- format(x)
cat(fmt_str)
invisible(fmt_str)
}
#' @export
#'
t.rowrecs_to_blocks_spec <- function(x) {
class(x) <- "blocks_to_rowrecs_spec"
return(x)
}
#' @export
#'
t.blocks_to_rowrecs_spec <- function(x) {
class(x) <- "rowrecs_to_blocks_spec"
return(x)
}
# use the transform spec
#' Use transform spec to layout data.
#'
#' @param transform object of class rowrecs_to_blocks_spec
#' @param table data.frame or relop.
#' @return re-arranged data or data reference (relop).
#'
#' @examples
#'
#' d <- wrapr::build_frame(
#' "id" , "AUC", "R2" |
#' 1 , 0.7 , 0.4 |
#' 2 , 0.8 , 0.5 )
#' transform <- rowrecs_to_blocks_spec(
#' wrapr::qchar_frame(
#' "measure", "value" |
#' "AUC" , AUC |
#' "R2" , R2 ),
#' recordKeys = "id")
#' print(transform)
#' layout_by(transform, d)
#'
#' d <- wrapr::build_frame(
#' "id", "measure", "value" |
#' 1 , "AUC" , 0.7 |
#' 1 , "R2" , 0.4 |
#' 2 , "AUC" , 0.8 |
#' 2 , "R2" , 0.5 )
#' transform <- blocks_to_rowrecs_spec(
#' wrapr::qchar_frame(
#' "measure", "value" |
#' "AUC" , AUC |
#' "R2" , R2 ),
#' recordKeys = "id")
#' print(transform)
#' layout_by(transform, d)
#'
#' @export
#'
layout_by <- function(transform, table) {
UseMethod("layout_by")
}
#' Use transform spec to layout data.
#'
#' @param transform object of class rowrecs_to_blocks_spec
#' @param table data.frame or relop.
#' @return re-arranged data or data reference (relop).
#'
#' @examples
#'
#' d <- wrapr::build_frame(
#' "id" , "AUC", "R2" |
#' 1 , 0.7 , 0.4 |
#' 2 , 0.8 , 0.5 )
#'
#' transform <- rowrecs_to_blocks_spec(
#' wrapr::qchar_frame(
#' "measure", "value" |
#' "AUC" , AUC |
#' "R2" , R2 ),
#' recordKeys = "id")
#'
#' print(transform)
#' layout_by(transform, d)
#'
#' @export
#'
layout_by.rowrecs_to_blocks_spec <- function(transform, table) {
rowrecs_to_blocks(wideTable = table,
controlTable = transform$controlTable,
controlTableKeys = transform$controlTableKeys,
columnsToCopy = transform$recordKeys,
checkNames = transform$checkNames,
checkKeys = transform$checkKeys,
strict = transform$strict,
allow_rqdatatable = transform$allow_rqdatatable)
}
#' Use transform spec to layout data.
#'
#' @param transform object of class blocks_to_rowrecs_spec.
#' @param table data.frame or relop.
#' @return re-arranged data or data reference (relop).
#'
#' @examples
#'
#' d <- wrapr::build_frame(
#' "id", "measure", "value" |
#' 1 , "AUC" , 0.7 |
#' 1 , "R2" , 0.4 |
#' 2 , "AUC" , 0.8 |
#' 2 , "R2" , 0.5 )
#'
#' transform <- blocks_to_rowrecs_spec(
#' wrapr::qchar_frame(
#' "measure", "value" |
#' "AUC" , AUC |
#' "R2" , R2 ),
#' recordKeys = "id")
#'
#' print(transform)
#'
#' layout_by(transform, d)
#'
#' @export
#'
layout_by.blocks_to_rowrecs_spec <- function(transform, table) {
blocks_to_rowrecs(tallTable = table,
keyColumns = transform$recordKeys,
controlTable = transform$controlTable,
controlTableKeys = transform$controlTableKeys,
checkNames = transform$checkNames,
checkKeys = transform$checkKeys,
strict = transform$strict,
allow_rqdatatable = transform$allow_rqdatatable)
}
#' Multiply/join row records into block records.
#'
#' Call \code{rowrecs_to_blocks()}.
#'
#' @param table data (data.frame or relop).
#' @param transform a rowrecs_to_blocks_spec.
#' @return rowrecs_to_blocks() result.
#'
#' @examples
#'
#' d <- wrapr::build_frame(
#' "id", "AUC", "R2" |
#' 1 , 0.7 , 0.4 |
#' 2 , 0.8 , 0.5 )
#'
#' transform <- rowrecs_to_blocks_spec(
#' wrapr::qchar_frame(
#' "measure", "value" |
#' "AUC" , AUC |
#' "R2" , R2 ),
#' recordKeys = "id")
#'
#' d %**% transform
#'
#' # identity (in structure)
#' d %**% transform %//% t(transform)
#'
#' @export
#'
`%**%` <- function(table, transform) {
if(!("rowrecs_to_blocks_spec" %in% class(transform))) {
stop("cdata::`%**%` transform must be of class rowrecs_to_blocks_spec")
}
rowrecs_to_blocks(wideTable = table,
controlTable = transform$controlTable,
controlTableKeys = transform$controlTableKeys,
columnsToCopy = transform$recordKeys,
checkNames = transform$checkNames,
checkKeys = transform$checkKeys,
strict = transform$strict,
allow_rqdatatable = transform$allow_rqdatatable)
}
#' Factor-out (aggregate/project) block records into row records.
#'
#' Call \code{blocks_to_rowrecs()}.
#'
#' @param table data (data.frame or relop).
#' @param transform a rowrecs_to_blocks_spec.
#' @return blocks_to_rowrecs() result.
#'
#' @examples
#'
#' d <- wrapr::build_frame(
#' "id", "measure", "value" |
#' 1 , "AUC" , 0.7 |
#' 1 , "R2" , 0.4 |
#' 2 , "AUC" , 0.8 |
#' 2 , "R2" , 0.5 )
#'
#' transform <- blocks_to_rowrecs_spec(
#' wrapr::qchar_frame(
#' "measure", "value" |
#' "AUC" , AUC |
#' "R2" , R2 ),
#' recordKeys = "id")
#'
#' d %//% transform
#'
#' # identity (in structure)
#' d %//% transform %**% t(transform)
#'
#' @export
#'
`%//%` <- function(table, transform) {
if(!("blocks_to_rowrecs_spec" %in% class(transform))) {
stop("cdata::`%//%` transform must be of class blocks_to_rowrecs_spec")
}
blocks_to_rowrecs(tallTable = table,
keyColumns = transform$recordKeys,
controlTable = transform$controlTable,
controlTableKeys = transform$controlTableKeys,
checkNames = transform$checkNames,
checkKeys = transform$checkKeys,
strict = transform$strict,
allow_rqdatatable = transform$allow_rqdatatable)
}
# wrapr dot-pipe interface
#' @export
#'
apply_right.rowrecs_to_blocks_spec <- function(pipe_left_arg,
pipe_right_arg,
pipe_environment,
left_arg_name,
pipe_string,
right_arg_name) {
table <- pipe_left_arg
transform_spec <- pipe_right_arg
rowrecs_to_blocks(wideTable = table,
controlTable = transform_spec$controlTable,
controlTableKeys = transform_spec$controlTableKeys,
columnsToCopy = transform_spec$recordKeys,
checkNames = transform_spec$checkNames,
checkKeys = transform_spec$checkKeys,
strict = transform_spec$strict,
allow_rqdatatable = transform_spec$allow_rqdatatable)
}
#' @export
#'
apply_right.blocks_to_rowrecs_spec <- function(pipe_left_arg,
pipe_right_arg,
pipe_environment,
left_arg_name,
pipe_string,
right_arg_name) {
table <- pipe_left_arg
transform_spec <- pipe_right_arg
blocks_to_rowrecs(tallTable = table,
keyColumns = transform_spec$recordKeys,
controlTable = transform_spec$controlTable,
controlTableKeys = transform_spec$controlTableKeys,
checkNames = transform_spec$checkNames,
checkKeys = transform_spec$checkKeys,
strict = transform_spec$strict,
allow_rqdatatable = transform_spec$allow_rqdatatable)
}
# general spec
#' Create a record to record spec.
#'
#' Create a general record to record transform specification.
#'
#' @param incoming_shape data.frame, definition of incoming record shape.
#' @param outgoing_shape data.frame, defintion of outgoing record shape.
#' @param ... not used, force later arguments to bind by name.
#' @param recordKeys vector of columns identifying records.
#' @param incoming_controlTableKeys character, which column names of the incoming control table are considered to be keys.
#' @param outgoing_controlTableKeys character, which column names of the outgoing control table are considered to be keys.
#' @param checkNames passed to rowrecs_to_blocks.
#' @param checkKeys passed to rowrecs_to_blocks.
#' @param strict passed to rowrecs_to_blocks.
#' @param allow_rqdatatable_in logical, if TRUE allow rqdatatable shortcutting on simple conversions.
#' @param allow_rqdatatable_out logical, if TRUE allow rqdatatable shortcutting on simple conversions.
#' @return a record specification object
#'
#' @examples
#'
#'
#' incoming_shape <- qchar_frame(
#' "row", "col1", "col2", "col3" |
#' "row1", v11, v12, v13 |
#' "row2", v21, v22, v23 |
#' "row3", v31, v32, v33 )
#'
#'
#' outgoing_shape <- qchar_frame(
#' "column", "row1", "row2", "row3" |
#' "col1", v11, v21 , v31 |
#' "col2", v12, v22 , v32 |
#' "col3", v13, v23 , v33 )
#'
#' data <- build_frame(
#' 'record_id', 'row', 'col1', 'col2', 'col3' |
#' 1, 'row1', 1, 2, 3 |
#' 1, 'row2', 4, 5, 6 |
#' 1, 'row3', 7, 8, 9 |
#' 2, 'row1', 11, 12, 13 |
#' 2, 'row2', 14, 15, 16 |
#' 2, 'row3', 17, 18, 19 )
#'
#' print(data)
#'
#' layout <- layout_specification(
#' incoming_shape = incoming_shape,
#' outgoing_shape = outgoing_shape,
#' recordKeys = 'record_id')
#'
#' print(layout)
#'
#' data %.>% layout
#'
#' data %.>% layout %.>% .(t(layout))
#'
#' @export
#'
layout_specification <- function(incoming_shape = NULL,
outgoing_shape = NULL,
...,
recordKeys = character(0),
incoming_controlTableKeys = colnames(incoming_shape)[[1]],
outgoing_controlTableKeys = colnames(outgoing_shape)[[1]],
checkNames = TRUE,
checkKeys = TRUE,
strict = FALSE,
allow_rqdatatable_in = FALSE,
allow_rqdatatable_out = FALSE) {
# check for some trivial cases
if(is.null(incoming_shape)) {
if(is.null(outgoing_shape)) {
stop("at least one of incoming_shape or outgoing_shape must not be NULL")
}
return(rowrecs_to_blocks_spec(outgoing_shape,
controlTableKeys = outgoing_controlTableKeys,
recordKeys = character(0),
checkNames = checkNames,
checkKeys = checkKeys,
strict = strict,
allow_rqdatatable = allow_rqdatatable_out))
}
if(is.null(outgoing_shape)) {
return(blocks_to_rowrecs_spec(incoming_shape,
controlTableKeys = incoming_controlTableKeys,
recordKeys = recordKeys,
checkNames = checkNames,
checkKeys = checkKeys,
strict = strict,
allow_rqdatatable = allow_rqdatatable_in))
}
ca <- blocks_to_rowrecs_spec(incoming_shape,
controlTableKeys = incoming_controlTableKeys,
recordKeys = character(0),
checkNames = checkNames,
checkKeys = checkKeys,
strict = strict,
allow_rqdatatable = allow_rqdatatable_in)
cb <- rowrecs_to_blocks_spec(outgoing_shape,
controlTableKeys = outgoing_controlTableKeys,
recordKeys = character(0),
checkNames = checkNames,
checkKeys = checkKeys,
strict = strict,
allow_rqdatatable = allow_rqdatatable_out)
if((nrow(outgoing_shape)==1) && (length(outgoing_controlTableKeys)==0)) {
ra <- incoming_shape %.>% ca
if(isTRUE(all.equal(sort(colnames(ra)), sort(colnames(outgoing_shape))))) {
return(blocks_to_rowrecs_spec(incoming_shape,
controlTableKeys = incoming_controlTableKeys,
recordKeys = recordKeys,
checkNames = checkNames,
checkKeys = checkKeys,
strict = strict,
allow_rqdatatable = allow_rqdatatable_in))
}
}
if((nrow(incoming_shape)==1) && (length(incoming_controlTableKeys)==0)) {
cb_inv <- t(cb)
rb <- outgoing_shape %.>% cb_inv
if(isTRUE(all.equal(sort(colnames(rb)), sort(colnames(incoming_shape))))) {
return(rowrecs_to_blocks_spec(outgoing_shape,
controlTableKeys = outgoing_controlTableKeys,
recordKeys = character(0),
checkNames = checkNames,
checkKeys = checkKeys,
strict = strict,
allow_rqdatatable = allow_rqdatatable_out))
}
}
# check transform makes sense
tryCatch(
{ incoming_shape %.>% ca %.>% cb },
error = function(e) {
stop("cdata::layout_specification, incoming and outgoing shape not compatible")
}
)
a <- blocks_to_rowrecs_spec(incoming_shape,
controlTableKeys = incoming_controlTableKeys,
recordKeys = recordKeys,
checkNames = checkNames,
checkKeys = checkKeys,
strict = strict,
allow_rqdatatable = allow_rqdatatable_in)
b <- rowrecs_to_blocks_spec(outgoing_shape,
controlTableKeys = outgoing_controlTableKeys,
recordKeys = recordKeys,
checkNames = checkNames,
checkKeys = checkKeys,
strict = strict,
allow_rqdatatable = allow_rqdatatable_out)
r <- list(
blocks_to_rowrecs_spec = a,
rowrecs_to_blocks_spec = b)
class(r) <- "cdata_general_transform_spec"
r
}
#' @export
#'
apply_right.cdata_general_transform_spec <- function(pipe_left_arg,
pipe_right_arg,
pipe_environment,
left_arg_name,
pipe_string,
right_arg_name) {
table <- pipe_left_arg
transform_spec <- pipe_right_arg
blocks_to_rowrecs_spec <- transform_spec$blocks_to_rowrecs_spec
if(!is.null(blocks_to_rowrecs_spec)) {
table <- blocks_to_rowrecs(tallTable = table,
keyColumns = blocks_to_rowrecs_spec$recordKeys,
controlTable = blocks_to_rowrecs_spec$controlTable,
controlTableKeys = blocks_to_rowrecs_spec$controlTableKeys,
checkNames = blocks_to_rowrecs_spec$checkNames,
checkKeys = blocks_to_rowrecs_spec$checkKeys,
strict = blocks_to_rowrecs_spec$strict,
allow_rqdatatable = blocks_to_rowrecs_spec$allow_rqdatatable)
}
rowrecs_to_blocks_spec <- transform_spec$rowrecs_to_blocks_spec
if(!is.null(rowrecs_to_blocks_spec)) {
table <- rowrecs_to_blocks(wideTable = table,
controlTable = rowrecs_to_blocks_spec$controlTable,
controlTableKeys = rowrecs_to_blocks_spec$controlTableKeys,
columnsToCopy = rowrecs_to_blocks_spec$recordKeys,
checkNames = rowrecs_to_blocks_spec$checkNames,
checkKeys = rowrecs_to_blocks_spec$checkKeys,
strict = rowrecs_to_blocks_spec$strict,
allow_rqdatatable = rowrecs_to_blocks_spec$allow_rqdatatable)
}
return(table)
}
#' @export
#'
t.cdata_general_transform_spec <- function(x) {
layout_specification(incoming_shape = x$rowrecs_to_blocks_spec$controlTable,
outgoing_shape = x$blocks_to_rowrecs_spec$controlTable,
recordKeys = x$rowrecs_to_blocks_spec$recordKeys,
incoming_controlTableKeys = x$rowrecs_to_blocks_spec$controlTableKeys,
outgoing_controlTableKeys = x$blocks_to_rowrecs_spec$controlTableKeys,
checkNames = x$rowrecs_to_blocks_spec$checkNames,
checkKeys = x$rowrecs_to_blocks_spec$checkKeys,
strict = x$rowrecs_to_blocks_spec$strict,
allow_rqdatatable = x$rowrecs_to_blocks_spec$allow_rqdatatable)
}
#' Use transform spec to layout data.
#'
#' @param transform object of class blocks_to_rowrecs_spec.
#' @param table data.frame or relop.
#' @return re-arranged data or data reference (relop).
#'
#'
#' @export
#'
layout_by.cdata_general_transform_spec <- function(transform, table) {
blocks_to_rowrecs_spec <- transform$blocks_to_rowrecs_spec
table <- blocks_to_rowrecs(tallTable = table,
keyColumns = blocks_to_rowrecs_spec$recordKeys,
controlTable = blocks_to_rowrecs_spec$controlTable,
controlTableKeys = blocks_to_rowrecs_spec$controlTableKeys,
checkNames = blocks_to_rowrecs_spec$checkNames,
checkKeys = blocks_to_rowrecs_spec$checkKeys,
strict = blocks_to_rowrecs_spec$strict,
allow_rqdatatable = blocks_to_rowrecs_spec$allow_rqdatatable)
rowrecs_to_blocks_spec <- transform$rowrecs_to_blocks_spec
rowrecs_to_blocks(wideTable = table,
controlTable = rowrecs_to_blocks_spec$controlTable,
controlTableKeys = rowrecs_to_blocks_spec$controlTableKeys,
columnsToCopy = rowrecs_to_blocks_spec$recordKeys,
checkNames = rowrecs_to_blocks_spec$checkNames,
checkKeys = rowrecs_to_blocks_spec$checkKeys,
strict = rowrecs_to_blocks_spec$strict,
allow_rqdatatable = rowrecs_to_blocks_spec$allow_rqdatatable)
}
#' @export
#'
format.cdata_general_transform_spec <- function(x, ...) {
details_in <- get_transform_details(x$blocks_to_rowrecs_spec)
details_out <- get_transform_details(x$rowrecs_to_blocks_spec)
in_record <- details_in$block_record
out_record <- details_out$block_record
in_str <- wrapr::draw_framec(in_record, unquote_cols = setdiff(colnames(in_record),
x$blocks_to_rowrecs_spec$controlTableKeys))
out_str <- wrapr::draw_framec(out_record, unquote_cols = setdiff(colnames(out_record),
x$rowrecs_to_blocks_spec$controlTableKeys))
args <- x$blocks_to_rowrecs_spec[setdiff(names(x$blocks_to_rowrecs_spec),
c("controlTable", "recordKeys", "controlTableKeys"))]
fmt_str <- paste0("{\n ",
in_str,
" in_keys <- ", wrapr::map_to_char(c(x$blocks_to_rowrecs_spec$recordKeys,
x$blocks_to_rowrecs_spec$controlTableKeys)),
"\n\n # becomes\n\n " ,
out_str,
" out_keys <- ", wrapr::map_to_char(c(x$rowrecs_to_blocks_spec$recordKeys,
x$rowrecs_to_blocks_spec$controlTableKeys)),
"\n\n # args: ", gsub("['\"]+", "", wrapr::map_to_char(args)),
"\n}\n\n")
fmt_str
}
#' @export
#'
print.cdata_general_transform_spec <- function(x, ...) {
fmt_str <- format(x)
cat(fmt_str)
invisible(fmt_str)
}