Skip to content

Commit f3696a1

Browse files
committed
start adding YAML interop
1 parent f1b253c commit f3696a1

3 files changed

Lines changed: 80 additions & 2 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: cdata
22
Type: Package
33
Title: Fluid Data Transformations
4-
Version: 1.1.1
5-
Date: 2019-07-24
4+
Version: 1.1.2
5+
Date: 2019-09-15
66
Authors@R: c(
77
person("John", "Mount", email = "jmount@win-vector.com", role = c("aut", "cre")),
88
person("Nina", "Zumel", email = "nzumel@win-vector.com", role = c("aut")),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
# cdata 1.1.2 2019/09/15
3+
4+
* Add data_algebra interop.
5+
26
# cdata 1.1.1 2019/07/24
37

48
* Fix some newly dead links.

R/cdata_yaml.R

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
convert_yaml_to_data_frame <- function(obj) {
3+
n <- length(obj)
4+
if (n<1) {
5+
return(NULL)
6+
}
7+
n1 = names(obj)[[1]]
8+
d = data.frame(x = obj[[n1]],
9+
stringsAsFactors = FALSE)
10+
colnames(d) <- n1
11+
if (n<2) {
12+
return(d)
13+
}
14+
for (i in (2:n)) {
15+
ni = names(obj)[[i]]
16+
d[[ni]] <- obj[[ni]]
17+
}
18+
return(d)
19+
}
20+
21+
convert_yaml_to_record_spec <- function(obj) {
22+
record_keys <- NULL
23+
control_table_keys <- NULL
24+
if ("record_keys" %in% names(obj)) {
25+
record_keys = obj$record_keys
26+
}
27+
if ("control_table_keys" %in% names(obj)) {
28+
control_table_keys = obj$control_table_keys
29+
}
30+
control_table <- convert_yaml_to_data_frame(obj$control_table)
31+
return(list('record_keys' = record_keys,
32+
'control_table_keys' = control_table_keys,
33+
'control_table' = control_table))
34+
}
35+
36+
#' Read a cdata record transform from a simple object (such as is imported from YAML).
37+
#'
38+
#' @param obj object to convert
39+
#' @return cdata transform specification
40+
#'
41+
#' @export
42+
#'
43+
convert_yaml_to_cdata_spec <- function(obj) {
44+
blocks_in = NULL
45+
blocks_out = NULL
46+
if ("blocks_out" %in% names(obj)) {
47+
blocks_out = convert_yaml_to_record_spec(obj$blocks_out)
48+
}
49+
if ("blocks_in" %in% names(obj)) {
50+
blocks_in = convert_yaml_to_record_spec(obj$blocks_in)
51+
}
52+
# TODO: work on check/strict options
53+
if ((!is.null(blocks_in)) && (!is.null(blocks_out))) {
54+
# TODO: work on recordKeys point
55+
return(cdata::layout_specification(incoming_shape = blocks_in$control_table,
56+
outgoing_shape = blocks_out$control_table,
57+
recordKeys = blocks_in$record_keys,
58+
incoming_controlTableKeys = blocks_in$control_table_keys,
59+
outgoing_controlTableKeys = blocks_out$control_table_keys))
60+
}
61+
if (!is.null(blocks_in)) {
62+
return(blocks_to_rowrecs_spec(controlTable = blocks_in$control_table,
63+
recordKeys = blocks_in$record_keys,
64+
controlTableKeys = blocks_in$control_table_keys))
65+
}
66+
if (!is.null(blocks_out)) {
67+
return(rowrecs_to_blocks_spec(controlTable = blocks_out$control_table,
68+
recordKeys = blocks_out$record_keys,
69+
controlTableKeys = blocks_out$control_table_keys))
70+
}
71+
return(NULL)
72+
}
73+
74+
# TODO: cdata to yaml

0 commit comments

Comments
 (0)