99# ' 3:4
1010# ' )
1111# '
12+ # ' @keywords internal
1213time_slide_to_simple_hop <- function (.slide_comp , ... , .before_n_steps , .after_n_steps ) {
1314 function (grp_data , grp_key , ref_inds ) {
1415 available_ref_time_values <- vec_slice(grp_data $ time_value , ref_inds )
@@ -81,19 +82,39 @@ time_slide_to_simple_hop <- function(.slide_comp, ..., .before_n_steps, .after_n
8182 }
8283}
8384
84- # TODO simplify to just trailing and put shift elsewhere?
85+ # ' Convert upstream specialized slide function to a simple hop function
8586# '
8687# ' upstream_slide_to_simple_hop(frollmean, .in_colnames = "value", .out_colnames = "slide_value", .before_n_steps = 1L, .after_n_steps = 0L)(
8788# ' tibble(time_value = 1:5, value = 1:5),
8889# ' tibble(geo_value = 1),
8990# ' 3:4
9091# ' )
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
91105upstream_slide_to_simple_hop <- function (.f , ... , .in_colnames , .out_colnames , .before_n_steps , .after_n_steps ) {
92106 f_info <- upstream_slide_f_info(.f , ... )
93107 in_colnames <- .in_colnames
94108 out_colnames <- .out_colnames
95109 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+ }
97118 switch (f_from_package ,
98119 data.table = if (.before_n_steps == Inf ) {
99120 if (.after_n_steps != 0L ) {
@@ -116,24 +137,24 @@ upstream_slide_to_simple_hop <- function(.f, ..., .in_colnames, .out_colnames, .
116137 c(out_col [(.after_n_steps + 1L ): length(out_col )], rep(NA , .after_n_steps ))
117138 })
118139 }
119- grp_data [, out_colnames ] <- out_cols
120- grp_data
140+ out_cols
121141 }
122142 },
123143 slider =
124144 # TODO Inf checks?
125145 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
146+ names( in_colnames ) <- in_colnames
147+ lapply( in_colnames , function ( in_colname ) {
148+ f_dots_baked( grp_data [[ in_colname ]], before = .before_n_steps , after = .after_n_steps )
149+ })
130150 },
151+ # TODO improve message
131152 stop(" unsupported package" )
132153 )
133154}
134155
135- # TODO maybe make ref_inds optional or have special handling if it's the whole sequence?
136- #
156+ # 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.
157+
137158# TODO decide whether/where to put time range stuff
138159
139160# TODO grp_ -> ek_ ?
0 commit comments