Skip to content

Commit

Permalink
Remove underscore versions of dplyr verbs (select_ etc)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughjonesd committed Jan 28, 2025
1 parent b6f0cd4 commit 37425a3
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 70 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

* Bugfix: add newline at end of report_latex_dependencies() output.
Thanks @ceresek.

* Removed underscore dplyr verbs (`slice_`, `select_` etc.) These have long
been deprecated in dplyr itself.

# huxtable 5.5.7

Expand Down
60 changes: 8 additions & 52 deletions R/dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
NULL


filter_.huxtable <- function (.data, ..., .dots) {
filter.huxtable <- function(.data, ...) {
ht <- .data
.data <- as.data.frame(.data)
.data$filter.huxtable.rownames <- rownames(.data)
Expand All @@ -13,36 +13,6 @@ filter_.huxtable <- function (.data, ..., .dots) {
}


filter.huxtable <- function(.data, ...) {}
body(filter.huxtable) <- body(filter_.huxtable)


mutate_.huxtable <- function (.data, ..., .dots) {
ht <- .data
.data <- as.data.frame(.data)
copy_cell_props <- if (! is.null(.dots$copy_cell_props)) .dots$copy_cell_props else TRUE
.dots <- .dots[setdiff(names(.dots), "copy_cell_props")]
result <- NextMethod()
result <- as_hux(result, autoformat = FALSE)

for (a in c(huxtable_row_attrs, huxtable_table_attrs)) attr(result, a) <- attr(ht, a)

# unlike in extract-methods we can't assume new columns are on right: transmute can reorder them
# columns may even be reordered by e.g. a=NULL,...,a=new_value
# so: all columns with an old name get the old attributes. New columns get copied attributes maybe.
match_cols <- match(colnames(result), colnames(ht))
if (copy_cell_props) match_cols <- Reduce(function (x, y) if (is.na(y)) x else y, match_cols, accumulate = TRUE)
result_cols <- ! is.na(match_cols)
match_cols <- na.omit(match_cols)

for (a in huxtable_cell_attrs) attr(result, a)[, result_cols] <- attr(ht, a)[, match_cols]
for (a in huxtable_col_attrs) attr(result, a)[result_cols] <- attr(ht, a)[match_cols]

result <- set_attr_dimnames(result)

result
}

#' Use dplyr verbs with huxtable objects
#'
#' Huxtable can be used with dplyr verbs [dplyr::select()], [dplyr::rename()],
Expand Down Expand Up @@ -94,7 +64,7 @@ mutate.huxtable <- function (.data, ..., copy_cell_props = TRUE) {
match_cols <- match(colnames(result), colnames(ht))
if (copy_cell_props) match_cols <- Reduce(function (x, y) if (is.na(y)) x else y, match_cols, accumulate = TRUE)
result_cols <- ! is.na(match_cols)
match_cols <- na.omit(match_cols)
match_cols <- match_cols[result_cols]

for (a in huxtable_cell_attrs) attr(result, a)[, result_cols] <- attr(ht, a)[, match_cols]
for (a in huxtable_col_attrs) attr(result, a)[result_cols] <- attr(ht, a)[match_cols]
Expand All @@ -105,11 +75,10 @@ mutate.huxtable <- function (.data, ..., copy_cell_props = TRUE) {
}


transmute_.huxtable <- mutate_.huxtable

transmute.huxtable <- mutate.huxtable

arrange_.huxtable <- function (.data, ..., .dots) {

arrange.huxtable <- function(.data, ...) {
ht <- .data
.data <- as.data.frame(.data)
.data$arrange.huxtable.rownames <- rownames(.data)
Expand All @@ -118,29 +87,23 @@ arrange_.huxtable <- function (.data, ..., .dots) {
}


arrange.huxtable <- function(.data, ...) {}
body(arrange.huxtable) <- body(arrange_.huxtable)


slice_.huxtable <- function (.data, ..., .dots) {
slice.huxtable <- function (.data, ...) {
ht <- .data
.data <- as.data.frame(.data)
.data$slice.huxtable.rownames <- rownames(.data)
result <- NextMethod()
ht[na.omit(match(result$slice.huxtable.rownames, .data$slice.huxtable.rownames)), ]
}


slice.huxtable <- function (.data, ...) {}
body(slice.huxtable) <- body(slice_.huxtable)
}


# The following functions will only be registered with dplyr if
# packageVersion("dplyr") <= "0.8.5".
# After that, we can just use the dplyr builtins. (Until they break
# subclasses again....)

select_.huxtable <- function (.data, ..., .dots) {

select.huxtable <- function(.data, ...) {
ht <- .data
.data <- as.data.frame(t(colnames(.data)), stringsAsFactors = FALSE)
colnames(.data) <- colnames(ht)
Expand All @@ -152,11 +115,4 @@ select_.huxtable <- function (.data, ..., .dots) {
}


select.huxtable <- function(.data, ...) {}
body(select.huxtable) <- body(select_.huxtable)


rename_.huxtable <- select_.huxtable


rename.huxtable <- select.huxtable
7 changes: 0 additions & 7 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,13 @@

if (requireNamespace("dplyr", quietly = TRUE)) {
register_s3_method("dplyr", "arrange")
register_s3_method("dplyr", "arrange_")
register_s3_method("dplyr", "filter")
register_s3_method("dplyr", "filter_")
register_s3_method("dplyr", "mutate")
register_s3_method("dplyr", "mutate_")
register_s3_method("dplyr", "slice")
register_s3_method("dplyr", "slice_")
register_s3_method("dplyr", "transmute")
register_s3_method("dplyr", "transmute_")
if (utils::packageVersion("dplyr") <= "0.8.5") {
register_s3_method("dplyr", "rename")
register_s3_method("dplyr", "rename_")
register_s3_method("dplyr", "select")
register_s3_method("dplyr", "select_")
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions man/huxtable-news.Rd

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

11 changes: 1 addition & 10 deletions tests/testthat/test-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test_that("slice, filter, arrange and pull work", {
})


test_that("mutate, mutate_ and transmute work", {
test_that("mutate and transmute work", {
ht <- hux(a = 1:3, b = 1:3)
bold(ht)[1, ] <- TRUE

Expand All @@ -59,15 +59,6 @@ test_that("mutate, mutate_ and transmute work", {

expect_silent(ht5 <- dplyr::mutate(ht, a = NULL))
expect_equivalent(dim(font(ht5)), c(3, 1))

ht6 <- dplyr::mutate_(ht, .dots = list(x = quote(a + b)))
expect_equivalent(ht6$x, c(2, 4, 6))
expect_equivalent(bold(ht6)[, 3], c(TRUE, FALSE, FALSE))

ht7 <- dplyr::mutate_(ht, .dots = list(x = quote(a + b), copy_cell_props = FALSE))
expect_equivalent(ht7$x, c(2, 4, 6))
expect_equivalent(bold(ht7)[, 3], c(FALSE, FALSE, FALSE))
expect_identical(names(ht7), c("a", "b", "x"))
})


Expand Down

0 comments on commit 37425a3

Please sign in to comment.