@@ -18,9 +18,10 @@ ct_compact = function(data, ...){
18
18
# ' @param data the object to compact
19
19
# ' @param name_from name of the column to be collapsed when compacting
20
20
# ' @param name_to name of the column that will receive the collapsed column. Will be created if it doesn't exist.
21
+ # ' @param ... additional arguments (not used)
22
+ # ' @param id_from name of the columns to use as cut-off. Useful when successive `name_from` have the same value.
21
23
# ' @param wrap_cols name of the columns to wrap
22
24
# ' @param rtn_flextable whether to return a formatted [flextable::flextable()] object or a simple `data.frame`
23
- # ' @param ... additional arguments (not used)
24
25
# ' @rdname ct_compact
25
26
# '
26
27
# ' @author Dan Chaltiel
@@ -40,10 +41,15 @@ ct_compact = function(data, ...){
40
41
# ' ct_compact(x, name_from="Species", name_to="Petal.Length")
41
42
# ' x$Species2 = substr(x$Species, 1, 1)
42
43
# ' ct_compact(x, name_from="Species", wrap_cols="Species2")
43
- ct_compact.data.frame = function (data , name_from , name_to = " variable" , wrap_cols = NULL , rtn_flextable = FALSE , ... ){
44
+ # ' ct_compact(x, name_from="Species", id_from="Species2") #cut on "v"
45
+ ct_compact.data.frame = function (data , name_from , name_to = " variable" , ... ,
46
+ id_from = name_from ,
47
+ wrap_cols = NULL , rtn_flextable = FALSE ){
44
48
assert_scalar(name_from )
45
49
assert_scalar(name_to )
46
- id = (data [[name_from ]]!= lag(data [[name_from ]])) %> % replace_na(TRUE )
50
+ assert_scalar(id_from )
51
+ ifr = sym(id_from )
52
+ id = (data [[id_from ]]!= lag(data [[id_from ]])) %> % replace_na(TRUE )
47
53
48
54
nf = sym(name_from )
49
55
nt = sym(name_to )
@@ -52,12 +58,12 @@ ct_compact.data.frame = function(data, name_from, name_to="variable", wrap_cols=
52
58
rtn = data [x ,] %> %
53
59
mutate(
54
60
across(everything(), as.character ),
55
- gp = row_number()== 1 | !! nf != lag(!! nf ),
61
+ gp = row_number()== 1 | !! ifr != lag(!! ifr ),
56
62
!! nt : = ifelse(.data $ gp , !! nf , !! nt ),
57
63
across(any_of(wrap_cols ), ~ ifelse(.data $ gp , .x , " " )),
58
64
across(- any_of(c(name_to , wrap_cols )), ~ ifelse(.data $ gp , " " , .x )),
59
65
) %> %
60
- select(any_of(name_to ), everything(), - any_of(name_from ), - " gp" )
66
+ select(any_of(name_to ), everything(), - any_of(c( name_from , id_from ) ), - " gp" )
61
67
rownames(rtn ) = NULL # resets row numbers
62
68
63
69
if (rtn_flextable ){
@@ -88,7 +94,8 @@ ct_compact.data.frame = function(data, name_from, name_to="variable", wrap_cols=
88
94
# ' x=crosstable(mtcars2, c(disp,hp,am), by=vs, test=TRUE, effect=TRUE)
89
95
# ' ct_compact(x)
90
96
# ' ct_compact(x, name_from=".id")
91
- ct_compact.crosstable = function (data , name_from = c(" label" , " .id" ), name_to = " variable" , keep_id = FALSE , ... ){
97
+ ct_compact.crosstable = function (data , name_from = c(" label" , " .id" ), name_to = " variable" ,
98
+ id_from = " .id" , keep_id = FALSE , ... ){
92
99
by_levels = attr(data , " by_levels" )
93
100
by = attr(data , " by" )
94
101
name_from = match.arg(name_from )
@@ -98,15 +105,13 @@ ct_compact.crosstable = function(data, name_from=c("label", ".id"), name_to="var
98
105
if (isTRUE(keep_id )) keep_id = " {label} ({.id})"
99
106
data = data %> % mutate(label = glue(keep_id ))
100
107
}
101
-
102
108
rtn = data %> %
103
- select(- any_of(rcol )) %> %
104
- ct_compact.data.frame(name_from = name_from , name_to = name_to ,
109
+ ct_compact.data.frame(name_from = name_from , name_to = name_to , id_from = id_from ,
105
110
wrap_cols = wrap_cols , rtn_flextable = FALSE )
106
111
107
112
new_attr_names = setdiff(names(attributes(data )), names(attributes(rtn )))
108
113
attributes(rtn ) = c(attributes(rtn ), attributes(data )[new_attr_names ])
109
- class(rtn ) = c(" crosstable " , " compacted_crosstable" , " data.frame " )
114
+ class(rtn ) = c(" compacted_crosstable" , class( data ) )
110
115
rtn
111
116
}
112
117
0 commit comments