diff --git a/DESCRIPTION b/DESCRIPTION index 2e528f4..6bd66cc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/NAMESPACE b/NAMESPACE index 8732a1c..4256051 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/R/geom_stripped_rows.R b/R/geom_stripped_rows.R index e57488c..d5071ca 100644 --- a/R/geom_stripped_rows.R +++ b/R/geom_stripped_rows.R @@ -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) -} diff --git a/R/round_any.R b/R/round_any.R new file mode 100644 index 0000000..d2bf3dd --- /dev/null +++ b/R/round_any.R @@ -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) +} diff --git a/_pkgdown.yml b/_pkgdown.yml index 8df0a54..a7dab39 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -42,3 +42,4 @@ reference: - augment_chisq_add_phi - label_number_abs - weighted.median + - round_any diff --git a/man/round_any.Rd b/man/round_any.Rd new file mode 100644 index 0000000..8d807cf --- /dev/null +++ b/man/round_any.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/round_any.R +\name{round_any} +\alias{round_any} +\title{Round to multiple of any number.} +\source{ +adapted from \code{plyr} +} +\usage{ +round_any(x, accuracy, f = round) +} +\arguments{ +\item{x}{numeric or date-time (POSIXct) vector to round} + +\item{accuracy}{number to round to; for POSIXct objects, a number of seconds} + +\item{f}{rounding function: \code{\link{floor}}, \code{\link{ceiling}} or +\code{\link{round}}} +} +\description{ +Round to multiple of any number. +} +\examples{ +round_any(1.865, accuracy = .25) +}