Skip to content

Commit

Permalink
closes #79 (#80)
Browse files Browse the repository at this point in the history
* closes #79

* test grouped_df where idx is specified directly in runner call

* add tests to grouped_df

* update news about defunct arg

* reload docs after removing type argument

* convince codecov that grouped_df is covered

* release
  • Loading branch information
gogonzo authored Oct 3, 2021
1 parent 43e1f1e commit 3ddbfb8
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 59 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: runner
Title: Running Operations for Vectors
Type: Package
Version: 0.4.0
Version: 0.4.1
Depends: R (>= 3.0)
Language: en-US
Encoding: UTF-8
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# runner 0.4.1
* Fix `runner.grouped_df` (`dplyr` class) to not ignore `k` argument.
* removed defunct argument `type`.

# runner 0.4.0
* defunct `type` argument in favor of `simplify`.
* fixed error when using `runner::runner`.
Expand Down
20 changes: 2 additions & 18 deletions R/run.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Apply running function
#'
#' Applies custom function on running windows.
#' @param x (`vector`, `data.frame`, `matrix`, `xts`)\cr
#' @param x (`vector`, `data.frame`, `matrix`, `xts`, `grouped_df`)\cr
#' Input in runner custom function `f`.
#'
#' @param k (`integer` vector or single value)\cr
Expand Down Expand Up @@ -42,9 +42,6 @@
#' Whether incomplete window should return `NA` (if `na_pad = TRUE`)
#' Incomplete window is when some parts of the window are out of range.
#'
#' @param type (defunct)\cr
#' argument defunct from version 0.4.0. Use `simplify` instead.
#'
#' @param simplify (`logical` or `character` value)\cr
#' should the result be simplified to a vector, matrix or higher dimensional
#' array if possible. The default value, `simplify = TRUE`, returns a vector or
Expand All @@ -57,7 +54,6 @@
#' Create and pass the cluster to the `runner` function to run each window
#' calculation in parallel. See \code{\link[parallel]{makeCluster}} in details.
#'
#'
#' @param ... (optional)\cr
#' other arguments passed to the function `f`.
#'
Expand Down Expand Up @@ -149,18 +145,10 @@ runner <- function(
idx = integer(0),
at = integer(0),
na_pad = FALSE,
type,
simplify = TRUE,
cl = NULL,
...
) {
if (!missing(type)) {
warning(
"Argument 'type' is defunct and will be completely removed in the next release. #nolint
Please use 'simplify' argument to manage the output type."
)
}

UseMethod("runner", x)
}

Expand Down Expand Up @@ -235,7 +223,6 @@ runner.default <- function( #nolint
idx = integer(0),
at = integer(0),
na_pad = FALSE,
type,
simplify = TRUE,
cl = NULL,
...
Expand Down Expand Up @@ -339,7 +326,6 @@ runner.data.frame <- function( #nolint
idx = integer(0),
at = integer(0),
na_pad = FALSE,
type,
simplify = TRUE,
cl = NULL,
...
Expand Down Expand Up @@ -419,14 +405,14 @@ runner.grouped_df <- function(
idx = integer(0),
at = integer(0),
na_pad = FALSE,
type,
simplify = TRUE,
cl = NULL,
...
) {
runner.data.frame(
x = this_group(x),
f = f,
k = k,
lag = lag,
idx = idx,
at = at,
Expand Down Expand Up @@ -459,7 +445,6 @@ runner.matrix <- function(
idx = integer(0),
at = integer(0),
na_pad = FALSE,
type,
simplify = TRUE,
cl = NULL,
...
Expand Down Expand Up @@ -533,7 +518,6 @@ runner.xts <- function(
idx = integer(0),
at = integer(0),
na_pad = FALSE,
type,
simplify = TRUE,
cl = NULL,
...
Expand Down
117 changes: 104 additions & 13 deletions inst/tinytest/test_external_compatibility.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ expect_identical(
"index"
)


# correct grouping results ------
data <- data.frame(
index = c(2, 5, 5, 7, 10, 12, 12, 12, 15, 16, 19,
20, 20, 21, 23, 23, 25, 27, 27, 28),
Expand All @@ -52,34 +50,127 @@ data <- data.frame(
x = 1:20
)

# running on grouped_df
# single column in mutate is grouped ------
res <- data %>%
group_by(group1) %>%
mutate(
m = length(x)
)

expect_true(
all(res$m == 20)
)

# running on grouped_df index set ------
res <- data %>%
dplyr::group_by(group1, group2) %>%
run_by(idx = "index") %>%
dplyr::mutate(
x = runner(
xx = runner(
.,
f = function(x) {
unique(paste(x$group1, x$group2))
f = function(df) {
paste(df$x, collapse = " ")
}
)
)

expected <- unlist(
use.names = FALSE,
tapply(
data$x,
paste(data$group1, data$group2),
runner,
f = paste,
collapse = " "
)
)

expect_identical(
res$x,
paste(data$group1, data$group2)
res$xx,
expected
)

# running on columns from grouped_df

# running on columns from grouped_df k without idx -----
grouped_dplyr <- data %>%
dplyr::group_by(group1, group2) %>%
run_by(idx = "index") %>%
dplyr::mutate(
xx = runner(
x = unique(paste(group1, group2)),
f = paste, collapse = "")
x = .,
f = function(df) {
paste(df$x, collapse = " ")
},
k = 2
)
)

expected <- unlist(
use.names = FALSE,
tapply(
data$x,
paste(data$group1, data$group2),
runner,
f = paste,
collapse = " ",
k = 2
)
)

expect_equal(
grouped_dplyr$xx,
paste(data$group1, data$group2)
expected
)

# running on columns from grouped_df k with idx -----
grouped_dplyr <- data %>%
dplyr::group_by(group1, group2) %>%
dplyr::mutate(
xx = runner.grouped_df(
x = .,
f = function(df) {
paste(df$x, collapse = " ")
},
k = 2,
idx = index
)
)

grouped_dplyr2 <- data %>%
dplyr::group_by(group1, group2) %>%
run_by(idx = "index") %>%
dplyr::mutate(
xx = runner.grouped_df(
x = .,
f = function(df) {
paste(df$x, collapse = " ")
},
k = 2
)
)

expected <- unlist(
use.names = FALSE,
tapply(
1:40,
paste(data$group1, data$group2),
function(idx) {
runner(
data$x[idx],
f = paste,
collapse = " ",
k = 2,
idx = data$index[idx]
)
}
)
)

expect_equal(
grouped_dplyr$xx,
expected
)

expect_equal(
grouped_dplyr2$xx,
expected
)
9 changes: 0 additions & 9 deletions inst/tinytest/test_runner.R
Original file line number Diff line number Diff line change
Expand Up @@ -1347,12 +1347,3 @@ expect_error(
runner(1:10, lag = rep(5, 10), idx = 1:10, at = c(4, 5), f = mean),
"length\\(lag\\) should be 1 or equal to"
)

#Test defunct -------
expect_warning(
runner(
1:10,
f = function(x) x,
type = "numeric"
)
)
2 changes: 1 addition & 1 deletion man/fill_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/lag_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/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/min_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/minmax_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/run_by.Rd

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

11 changes: 1 addition & 10 deletions man/runner.Rd

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

Loading

0 comments on commit 3ddbfb8

Please sign in to comment.