Skip to content

Commit

Permalink
export round_any
Browse files Browse the repository at this point in the history
  • Loading branch information
larmarange committed Mar 26, 2024
1 parent 86740a8 commit 139bb3e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Suggests:
vdiffr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
Language: en-US
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(round_any,POSIXct)
S3method(round_any,numeric)
export("%>%")
export(PositionLikert)
export(PositionLikertCount)
Expand All @@ -23,6 +25,7 @@ export(label_number_abs)
export(label_percent_abs)
export(position_likert)
export(position_likert_count)
export(round_any)
export(signif_stars)
export(stat_cross)
export(stat_prop)
Expand Down
21 changes: 0 additions & 21 deletions R/geom_stripped_rows.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,3 @@ GeomStrippedCols <- ggplot2::ggproto("GeomStrippedCols", ggplot2::Geom,
)
}
)

# Copied from plyr
# Round to multiple of any number.
#
# @param x numeric or date-time (POSIXct) vector to round
# @param accuracy number to round to; for POSIXct objects, a number of seconds
# @param f rounding function: \code{\link{floor}}, \code{\link{ceiling}} or
# \code{\link{round}}
round_any <- function(x, accuracy, f = round) {
UseMethod("round_any")
}

round_any.numeric <- function(x, accuracy, f = round) {
f(x / accuracy) * accuracy
}

round_any.POSIXct <- function(x, accuracy, f = round) {
tz <- format(x[1], "%Z")
xr <- round_any(as.numeric(x), accuracy, f)
as.POSIXct(xr, origin = "1970-01-01 00:00.00 UTC", tz = tz)
}
25 changes: 25 additions & 0 deletions R/round_any.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#' Round to multiple of any number.
#
#' @param x numeric or date-time (POSIXct) vector to round
#' @param accuracy number to round to; for POSIXct objects, a number of seconds
#' @param f rounding function: \code{\link{floor}}, \code{\link{ceiling}} or
#' \code{\link{round}}
#' @source adapted from `plyr`
#' @export
#' @examples
#' round_any(1.865, accuracy = .25)
round_any <- function(x, accuracy, f = round) {
UseMethod("round_any")
}

#' @export
round_any.numeric <- function(x, accuracy, f = round) {
f(x / accuracy) * accuracy
}

#' @export
round_any.POSIXct <- function(x, accuracy, f = round) {
tz <- format(x[1], "%Z")
xr <- round_any(as.numeric(x), accuracy, f)
as.POSIXct(xr, origin = "1970-01-01 00:00.00 UTC", tz = tz)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ reference:
- augment_chisq_add_phi
- label_number_abs
- weighted.median
- round_any
25 changes: 25 additions & 0 deletions man/round_any.Rd

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

0 comments on commit 139bb3e

Please sign in to comment.