| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/helpers.R |
| 3 |
\name{keep_units} |
| 4 |
\alias{keep_units} |
| 5 |
\title{Apply a function keeping units} |
| 6 |
\usage{ |
| 7 |
keep_units(FUN, x, ..., unit = units(x)) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{FUN}{the function to be applied.} |
| 11 |
|
| 12 |
\item{x}{first argument of \code{FUN}, of class \code{units}.} |
| 13 |
|
| 14 |
\item{...}{optional arguments to \code{FUN}.} |
| 15 |
|
| 16 |
\item{unit}{symbolic unit to restore after \code{FUN}.} |
| 17 |
} |
| 18 |
\value{ |
| 19 |
An object of class \code{units}. |
| 20 |
} |
| 21 |
\description{ |
| 22 |
Helper function to apply a function to a \code{units} object and then restore |
| 23 |
the original units. |
| 24 |
} |
| 25 |
\details{ |
| 26 |
Provided for incompatible functions that do not preserve units. The user is |
| 27 |
responsible for ensuring the correctness of the output. |
| 28 |
|
| 29 |
If \code{x} is not a \code{units} object |
| 30 |
and \code{unit} is not provided by the user, |
| 31 |
a warning is issued, and the output will also have no units |
| 32 |
(see examples). |
| 33 |
} |
| 34 |
\examples{ |
| 35 |
x <- set_units(1:5, m) |
| 36 |
keep_units(drop_units, x) |
| 37 |
|
| 38 |
# An example use case is with random number generating functions: |
| 39 |
mu <- as_units(10, "years") |
| 40 |
keep_units(rnorm, n = 1, x = mu) |
| 41 |
|
| 42 |
# units can be directly specified if needed; for example, with |
| 43 |
# `rexp()`, the units of the rate parameter are the inverse of |
| 44 |
# the units of the output: |
| 45 |
rate <- as_units(3, "1/year") |
| 46 |
keep_units(rexp, n = 1, x = rate, unit = units(1/rate)) |
| 47 |
|
| 48 |
# if `x` does not actually have units, a warning is issued, |
| 49 |
# and the output has no units: |
| 50 |
rate2 <- 3 |
| 51 |
keep_units(rexp, n = 1, x = rate2) |
| 52 |
|
| 53 |
} |