Skip to content

Commit a18be49

Browse files
committed
as_slide_computation documentation
1 parent 6a594f1 commit a18be49

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

R/utils.R

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,39 +181,45 @@ assert_sufficient_f_args <- function(f, ...) {
181181
}
182182
}
183183

184-
#' Convert to function
184+
#' Generate a `epi[x]_slide` computation function from a function, formula, or quosure
185185
#'
186-
#' @description
187-
#' `as_slide_computation()` transforms a one-sided formula into a function.
188-
#' This powers the lambda syntax in packages like purrr.
186+
#' @description `as_slide_computation()` transforms a one-sided formula or a
187+
#' quosure into a function; functions are returned as-is or with light
188+
#' modifications to calculate `ref_time_value`.
189189
#'
190190
#' This code and documentation borrows heavily from [`rlang::as_function`]
191191
#' (https://github.com/r-lib/rlang/blob/c55f6027928d3104ed449e591e8a225fcaf55e13/R/fn.R#L343-L427).
192192
#'
193193
#' This code extends `rlang::as_function` to create functions that take three
194-
#' arguments. The arguments can be accessed via the idiomatic `.x`, `.y`,
195-
#' etc, positional references (`..1`, `..2`, etc), and also by `epi
196-
#' [x]_slide`-specific names.
194+
#' arguments. The arguments can be accessed via the idiomatic `.`, `.x`, and
195+
#' `.y`, extended to include `.z`; positional references `..1` and `..2`,
196+
#' extended to include `..3`; and also by `epi[x]_slide`-specific names
197+
#' `.group_key` and `.ref_time_value`.
197198
#'
198199
#' @source https://github.com/r-lib/rlang/blob/c55f6027928d3104ed449e591e8a225fcaf55e13/R/fn.R#L343-L427
199200
#'
200-
#' @param x A function or formula.
201+
#' @param x A function, one-sided formula, or quosure.
201202
#'
202-
#' If a **function**, it is used as is.
203+
#' If a **function** and `calc_ref_time_value` is `FALSE`, the function is
204+
#' returned as-is, with no modifications. If `calc_ref_time_value` is
205+
#' `TRUE`, a wrapper function is returned. The wrapper calculates
206+
#' `ref_time_value` based on the input data and passes it to the original
207+
#' function.
203208
#'
204-
#' If a **formula**, e.g. `~ mean(.x$cases)`, it is converted to a function with up
205-
#' to three arguments: `.x` (single argument), or `.x` and `.y`
209+
#' If a **formula**, e.g. `~ mean(.x$cases)`, it is converted to a function
210+
#' with up to three arguments: `.x` (single argument), or `.x` and `.y`
206211
#' (two arguments), or `.x`, `.y`, and `.z` (three arguments). The `.`
207212
#' placeholder can be used instead of `.x`, `.group_key` can be used in
208213
#' place of `.y`, and `.ref_time_value` can be used in place of `.z`. This
209214
#' allows you to create very compact anonymous functions (lambdas) with up
210-
#' to three inputs. Functions created from formulas have a special class. Use
211-
#' `rlang::is_lambda()` to test for it.
212-
#'
213-
#' If a **string**, the function is looked up in `env`. Note that
214-
#' this interface is strictly for user convenience because of the
215-
#' scoping issues involved. Package developers should avoid
216-
#' supplying functions by name and instead supply them by value.
215+
#' to three inputs. Functions created from formulas have a special class.
216+
#' Use `rlang::is_lambda()` to test for it.
217+
#'
218+
#' If a **quosure**, in the case that `f` was not provided to the parent
219+
#' `epi[x]_slide` call and the `...` is interpreted as an expression for
220+
#' tidy evaluation, it is evaluated within a wrapper function. The wrapper
221+
#' sets up object access via a data mask. `ref_time_value` is calculated
222+
#' depending on the `cal_ref_time_value` setting.
217223
#'
218224
#' @param before how far `before` each `ref_time_value` the sliding window
219225
#' should extend, as specified in the parent `epi[x]_slide` call Must be a

0 commit comments

Comments
 (0)