Skip to content

Commit afc7224

Browse files
committed
basic attempt
1 parent b87a4fe commit afc7224

File tree

1 file changed

+107
-2
lines changed

1 file changed

+107
-2
lines changed

R/cbind.R

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ ct_bind_cols = function(...){
3737
if(heterogeneous){
3838
cli_abort("Cannot cbind crosstable with different `by` dimensions: {.val {by_dim}}",
3939
class="ct_cbind_hetero_multiby_error")
40+
# ct_list = ct_list %>%
41+
# map(~{
42+
# by = attr(.x, "by")
43+
# if(length(by)>1) return(.x)
44+
# lvls = attr(.x, "by_levels")[[1]]
45+
# i = names(.x) %in% lvls
46+
# new_names = paste0(by, "=", names(.x)[i])
47+
# names(.x)[i] = new_names
48+
# names(attr(.x, "by_levels")[[1]]) = new_names
49+
# # browser()
50+
# rownames(attr(.x, "by_table")) = new_names
51+
# # attr(.x, "by_levels") %>% names
52+
#
53+
# .x
54+
# })
4055
}
4156

4257
all_multi_by = all(multi_by)
@@ -48,16 +63,106 @@ ct_bind_cols = function(...){
4863
rtn = reduce(ct_list, ~full_join(.x, .y, by=c(".id", "label", "variable")))
4964
attr(rtn, "by_table") = map(ct_list, ~attr(.x, "by_table")) %>% unname() %>% unlist()
5065
} else {
51-
cli_abort("Cannot cbind crosstable with only one `by` dimension: {.val {by_dim}}",
52-
class="ct_cbind_monocol_error")
66+
# cli_abort("Cannot cbind crosstable with only one `by` dimension: {.val {by_dim}}",
67+
# class="ct_cbind_monocol_error")
5368

5469
id = ct_list %>% map(~unique(.x$.id))
5570
if(n_distinct(id)>1){
5671
cli_abort("Cannot cbind mono-column crosstables with different `.id`: {id}",
5772
class="ct_cbind_hetero_byname_error")
5873
}
74+
75+
# browser()
76+
ct_list2 = ct_list %>%
77+
map(~{
78+
by = attr(.x, "by")
79+
if(length(by)>1) return(.x)
80+
lvls = attr(.x, "by_levels")[[1]]
81+
i = names(.x) %in% lvls
82+
new_names = paste0(by, "=", names(.x)[i])
83+
names(.x)[i] = new_names
84+
# names(attr(.x, "by_levels")[[1]]) = new_names
85+
# browser()
86+
rownames(attr(.x, "by_table")) = new_names
87+
# attr(.x, "by_levels") %>% names
88+
89+
.x
90+
})
91+
92+
#TODO control of rows order, e.g.:
93+
# ct_list2 = ct_list2[order(lengths(ct_list2))]
94+
rtn = reduce(ct_list2, ~full_join(.x, .y, by=c(".id", "label", "variable")))
95+
rtn2 = mtcars2 %>% crosstable(cyl, by=c(am, vs))
96+
97+
by_label = map(ct_list2, ~attr(.x, "by_label")) %>% unname() %>% unlist()
98+
attr(rtn, "by_label") = by_label
99+
attr(rtn, "by_levels") = map(ct_list2, ~attr(.x, "by_levels")) %>% unname() %>% unlist(recursive=F)
100+
attr(rtn, "by_table") = map(ct_list2, ~attr(.x, "by_table")) %>% unname() %>% unlist() %>%
101+
set_names(~{
102+
p = str_remove(.x, "=.*")
103+
# paste(p, by_label[p], sep="=")
104+
l = paste(p, by_label[p], sep="=")
105+
paste(" & ", l)
106+
l
107+
})
108+
# attr(rtn, "by_table") = map(ct_list2, ~attr(.x, "by_table")) %>% unname() %>% unlist() %>%
109+
# set_names(~{
110+
# # browser()
111+
# paste(" & ", .x)
112+
# # by_label
113+
# })
114+
115+
# debugonce(af)
116+
#FIXME en fait c'est une galère car le header se base sur la names(ct), pas sur les attributes...
117+
# browser()
118+
rtn %>% af()
119+
120+
rtn2 %>% af()
121+
rtn %>% attributes()
122+
rtn2 %>% attributes()
123+
124+
125+
attr(rtn, "by_table"); attr(rtn2, "by_table")
126+
# attr(rtn, "by_label"); attr(rtn2, "by_label")
127+
# attr(rtn, "by_levels"); attr(rtn2, "by_levels")
128+
129+
130+
cli_abort("In fact, we can only cbind multi-column crosstables for now.",
131+
class="ct_cbind_monocol_error")
132+
# stop("en faisant un cbind, on transforme en multiby! Il faut donc refaire toute l'ingénierie...")
133+
59134
}
60135

136+
# browser()
137+
138+
# ct_list %>% map(names)
139+
# rtn %>% af()
140+
141+
# rtn = full_join(ct1, ct2, by=c(".id", "label", "variable"))
142+
143+
144+
145+
# ct1 %>% attributes()
146+
# ct2 %>% attributes()
147+
# .x %>% attributes()
148+
# rtn %>% attributes()
149+
150+
# attr(rtn, "by_table") = c(attr(ct1, "by_table"), attr(ct2, "by_table"))
151+
# attr(rtn, "by_label");attr(ct1, "by_label"); attr(ct2, "by_label")
152+
# attr(rtn, "by_levels");attr(ct1, "by_levels"); attr(ct2, "by_levels")
153+
#
154+
# lst(rtn, ct1, ct2) %>% map(~{attr(.x, "by")})
155+
#
156+
# waldo::compare(
157+
# # ct1 %>% attributes(),
158+
# ct2 %>% attributes(),
159+
# rtn %>% attributes()
160+
# )
161+
#
162+
# rtn %>% af(header_show_n=T)
163+
#
164+
# browser()
165+
61166
rtn
62167
}
63168

0 commit comments

Comments
 (0)