Skip to content

Commit

Permalink
added methods for dplyr::slice_*(), closes #84
Browse files Browse the repository at this point in the history
  • Loading branch information
tilltnet committed Feb 1, 2024
1 parent 359b831 commit f8ece90
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ S3method(semi_join,egor)
S3method(setdiff,egor)
S3method(setequal,egor)
S3method(slice,egor)
S3method(slice_head,egor)
S3method(slice_max,egor)
S3method(slice_min,egor)
S3method(slice_sample,egor)
S3method(slice_tail,egor)
S3method(subset,egor)
S3method(summarise,egor)
S3method(summarize,egor)
Expand Down
41 changes: 41 additions & 0 deletions R/dplyr_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,47 @@ slice.egor <- function(.data, ...) {
return_egor_with_result(.data, result)
}

#' @export
#' @noRd
#' @method slice_head egor
slice_head.egor <- function(.data, ...) {
result <- slice_head(tibble_egos(.data)[[attr(.data, "active")]], ...)
return_egor_with_result(.data, result)
}

#' @export
#' @noRd
#' @method slice_tail egor
slice_tail.egor <- function(.data, ...) {
result <- slice_tail(tibble_egos(.data)[[attr(.data, "active")]], ...)
return_egor_with_result(.data, result)
}

#' @export
#' @noRd
#' @method slice_min egor
slice_min.egor <- function(.data, ...) {
result <- slice_min(tibble_egos(.data)[[attr(.data, "active")]], ...)
return_egor_with_result(.data, result)
}


#' @export
#' @noRd
#' @method slice_max egor
slice_max.egor <- function(.data, ...) {
result <- slice_max(tibble_egos(.data)[[attr(.data, "active")]], ...)
return_egor_with_result(.data, result)
}

#' @export
#' @noRd
#' @method slice_sample egor
slice_sample.egor <- function(.data, ...) {
result <- slice_sample(tibble_egos(.data)[[attr(.data, "active")]], ...)
return_egor_with_result(.data, result)
}


# group_by count tally summarise ------------------------------------------

Expand Down
45 changes: 45 additions & 0 deletions tests/testthat/test-dplyr_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ test_that("methods for dplyr are working",
egor::activate(e, "alter") %>%
filter(sex == "w")


# slice and slice_* -------------------------------------

slice(e, 1)

activate(e, alter) %>%
Expand All @@ -52,6 +55,48 @@ test_that("methods for dplyr are working",
activate(e, aatie) %>%
slice(1:2)

slice_head(e, n = 1)

activate(e, alter) %>%
slice_head() # defaults to n = 1

activate(e, aatie) %>%
slice_head(prop = .2)

slice_tail(e, n = 1)

activate(e, alter) %>%
slice_tail() # defaults to n = 1

activate(e, aatie) %>%
slice_tail(prop = .2)

slice_min(e, age.years, n = 2)

activate(e, alter) %>%
slice_min(age.years) # defaults to n = 1

activate(e, aatie) %>%
slice_min(weight, prop = .2)

slice_max(e, age.years, n = 1)

activate(e, alter) %>%
slice_max(age.years) # defaults to n = 1

activate(e, aatie) %>%
slice_max(weight, prop = .2)

slice_sample(e, n = 1)

activate(e, alter) %>%
slice_sample() # defaults to n = 1

activate(e, aatie) %>%
slice_sample(prop = .01)

# summarise ----------------------------------------------

e %>%
activate("alter") %>%
group_by(.egoID) %>%
Expand Down

0 comments on commit f8ece90

Please sign in to comment.