Skip to content

Commit

Permalink
Tweak extend to include {Inf} separately if endpoint is like Inf)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughjonesd committed Sep 10, 2019
1 parent 0255670 commit ec3276d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
13 changes: 6 additions & 7 deletions R/breaks-impl.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,19 @@ maybe_extend <- function (breaks, x, extend) {


extend_breaks <- function (breaks) {
if (length(breaks) == 0 || breaks[1] > -Inf) {
left <- attr(breaks, "left")
left <- attr(breaks, "left")
# we add a break if the first break is above -Inf *or* if it is (-Inf. ...
if (length(breaks) == 0 || breaks[1] > -Inf || ! left[1]) {
breaks <- c(-Inf, breaks) # deletes attributes inc class
breaks <- create_breaks(breaks, c(TRUE, left))
}
attr(breaks, "left")[1] <- TRUE

if (breaks[length(breaks)] < Inf) {
left <- attr(breaks, "left")
left <- attr(breaks, "left")
# add a break if the last break is finite, or if it is ..., +Inf)
if (breaks[length(breaks)] < Inf || left[length(left)]) {
breaks <- c(breaks, Inf) # deletes attributes inc class
breaks <- create_breaks(breaks, c(left, FALSE))
}
left <- attr(breaks, "left")
attr(breaks, "left")[length(left)] <- FALSE

return(breaks)
}
Expand Down
9 changes: 5 additions & 4 deletions R/chop.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ NULL
#' [lbl_numerals()].
#'
#' If `extend` is `TRUE`, intervals will be extended to \code{[-Inf,
#' min(breaks))} and \code{(max(breaks), Inf]}, unless those endpoints are
#' already infinite. If `extend` is `NULL` (the default), intervals will
#' be extended only if the data is outside their range.
#' min(breaks))} and \code{(max(breaks), Inf]} (unless the first and last
#' endpoints are already `[-Inf` and `Inf]` respectively). If `extend` is `NULL`
#' (the default), intervals will be extended only if the data is outside their
#' range.
#'
#' `NA` values in `x`, and values which are outside the (extendeD) endpoints,
#' `NA` values in `x`, and values which are outside the (extended) endpoints,
#' return `NA`.
#'
#' Note that `chop`, like all of R, uses binary arithmetic. Thus, numbers may
Expand Down
9 changes: 5 additions & 4 deletions man/chop.Rd

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

8 changes: 4 additions & 4 deletions tests/testthat/test-chop.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ test_that("NA, NaN and Inf", {

x <- c(-Inf, 1, Inf)
# if extend is NULL, we should ensure even Inf is included
r <- chop(x, brk_right(-Inf, close_end = FALSE), labels = "a")
expect_equivalent(r, factor(c("a", "a", "a"), levels = "a"))
r <- chop(x, brk_left(Inf, close_end = FALSE), labels = "a")
expect_equivalent(r, factor(c("a", "a", "a"), levels = "a"))
r <- chop(x, brk_right(-Inf, close_end = FALSE), labels = c("-Inf", "a"))
expect_equivalent(r, factor(c("-Inf", "a", "a"), levels = c("-Inf", "a")))
r <- chop(x, brk_left(Inf, close_end = FALSE), labels = c("a", "Inf"))
expect_equivalent(r, factor(c("a", "a", "Inf"), levels = c("a", "Inf")))

# otherwise, we respect close_end = FALSE
r <- chop(x, brk_right(c(-Inf, Inf), close_end = FALSE), labels = "a",
Expand Down

0 comments on commit ec3276d

Please sign in to comment.