Skip to content

Commit

Permalink
fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gogonzo committed Mar 3, 2024
1 parent 57e7d46 commit 80cbc32
Show file tree
Hide file tree
Showing 6 changed files with 1,009 additions and 539 deletions.
59 changes: 29 additions & 30 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
#' @return vector - \code{x} containing all \code{x} elements with \code{NA}
#' replaced with previous non-NA element.
#' @examples
#' fill_run(c(NA, NA,1:10, NA, NA), run_for_first = TRUE)
#' fill_run(c(NA, NA,1:10, NA, NA), run_for_first = TRUE)
#' fill_run(c(NA, NA,1:10, NA, NA), run_for_first = FALSE)
#' fill_run(c(NA, NA, 1:10, NA, NA), run_for_first = TRUE)
#' fill_run(c(NA, NA, 1:10, NA, NA), run_for_first = TRUE)
#' fill_run(c(NA, NA, 1:10, NA, NA), run_for_first = FALSE)
#' fill_run(c(NA, NA, 1, 2, NA, NA, 2, 2, NA, NA, 1, NA, NA), run_for_first = TRUE, only_within = TRUE)
#' @export
fill_run <- function(x, run_for_first = FALSE, only_within = FALSE) {
.Call('_runner_fill_run', PACKAGE = 'runner', x, run_for_first, only_within)
.Call("_runner_fill_run", PACKAGE = "runner", x, run_for_first, only_within)
}

#' Lag dependent on variable
Expand All @@ -37,7 +37,7 @@ fill_run <- function(x, run_for_first = FALSE, only_within = FALSE) {
#' lag_run(letters[1:10], lag = 2, idx = c(1, 1, 1, 2, 3, 4, 6, 7, 8, 10), nearest = TRUE)
#' @export
lag_run <- function(x, lag = 1L, idx = integer(0), nearest = FALSE) {
.Call('_runner_lag_run', PACKAGE = 'runner', x, lag, idx, nearest)
.Call("_runner_lag_run", PACKAGE = "runner", x, lag, idx, nearest)
}

#' Length of running windows
Expand All @@ -51,7 +51,7 @@ lag_run <- function(x, lag = 1L, idx = integer(0), nearest = FALSE) {
#' length_run(k = 3, idx = c(1, 2, 2, 4, 5, 5, 5, 5, 5, 5))
#' @export
length_run <- function(k = integer(1), lag = integer(1), idx = integer(0)) {
.Call('_runner_length_run', PACKAGE = 'runner', k, lag, idx)
.Call("_runner_length_run", PACKAGE = "runner", k, lag, idx)
}

#' Running min/max
Expand All @@ -65,7 +65,7 @@ length_run <- function(k = integer(1), lag = integer(1), idx = integer(0)) {
#' @return list.
#' @export
minmax_run <- function(x, metric = "min", na_rm = TRUE) {
.Call('_runner_minmax_run', PACKAGE = 'runner', x, metric, na_rm)
.Call("_runner_minmax_run", PACKAGE = "runner", x, metric, na_rm)
}

#' Running sum
Expand Down Expand Up @@ -104,35 +104,35 @@ minmax_run <- function(x, metric = "min", na_rm = TRUE) {
#' @examples
#' set.seed(11)
#' x1 <- rnorm(15)
#' x2 <- sample(c(rep(NA, 5),rnorm(15)), 15, replace = TRUE)
#' x2 <- sample(c(rep(NA, 5), rnorm(15)), 15, replace = TRUE)
#' k <- sample(1:15, 15, replace = TRUE)
#' sum_run(x1)
#' sum_run(x2, na_rm = TRUE)
#' sum_run(x2, na_rm = FALSE)
#' sum_run(x2, na_rm = TRUE, k = 4)
#' @export
sum_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at = integer(0), na_rm = TRUE, na_pad = FALSE) {
.Call('_runner_sum_run', PACKAGE = 'runner', x, k, lag, idx, at, na_rm, na_pad)
.Call("_runner_sum_run", PACKAGE = "runner", x, k, lag, idx, at, na_rm, na_pad)
}

#' Running mean
#'
#' Running mean in specified window of numeric vector.
#' @inheritParams sum_run
#' @inheritParams runner
#' @return mean {numeric} vector of length equals length of \code{x}.
#' @return mean (`numeric`) vector of length equals length of \code{x}.
#' @examples
#' set.seed(11)
#' x1 <- rnorm(15)
#' x2 <- sample(c(rep(NA,5), rnorm(15)), 15, replace = TRUE)
#' x2 <- sample(c(rep(NA, 5), rnorm(15)), 15, replace = TRUE)
#' k <- sample(1:15, 15, replace = TRUE)
#' mean_run(x1)
#' mean_run(x2, na_rm = TRUE)
#' mean_run(x2, na_rm = FALSE )
#' mean_run(x2, na_rm = TRUE, k=4)
#' mean_run(x2, na_rm = FALSE)
#' mean_run(x2, na_rm = TRUE, k = 4)
#' @export
mean_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at = integer(0), na_rm = TRUE, na_pad = FALSE) {
.Call('_runner_mean_run', PACKAGE = 'runner', x, k, lag, idx, at, na_rm, na_pad)
.Call("_runner_mean_run", PACKAGE = "runner", x, k, lag, idx, at, na_rm, na_pad)
}

#' Running maximum
Expand All @@ -142,19 +142,19 @@ mean_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at =
#' specified \code{k} window size.
#' @inheritParams runner
#' @inheritParams sum_run
#' @return max {numeric} vector of length equals length of \code{x}.
#' @return max (`numeric`) vector of length equals length of \code{x}.
#' @examples
#' set.seed(11)
#' x1 <- sample( c(1,2,3), 15, replace=TRUE)
#' x2 <- sample( c(NA,1,2,3), 15, replace=TRUE)
#' k <- sample( 1:4, 15, replace=TRUE)
#' x1 <- sample(c(1, 2, 3), 15, replace = TRUE)
#' x2 <- sample(c(NA, 1, 2, 3), 15, replace = TRUE)
#' k <- sample(1:4, 15, replace = TRUE)
#' max_run(x1) # simple cumulative maximum
#' max_run(x2, na_rm = TRUE) # cumulative maximum with removing NA.
#' max_run(x2, na_rm = TRUE, k=4) # maximum in 4-element window
#' max_run(x2, na_rm = FALSE, k=k) # maximum in varying k window size
#' max_run(x2, na_rm = TRUE, k = 4) # maximum in 4-element window
#' max_run(x2, na_rm = FALSE, k = k) # maximum in varying k window size
#' @export
max_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at = integer(0), na_rm = TRUE, na_pad = FALSE) {
.Call('_runner_max_run', PACKAGE = 'runner', x, k, lag, idx, at, na_rm, na_pad)
.Call("_runner_max_run", PACKAGE = "runner", x, k, lag, idx, at, na_rm, na_pad)
}

#' Running minimum
Expand All @@ -163,19 +163,19 @@ max_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at =
#' \code{min_run} calculates running min on given \code{x} numeric vector, specified \code{k} window size.
#' @inheritParams runner
#' @inheritParams sum_run
#' @return min {numeric} vector of length equals length of \code{x}.
#' @return min (`numeric`) vector of length equals length of \code{x}.
#' @examples
#' set.seed(11)
#' x1 <- sample(c(1, 2, 3), 15, replace = TRUE)
#' x2 <- sample(c(NA, 1, 2, 3), 15, replace = TRUE)
#' k <- sample(1:4, 15, replace = TRUE)
#' k <- sample(1:4, 15, replace = TRUE)
#' min_run(x1)
#' min_run(x2, na_rm = TRUE)
#' min_run(x2, na_rm = TRUE, k = 4)
#' min_run(x2, na_rm = FALSE, k = k)
#' @export
min_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at = integer(0), na_rm = TRUE, na_pad = FALSE) {
.Call('_runner_min_run', PACKAGE = 'runner', x, k, lag, idx, at, na_rm, na_pad)
.Call("_runner_min_run", PACKAGE = "runner", x, k, lag, idx, at, na_rm, na_pad)
}

#' Running streak length
Expand All @@ -188,7 +188,7 @@ min_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at =
#' number of consecutive occurrences.
#' @examples
#' set.seed(11)
#' x1 <- sample(c("a","b"), 15, replace = TRUE)
#' x1 <- sample(c("a", "b"), 15, replace = TRUE)
#' x2 <- sample(c(NA_character_, "a", "b"), 15, replace = TRUE)
#' k <- sample(1:4, 15, replace = TRUE)
#' streak_run(x1) # simple streak run
Expand All @@ -197,7 +197,7 @@ min_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at =
#' streak_run(x1, k = k) # streak run within varying window size specified by vector k
#' @export
streak_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at = integer(0), na_rm = TRUE, na_pad = FALSE) {
.Call('_runner_streak_run', PACKAGE = 'runner', x, k, lag, idx, at, na_rm, na_pad)
.Call("_runner_streak_run", PACKAGE = "runner", x, k, lag, idx, at, na_rm, na_pad)
}

#' Running which
Expand All @@ -213,14 +213,14 @@ streak_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at
#' set.seed(11)
#' x1 <- sample(c(1, 2, 3), 15, replace = TRUE)
#' x2 <- sample(c(NA, 1, 2, 3), 15, replace = TRUE)
#' k <- sample(1:4, 15, replace = TRUE)
#' k <- sample(1:4, 15, replace = TRUE)
#' which_run(x1)
#' which_run(x2, na_rm = TRUE)
#' which_run(x2, na_rm = TRUE, k = 4)
#' which_run(x2, na_rm = FALSE, k = k)
#' @export
which_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at = integer(0), which = "last", na_rm = TRUE, na_pad = FALSE) {
.Call('_runner_which_run', PACKAGE = 'runner', x, k, lag, idx, at, which, na_rm, na_pad)
.Call("_runner_which_run", PACKAGE = "runner", x, k, lag, idx, at, which, na_rm, na_pad)
}

#' List of running windows
Expand All @@ -236,6 +236,5 @@ which_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at
#' window_run(letters[1:10], k = c(1, 2, 2, 4, 5, 5, 5, 5, 5, 5))
#' @export
window_run <- function(x, k = integer(0), lag = integer(1), idx = integer(0), at = integer(0), na_pad = FALSE) {
.Call('_runner_window_run', PACKAGE = 'runner', x, k, lag, idx, at, na_pad)
.Call("_runner_window_run", PACKAGE = "runner", x, k, lag, idx, at, na_pad)
}

26 changes: 12 additions & 14 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Validate date time character
#'
#' Checks if the character is a valid date time string
#' @param (`character`) can be anything but suppose to be a character.
#' @param x (`character`) can be anything but suppose to be a character.
#' @return `logical(1)` denoting if all elements of the character vectors are valid
#' @keywords internal
.is_datetime_valid <- function(x) {
Expand All @@ -19,7 +19,7 @@
#' @inheritParams runner
#' @param param name of the parameter to be printed in error message
#' @examples
#' k <- "1 month"
#' k <- "1 month"
#' idx <- seq(
#' as.POSIXct("2019-01-01 03:02:01"),
#' as.POSIXct("2020-01-01 03:02:01"),
Expand Down Expand Up @@ -71,7 +71,6 @@
}

return(as.integer(idx) - from)

} else if (is(k, "difftime")) {
k <- if (param == "k") {
if (any(k < 0)) {
Expand Down Expand Up @@ -136,12 +135,11 @@
#' @keywords internal
.seq_at <- function(at, idx) { # nolint
if (length(at) == 1 &&
(
(is.character(at) && .is_datetime_valid(at)) ||
(
(is.character(at) && .is_datetime_valid(at)) ||
is(at, "difftime")
)
) {

)
) {
if (length(idx) == 0) {
stop(
sprintf("`idx` can't be empty while specifying `at` as time interval")
Expand All @@ -151,7 +149,7 @@

if (inherits(idx, c("Date", "POSIXct", "POSIXxt", "POSIXlt"))) {
at <- if ((is.character(at) && grepl("^-", at)) ||
(is(at, "difftime") && at < 0)) {
(is(at, "difftime") && at < 0)) {
seq(max(idx), min(idx), by = at)
} else {
seq(min(idx), max(idx), by = at)
Expand Down Expand Up @@ -191,7 +189,7 @@
#' @inheritParams runner
#' @return resolved `at`
#' @keywords internal
.check_unresolved_at <- function(x, at) { #nolint
.check_unresolved_at <- function(x, at) { # nolint
arg_name <- deparse(substitute(at))

if (length(at) > 0) {
Expand All @@ -203,7 +201,7 @@
} else if (inherits(at, c("numeric", "integer", "Date", "POSIXct", "POSIXxt", "POSIXlt"))) {
NULL
} else {
stop(
stop(
sprintf(
"`%s` is invalid, should be either:
- `numeric`, `Date`, `POSIXct` or `POSIXlt` vector of any length.
Expand All @@ -226,7 +224,7 @@
#' @inheritParams runner
#' @return resolved `idx`
#' @keywords internal
.check_unresolved_difftime <- function(x, k) { #nolint
.check_unresolved_difftime <- function(x, k) { # nolint
arg_name <- deparse(substitute(k))
if (length(k) > 0) {
if (length(k) == 1 && is.character(k) && k %in% names(x)) {
Expand Down Expand Up @@ -259,15 +257,15 @@
#' @inheritParams runner
#' @return resolved `idx`
#' @keywords internal
.check_unresolved_index <- function(x, idx) { #nolint
.check_unresolved_index <- function(x, idx) { # nolint
arg_name <- deparse(substitute(idx))

if (length(idx) > 0) {
if (is.character(idx) && length(idx) == 1 && idx %in% names(x)) {
idx <- x[[idx]]
}
if (length(idx) == nrow(x) &&
inherits(idx, c("numeric", "integer", "Date", "POSIXct", "POSIXxt", "POSIXlt"))) {
inherits(idx, c("numeric", "integer", "Date", "POSIXct", "POSIXxt", "POSIXlt"))) {
NULL
} else {
stop(
Expand Down
2 changes: 1 addition & 1 deletion man/max_run.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/mean_run.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/min_run.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 80cbc32

Please sign in to comment.