Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Add remove_whitespace()
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Jan 17, 2024
1 parent 105f4ba commit f5eafad
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ exportMethods(remove_Inf)
exportMethods(remove_NA)
exportMethods(remove_constant)
exportMethods(remove_empty)
exportMethods(remove_whitespace)
exportMethods(remove_zero)
exportMethods(replace_Inf)
exportMethods(replace_NA)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## New classes and methods
* Add `palette_color_continuous()` and `palette_color_discrete()` for color mapping.
* Add `palette_shape()` and `palette_size()` for symbol shape and size mapping.
* Add `remove_whitespace()` to remove leading/trailing Whitespace.

## Enhancements
* `compact()` and `remove_*()` gained a new `verbose` argument to report extra information on progress.
Expand Down
19 changes: 19 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,25 @@ setGeneric(
def = function(x, ...) standardGeneric("remove_constant")
)

## Whitespace ------------------------------------------------------------------
#' Remove Leading/Trailing Whitespace
#'
#' @param x An \R object (should be a [`matrix`] or a [`data.frame`]).
#' @param which A [`character`] string specifying whether to remove `both`
#' leading and trailing whitespace (default), or only leading ("`left`") or
#' trailing ("`right`").
#' @param ... Currently not used.
#' @example inst/examples/ex-whitespace.R
#' @seealso [trimws()]
#' @author N. Frerebeau
#' @docType methods
#' @family data cleaning tools
#' @aliases remove_whitespace-method
setGeneric(
name = "remove_whitespace",
def = function(x, ...) standardGeneric("remove_whitespace")
)

# Data transformation ==========================================================

# Mathematics ==================================================================
Expand Down
32 changes: 32 additions & 0 deletions R/remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,35 @@ setMethod(
margin = 2, all = FALSE, verbose = verbose)
}
)

# Whitespace ===================================================================
#' @export
#' @rdname remove_whitespace
#' @aliases remove_whitespace,data.frame-method
setMethod(
f = "remove_whitespace",
signature = c(x = "data.frame"),
definition = function(x, which = c("both", "left", "right")) {
x[] <- lapply(
X = x,
FUN = function(x, which) {
if (!is.character(x)) return(x)
trimws(x, which = which)
},
which = which
)
x
}
)

#' @export
#' @rdname remove_whitespace
#' @aliases remove_whitespace,matrix-method
setMethod(
f = "remove_whitespace",
signature = c(x = "matrix"),
definition = function(x, which = c("both", "left", "right")) {
x[] <- trimws(x, which = which)
x
}
)
8 changes: 8 additions & 0 deletions inst/examples/ex-whitespace.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
x <- data.frame(
A = c(" Both. ", " Left.", "Right. "),
B = 1:3
)

remove_whitespace(x, which = "both")
remove_whitespace(x, which = "left")
remove_whitespace(x, which = "right")
12 changes: 12 additions & 0 deletions inst/tinytest/test_remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,15 @@ df1[1, 1] <- NA # Add NA
expect_equal(remove_constant(df1), df1)

expect_equal(remove_constant(df1, na.rm = TRUE), df2)

# Whitespace ===================================================================
x <- data.frame(A = c(" Both. ", " Left.", "Right. "), B = 1:3)
xb <- xl <- xr <- x
xb$A <- trimws(x$A, which = "both")
xl$A <- trimws(x$A, which = "left")
xr$A <- trimws(x$A, which = "right")

expect_equal(remove_whitespace(x, which = "both"), xb)
expect_equal(remove_whitespace(x, which = "left"), xl)
expect_equal(remove_whitespace(x, which = "right"), xr)
expect_equal(remove_whitespace(as.matrix(x), which = "both"), as.matrix(xb))
1 change: 1 addition & 0 deletions man/empty.Rd

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

1 change: 1 addition & 0 deletions man/infinite.Rd

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

1 change: 1 addition & 0 deletions man/missing.Rd

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

1 change: 1 addition & 0 deletions man/remove_constant.Rd

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

52 changes: 52 additions & 0 deletions man/remove_whitespace.Rd

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

3 changes: 2 additions & 1 deletion man/zero.Rd

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

0 comments on commit f5eafad

Please sign in to comment.