9
9
# ' 3:4
10
10
# ' )
11
11
# '
12
+ # ' @keywords internal
12
13
time_slide_to_simple_hop <- function (.slide_comp , ... , .before_n_steps , .after_n_steps ) {
13
14
function (grp_data , grp_key , ref_inds ) {
14
15
available_ref_time_values <- vec_slice(grp_data $ time_value , ref_inds )
@@ -81,29 +82,46 @@ time_slide_to_simple_hop <- function(.slide_comp, ..., .before_n_steps, .after_n
81
82
}
82
83
}
83
84
84
- # TODO simplify to just trailing and put shift elsewhere?
85
+ # ' Convert upstream specialized slide function to a simple hop function
85
86
# '
86
87
# ' upstream_slide_to_simple_hop(frollmean, .in_colnames = "value", .out_colnames = "slide_value", .before_n_steps = 1L, .after_n_steps = 0L)(
87
88
# ' tibble(time_value = 1:5, value = 1:5),
88
89
# ' tibble(geo_value = 1),
89
90
# ' 3:4
90
91
# ' )
92
+ # ' upstream_slide_to_simple_hop(slide_mean, .in_colnames = "value", .out_colnames = "slide_value", .before_n_steps = 1L, .after_n_steps = 0L)(
93
+ # ' tibble(time_value = 1:5, value = 1:5),
94
+ # ' tibble(geo_value = 1),
95
+ # ' 3:4
96
+ # ' )
97
+ # '
98
+ # ' upstream_slide_to_simple_hop(frollmean, .in_colnames = "value", .out_colnames = "slide_value", .before_n_steps = Inf, .after_n_steps = 0L)(
99
+ # ' tibble(time_value = 1:5, value = 1:5),
100
+ # ' tibble(geo_value = 1),
101
+ # ' 3:4
102
+ # ' )
103
+ # '
104
+ # ' @keywords internal
91
105
upstream_slide_to_simple_hop <- function (.f , ... , .in_colnames , .out_colnames , .before_n_steps , .after_n_steps ) {
92
106
f_info <- upstream_slide_f_info(.f , ... )
93
107
in_colnames <- .in_colnames
94
108
out_colnames <- .out_colnames
95
109
f_from_package <- f_info $ from_package
96
- # TODO move .before_n_steps, .after_n_steps to args of this function?
110
+ f_dots_baked <-
111
+ if (rlang :: dots_n(... ) == 0L ) {
112
+ # Leaving `.f` unchanged slightly improves computation speed and trims
113
+ # debug stack traces:
114
+ .f
115
+ } else {
116
+ purrr :: partial(.f , ... = , ... ) # `... =` stands in for future args
117
+ }
97
118
switch (f_from_package ,
98
119
data.table = if (.before_n_steps == Inf ) {
99
120
if (.after_n_steps != 0L ) {
100
121
stop(" .before_n_steps only supported with .after_n_steps = 0" )
101
122
}
102
123
function (grp_data , grp_key , ref_inds ) {
103
- grp_data [, out_colnames ] <-
104
- f_dots_baked(grp_data [, in_colnames ], seq_len(nrow(grp_data )), adaptive = TRUE )
105
- grp_data [, out_colnames ] <- out_cols
106
- grp_data
124
+ f_dots_baked(grp_data [, in_colnames ], seq_len(nrow(grp_data )), adaptive = TRUE )
107
125
}
108
126
} else {
109
127
function (grp_data , grp_key , ref_inds ) {
@@ -116,24 +134,24 @@ upstream_slide_to_simple_hop <- function(.f, ..., .in_colnames, .out_colnames, .
116
134
c(out_col [(.after_n_steps + 1L ): length(out_col )], rep(NA , .after_n_steps ))
117
135
})
118
136
}
119
- grp_data [, out_colnames ] <- out_cols
120
- grp_data
137
+ out_cols
121
138
}
122
139
},
123
140
slider =
124
141
# TODO Inf checks?
125
142
function (grp_data , grp_key , ref_inds ) {
126
- for ( col_i in seq_along( in_colnames )) {
127
- grp_data [[ out_colnames [[ col_i ]]]] <- f_dots_baked( grp_data [[ in_colnames [[ col_i ]]]], before = .before_n_steps , after = .after_n_steps )
128
- }
129
- grp_data
143
+ names( in_colnames ) <- in_colnames
144
+ lapply( in_colnames , function ( in_colname ) {
145
+ f_dots_baked( grp_data [[ in_colname ]], before = .before_n_steps , after = .after_n_steps )
146
+ })
130
147
},
148
+ # TODO improve message
131
149
stop(" unsupported package" )
132
150
)
133
151
}
134
152
135
- # TODO maybe make ref_inds optional or have special handling if it's the whole sequence?
136
- #
153
+ # TODO maybe make ref_inds optional or have special handling if it's the whole sequence? But can it ever be the full sequence in the common fixed-width window case? Should be some truncation of it.
154
+
137
155
# TODO decide whether/where to put time range stuff
138
156
139
157
# TODO grp_ -> ek_ ?
0 commit comments