prettyunits/ 0000755 0001762 0000144 00000000000 14504122652 012665 5 ustar ligges users prettyunits/NAMESPACE 0000644 0001762 0000144 00000000607 14504004735 014110 0 ustar ligges users # Generated by roxygen2: do not edit by hand
export(compute_bytes)
export(compute_num)
export(pretty_bytes)
export(pretty_color)
export(pretty_colour)
export(pretty_dt)
export(pretty_ms)
export(pretty_num)
export(pretty_p_value)
export(pretty_round)
export(pretty_sec)
export(pretty_signif)
export(time_ago)
export(vague_dt)
importFrom(grDevices,col2rgb)
importFrom(grDevices,convertColor)
prettyunits/LICENSE 0000644 0001762 0000144 00000000052 14253066262 013674 0 ustar ligges users YEAR: 2014
COPYRIGHT HOLDER: Gabor Csardi
prettyunits/README.md 0000644 0001762 0000144 00000017136 14504004233 014146 0 ustar ligges users
[](https://github.com/r-lib/prettyunits/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/r-lib/prettyunits?branch=main)
[](https://CRAN.R-project.org/package=prettyunits)
# prettyunits
The `prettyunits` package formats quantities in human readable form.
* Time intervals: '1337000' -> '15d 11h 23m 20s'.
* Vague time intervals: '2674000' -> 'about a month ago'.
* Bytes: '1337' -> '1.34 kB'.
* Rounding: '99' with 3 significant digits -> '99.0'
* p-values: '0.00001' -> '<0.0001'.
* Colors: '#FF0000' -> 'red'.
* Quantities: '1239437' -> '1.24 M'.
## Installation
You can install the package from CRAN:
```r
install.packages("prettyunits")
```
## Bytes
`pretty_bytes` formats number of bytes in a human readable way:
```r
pretty_bytes(1337)
```
```
##> [1] "1.34 kB"
```
```r
pretty_bytes(133337)
```
```
##> [1] "133.34 kB"
```
```r
pretty_bytes(13333337)
```
```
##> [1] "13.33 MB"
```
```r
pretty_bytes(1333333337)
```
```
##> [1] "1.33 GB"
```
```r
pretty_bytes(133333333337)
```
```
##> [1] "133.33 GB"
```
Here is a simple function that emulates the Unix `ls` command, with
nicely formatted file sizes:
```r
uls <- function(path = ".") {
files <- dir(path)
info <- files %>%
lapply(file.info) %>%
do.call(what = rbind)
info$size <- pretty_bytes(info$size)
df <- data.frame(d = ifelse(info$isdir, "d", " "),
mode = as.character(info$mode), user = info$uname, group = info$grname,
size = ifelse(info$isdir, "", info$size), modified = info$mtime, name = files)
print(df, row.names = FALSE)
}
uls()
```
```
##> d mode user group size modified name
##> 644 gaborcsardi staff 232 B 2023-09-24 10:37:41 codecov.yml
##> d 755 gaborcsardi staff 2023-09-24 10:37:41 data-raw
##> 644 gaborcsardi staff 1.06 kB 2023-09-24 10:40:32 DESCRIPTION
##> 644 gaborcsardi staff 42 B 2022-06-17 13:59:46 LICENSE
##> 644 gaborcsardi staff 111 B 2023-09-23 16:44:21 Makefile
##> d 755 gaborcsardi staff 2023-09-24 10:37:59 man
##> 644 gaborcsardi staff 523 B 2023-09-24 10:39:58 NAMESPACE
##> 644 gaborcsardi staff 1.46 kB 2023-09-24 10:42:01 NEWS.md
##> d 755 gaborcsardi staff 2023-09-24 11:25:00 R
##> 644 gaborcsardi staff 7.90 kB 2023-09-24 11:27:42 README.md
##> 644 gaborcsardi staff 4.31 kB 2023-09-24 11:28:23 README.Rmd
##> d 755 gaborcsardi staff 2022-06-17 13:59:46 tests
```
## Quantities
`pretty_num` formats number related to linear quantities in a human readable way:
```r
pretty_num(1337)
```
```
##> [1] "1.34 k"
```
```r
pretty_num(-133337)
```
```
##> [1] "-133.34 k"
```
```r
pretty_num(1333.37e-9)
```
```
##> [1] "1.33 u"
```
Be aware that the result is wrong in case of surface or volumes, and for any non-linear quantity.
Here is a simple example of how to prettify a entire tibble
```r
library(tidyverse)
```
```
##> ── Attaching core tidyverse packages ─────────────────────────────────────────────────────────────────────────── tidyverse 2.0.0 ──
##> ✔ dplyr 1.1.2 ✔ readr 2.1.4
##> ✔ forcats 1.0.0 ✔ stringr 1.5.0
##> ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
##> ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
##> ✔ purrr 1.0.1
##> ── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
##> ✖ tidyr::extract() masks magrittr::extract()
##> ✖ dplyr::filter() masks stats::filter()
##> ✖ dplyr::lag() masks stats::lag()
##> ✖ purrr::set_names() masks magrittr::set_names()
##> ℹ Use the conflicted package () to force all conflicts to become errors
```
```r
tdf <- tribble( ~name, ~`size in m`, ~`speed in m/s`,
"land snail", 0.075, 0.001,
"photon", NA, 299792458,
"African plate", 10546330, 0.000000000681)
tdf %>% mutate(across(where(is.numeric), pretty_num))
```
```
##> # A tibble: 3 × 3
##> name `size in m` `speed in m/s`
##>
##> 1 land snail " 75 m" " 1 m"
##> 2 photon " NA " "299.79 M"
##> 3 African plate "10.55 M" " 681 p"
```
## Time intervals
`pretty_ms` formats a time interval given in milliseconds. `pretty_sec` does
the same for seconds, and `pretty_dt` for `difftime` objects. The optional
`compact` argument turns on a compact, approximate format.
```r
pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000))
```
```
##> [1] "1.3s" "13.4s" "2m 13.7s" "22m 17s"
##> [5] "15d 11h 23m 20s"
```
```r
pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000),
compact = TRUE)
```
```
##> [1] "~1.3s" "~13.4s" "~2m" "~22m" "~15d"
```
```r
pretty_sec(c(1337, 13370, 133700, 1337000, 13370000))
```
```
##> [1] "22m 17s" "3h 42m 50s" "1d 13h 8m 20s" "15d 11h 23m 20s"
##> [5] "154d 17h 53m 20s"
```
```r
pretty_sec(c(1337, 13370, 133700, 1337000, 13370000),
compact = TRUE)
```
```
##> [1] "~22m" "~3h" "~1d" "~15d" "~154d"
```
## Vague time intervals
`vague_dt` and `time_ago` formats time intervals using a vague format,
omitting smaller units. They both have three formats: `default`, `short` and `terse`.
`vague_dt` takes a `difftime` object, and `time_ago` works relatively to the
specified date.
```r
vague_dt(format = "short", as.difftime(30, units = "secs"))
```
```
##> [1] "<1 min"
```
```r
vague_dt(format = "short", as.difftime(14, units = "mins"))
```
```
##> [1] "14 min"
```
```r
vague_dt(format = "short", as.difftime(5, units = "hours"))
```
```
##> [1] "5 hours"
```
```r
vague_dt(format = "short", as.difftime(25, units = "hours"))
```
```
##> [1] "1 day"
```
```r
vague_dt(format = "short", as.difftime(5, units = "days"))
```
```
##> [1] "5 day"
```
```r
now <- Sys.time()
time_ago(now)
```
```
##> [1] "moments ago"
```
```r
time_ago(now - as.difftime(30, units = "secs"))
```
```
##> [1] "less than a minute ago"
```
```r
time_ago(now - as.difftime(14, units = "mins"))
```
```
##> [1] "14 minutes ago"
```
```r
time_ago(now - as.difftime(5, units = "hours"))
```
```
##> [1] "5 hours ago"
```
```r
time_ago(now - as.difftime(25, units = "hours"))
```
```
##> [1] "a day ago"
```
## Rounding
`pretty_round()` and `pretty_signif()` preserve trailing zeros.
```r
pretty_round(1, digits=6)
```
```
##> [1] "1.000000"
```
```r
pretty_signif(c(99, 0.9999), digits=3)
```
```
##> [1] "99.0" "1.00"
```
## p-values
`pretty_p_value()` rounds small p-values to indicate less than significance
level for small values.
```r
pretty_p_value(c(0.05, 0.0000001, NA))
```
```
##> [1] "0.0500" "<0.0001" NA
```
## Colors
`pretty_color` converts colors from other representations to human-readable
names.
```r
pretty_color("black")
```
```
##> [1] "black"
##> attr(,"alt")
##> [1] "black" "gray0" "grey0" "Black"
```
```r
pretty_color("#123456")
```
```
##> [1] "Prussian Blue"
##> attr(,"alt")
##> [1] "Prussian Blue"
```
prettyunits/man/ 0000755 0001762 0000144 00000000000 14504004233 013432 5 ustar ligges users prettyunits/man/time_ago.Rd 0000644 0001762 0000144 00000004327 14503574705 015532 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/xtime-ago.R
\name{time_ago}
\alias{time_ago}
\title{Human readable format of the time interval since a time point}
\usage{
time_ago(date, format = c("default", "short", "terse"))
}
\arguments{
\item{date}{Date(s), \code{as.POSIXct} will be called on them.}
\item{format}{Format, currently available formats are:
\sQuote{default}, \sQuote{short}, \sQuote{terse}. See examples below.}
}
\value{
Character vector of the formatted time intervals.
}
\description{
It calls \code{\link{vague_dt}} to do the actual formatting.
}
\examples{
now <- Sys.time()
time_ago(now)
time_ago(now - as.difftime(30, units = "secs"))
time_ago(now - as.difftime(14, units = "mins"))
time_ago(now - as.difftime(5, units = "hours"))
time_ago(now - as.difftime(25, units = "hours"))
time_ago(now - as.difftime(5, units = "days"))
time_ago(now - as.difftime(30, units = "days"))
time_ago(now - as.difftime(365, units = "days"))
time_ago(now - as.difftime(365 * 10, units = "days"))
## Short format
time_ago(format = "short", now)
time_ago(format = "short", now - as.difftime(30, units = "secs"))
time_ago(format = "short", now - as.difftime(14, units = "mins"))
time_ago(format = "short", now - as.difftime(5, units = "hours"))
time_ago(format = "short", now - as.difftime(25, units = "hours"))
time_ago(format = "short", now - as.difftime(5, units = "days"))
time_ago(format = "short", now - as.difftime(30, units = "days"))
time_ago(format = "short", now - as.difftime(365, units = "days"))
time_ago(format = "short", now - as.difftime(365 * 10, units = "days"))
## Even shorter, terse format, (almost always) exactly 3 characters wide
time_ago(format = "terse", now)
time_ago(format = "terse", now - as.difftime(30, units = "secs"))
time_ago(format = "terse", now - as.difftime(14, units = "mins"))
time_ago(format = "terse", now - as.difftime(5, units = "hours"))
time_ago(format = "terse", now - as.difftime(25, units = "hours"))
time_ago(format = "terse", now - as.difftime(5, units = "days"))
time_ago(format = "terse", now - as.difftime(30, units = "days"))
time_ago(format = "terse", now - as.difftime(365, units = "days"))
time_ago(format = "terse", now - as.difftime(365 * 10, units = "days"))
}
prettyunits/man/pretty_p_value.Rd 0000644 0001762 0000144 00000001116 14504001530 016757 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/p-value.R
\name{pretty_p_value}
\alias{pretty_p_value}
\title{p-values in a human-readable string}
\usage{
pretty_p_value(x, minval = 1e-04)
}
\arguments{
\item{x}{A numeric vector.}
\item{minval}{The minimum p-value to show (lower values will show as
\code{paste0("<", minval)}).}
}
\value{
A character vector of p-value representations.
}
\description{
p-values in a human-readable string
}
\examples{
pretty_p_value(c(1, 0, NA, 0.01, 0.0000001))
pretty_p_value(c(1, 0, NA, 0.01, 0.0000001), minval = 0.05)
}
prettyunits/man/vague_dt.Rd 0000644 0001762 0000144 00000003666 14503574705 015551 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/xtime-ago.R
\name{vague_dt}
\alias{vague_dt}
\title{Human readable format of a time interval}
\usage{
vague_dt(dt, format = c("default", "short", "terse"))
}
\arguments{
\item{dt}{A \code{difftime} object, the time interval(s).}
\item{format}{Format, currently available formats are:
\sQuote{default}, \sQuote{short}, \sQuote{terse}. See examples below.}
}
\value{
Character vector of the formatted time intervals.
}
\description{
Human readable format of a time interval
}
\examples{
vague_dt(as.difftime(30, units = "secs"))
vague_dt(as.difftime(14, units = "mins"))
vague_dt(as.difftime(5, units = "hours"))
vague_dt(as.difftime(25, units = "hours"))
vague_dt(as.difftime(5, units = "days"))
vague_dt(as.difftime(30, units = "days"))
vague_dt(as.difftime(365, units = "days"))
vague_dt(as.difftime(365 * 10, units = "days"))
## Short format
vague_dt(format = "short", as.difftime(30, units = "secs"))
vague_dt(format = "short", as.difftime(14, units = "mins"))
vague_dt(format = "short", as.difftime(5, units = "hours"))
vague_dt(format = "short", as.difftime(25, units = "hours"))
vague_dt(format = "short", as.difftime(5, units = "days"))
vague_dt(format = "short", as.difftime(30, units = "days"))
vague_dt(format = "short", as.difftime(365, units = "days"))
vague_dt(format = "short", as.difftime(365 * 10, units = "days"))
## Even shorter, terse format, (almost always) exactly 3 characters wide
vague_dt(format = "terse", as.difftime(30, units = "secs"))
vague_dt(format = "terse", as.difftime(14, units = "mins"))
vague_dt(format = "terse", as.difftime(5, units = "hours"))
vague_dt(format = "terse", as.difftime(25, units = "hours"))
vague_dt(format = "terse", as.difftime(5, units = "days"))
vague_dt(format = "terse", as.difftime(30, units = "days"))
vague_dt(format = "terse", as.difftime(365, units = "days"))
vague_dt(format = "terse", as.difftime(365 * 10, units = "days"))
}
prettyunits/man/pretty_dt.Rd 0000644 0001762 0000144 00000001314 14503574705 015755 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/xtime.R
\name{pretty_dt}
\alias{pretty_dt}
\title{Pretty formatting of time intervals (difftime objects)}
\usage{
pretty_dt(dt, compact = FALSE)
}
\arguments{
\item{dt}{A \code{difftime} object, a vector of time
differences.}
\item{compact}{If true, then only the first non-zero
unit is used. See examples below.}
}
\value{
Character vector of formatted time intervals.
}
\description{
Pretty formatting of time intervals (difftime objects)
}
\examples{
pretty_dt(as.difftime(1000, units = "secs"))
pretty_dt(as.difftime(0, units = "secs"))
}
\seealso{
Other time:
\code{\link{pretty_ms}()},
\code{\link{pretty_sec}()}
}
\concept{time}
prettyunits/man/pretty_round.Rd 0000644 0001762 0000144 00000002160 14504004735 016465 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/rounding.R
\name{pretty_round}
\alias{pretty_round}
\title{Round a value to a defined number of digits printing out trailing zeros, if
applicable}
\usage{
pretty_round(x, digits = 0, sci_range = Inf, sci_sep = "e")
}
\arguments{
\item{x}{The number to round.}
\item{digits}{integer indicating the number of decimal places.}
\item{sci_range}{See help for \code{\link[=pretty_signif]{pretty_signif()}} (and you likely want
to round with \code{\link[=pretty_signif]{pretty_signif()}} if you want to use this argument).}
\item{sci_sep}{The separator to use for scientific notation strings
(typically this will be either "e" or "x10^" for computer- or
human-readable output).}
}
\value{
A string with the value.
}
\description{
Round a value to a defined number of digits printing out trailing zeros, if
applicable
}
\details{
Values that are not standard numbers like \code{Inf}, \code{NA}, and
\code{NaN} are returned as \code{"Inf"}, \code{"NA"}, and \code{"NaN"}.
}
\seealso{
\code{\link[=round]{round()}}, \code{\link[=pretty_signif]{pretty_signif()}}.
}
prettyunits/man/pretty_signif.Rd 0000644 0001762 0000144 00000002353 14504004735 016621 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/rounding.R
\name{pretty_signif}
\alias{pretty_signif}
\title{Round a value to a defined number of significant digits printing out trailing
zeros, if applicable}
\usage{
pretty_signif(x, digits = 6, sci_range = 6, sci_sep = "e")
}
\arguments{
\item{x}{The number to round.}
\item{digits}{integer indicating the number of significant digits.}
\item{sci_range}{integer (or \code{Inf}) indicating when to switch to
scientific notation instead of floating point. Zero indicates always use
scientific; \code{Inf} indicates to never use scientific notation;
otherwise, scientific notation is used when \code{abs(log10(x)) > sci_range}.}
\item{sci_sep}{The separator to use for scientific notation strings
(typically this will be either "e" or "x10^" for computer- or
human-readable output).}
}
\value{
A string with the value.
}
\description{
Round a value to a defined number of significant digits printing out trailing
zeros, if applicable
}
\details{
Values that are not standard numbers like \code{Inf}, \code{NA}, and
\code{NaN} are returned as \code{"Inf"}, \code{"NA"}, and \code{NaN}.
}
\seealso{
\code{\link[=signif]{signif()}}, \code{\link[=pretty_round]{pretty_round()}}.
}
prettyunits/man/pretty_ms.Rd 0000644 0001762 0000144 00000001273 14503574705 015771 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/xtime.R
\name{pretty_ms}
\alias{pretty_ms}
\title{Pretty formatting of milliseconds}
\usage{
pretty_ms(ms, compact = FALSE)
}
\arguments{
\item{ms}{Numeric vector of milliseconds}
\item{compact}{If true, then only the first non-zero
unit is used. See examples below.}
}
\value{
Character vector of formatted time intervals.
}
\description{
Pretty formatting of milliseconds
}
\examples{
pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000))
pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000),
compact = TRUE)
}
\seealso{
Other time:
\code{\link{pretty_dt}()},
\code{\link{pretty_sec}()}
}
\concept{time}
prettyunits/man/pretty_bytes.Rd 0000644 0001762 0000144 00000002521 14504004233 016456 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/xsizes-docs.R
\name{pretty_bytes}
\alias{pretty_bytes}
\alias{compute_bytes}
\title{Bytes in a human readable string}
\usage{
pretty_bytes(bytes, style = c("default", "nopad", "6"))
compute_bytes(bytes, smallest_unit = "B")
}
\arguments{
\item{bytes}{Numeric vector, number of bytes.}
\item{style}{Formatting style:
\itemize{
\item \code{"default"} is the original \code{pretty_bytes} formatting, and it always
pads the output, so that all vector elements are of the same width,
\item \code{"nopad"} is similar, but does not pad the output,
\item \code{"6"} always uses 6 characters,
The \code{"6"} style is useful if it is important that the output always
has the same width (number of characters), e.g. in progress bars.
See some examples below.
}}
\item{smallest_unit}{A character scalar, the smallest unit to use.}
}
\value{
Character vector, the formatted sizes.
For \code{compute_bytes}, a data frame with columns \code{amount}, \code{unit},
\code{negative}.
}
\description{
Use \code{pretty_bytes()} to format bytes. \code{compute_bytes()} is the underlying
engine that may be useful for custom formatting.
}
\examples{
bytes <- c(1337, 133337, 13333337, 1333333337, 133333333337)
pretty_bytes(bytes)
pretty_bytes(bytes, style = "nopad")
pretty_bytes(bytes, style = "6")
}
prettyunits/man/color_reference.Rd 0000644 0001762 0000144 00000001236 14504001530 017054 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/color.R
\name{color_reference}
\alias{color_reference}
\title{Color names, hexadecimal, and CIE Lab colorspace representations}
\source{
{https://github.com/colorjs/color-namer} and R \code{colors()}
}
\description{
\describe{
\item{hex}{hexadecimal color representation (without the # at the beginning)}
\item{L,a,b}{CIE Lab colorspace representation of \code{hex}}
\item{name}{Preferred human-readable name of the color}
\item{name_alt}{All available human-readable names of the color}
\item{roygbiv,basic,html,R,pantone,x11,ntc}{Source dataset containing the color}
}
}
\keyword{internal}
prettyunits/man/pretty_sec.Rd 0000644 0001762 0000144 00000001260 14503574705 016120 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/xtime.R
\name{pretty_sec}
\alias{pretty_sec}
\title{Pretty formatting of seconds}
\usage{
pretty_sec(sec, compact = FALSE)
}
\arguments{
\item{sec}{Numeric vector of seconds.}
\item{compact}{If true, then only the first non-zero
unit is used. See examples below.}
}
\value{
Character vector of formatted time intervals.
}
\description{
Pretty formatting of seconds
}
\examples{
pretty_sec(c(1337, 13370, 133700, 1337000, 13370000))
pretty_sec(c(1337, 13370, 133700, 1337000, 13370000),
compact = TRUE)
}
\seealso{
Other time:
\code{\link{pretty_dt}()},
\code{\link{pretty_ms}()}
}
\concept{time}
prettyunits/man/pretty_color.Rd 0000644 0001762 0000144 00000001115 14504001530 016441 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/color.R
\name{pretty_color}
\alias{pretty_color}
\alias{pretty_colour}
\title{Color definition (like RGB) to a name}
\usage{
pretty_color(color)
pretty_colour(color)
}
\arguments{
\item{color}{A scalar color that is usable as an input to \code{col2rgb()}
(assumed to be in the sRGB color space).}
}
\value{
A character string that is the closest named colors to the input
color. The output will have an attribute of alternate color names (named
"alt").
}
\description{
Color definition (like RGB) to a name
}
prettyunits/man/pretty_num.Rd 0000644 0001762 0000144 00000002562 14504004233 016134 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/xnumbers-docs.R
\name{pretty_num}
\alias{pretty_num}
\alias{compute_num}
\title{Linear quantities in a human readable string}
\usage{
pretty_num(number, style = c("default", "nopad", "6"))
compute_num(number, smallest_prefix = "y")
}
\arguments{
\item{number}{Numeric vector, number related to a linear quantity.}
\item{style}{Formatting style:
\itemize{
\item \code{"default"} is the original \code{pretty_num} formatting, and it always
pads the output, so that all vector elements are of the same width,
\item \code{"nopad"} is similar, but does not pad the output,
\item \code{"6"} always uses 6 characters,
The \code{"6"} style is useful if it is important that the output always
has the same width (number of characters), e.g. in progress bars.
See some examples below.
}}
\item{smallest_prefix}{A character scalar, the smallest prefix to use.}
}
\value{
Character vector, the formatted sizes.
For \code{compute_num}, a data frame with columns \code{amount}, \code{prefix},
\code{negative}.
}
\description{
Use \code{pretty_num()} to format numbers \code{compute_num()} is the underlying
engine that may be useful for custom formatting.
}
\examples{
numbers <- c(1337, 1.3333e-5, 13333337, 1333333337, 133333333337)
pretty_num(numbers)
pretty_num(numbers, style = "nopad")
pretty_num(numbers, style = "6")
}
prettyunits/man/prettyunits.Rd 0000644 0001762 0000144 00000002031 14504005772 016340 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/pretty-package.R
\docType{package}
\name{prettyunits}
\alias{prettyunits}
\alias{prettyunits-package}
\title{Prettier formatting of quantities}
\description{
Render quantities with a pretty, human-readable formatting.
\itemize{
\item Time intervals: '1337000' -> '15d 11h 23m 20s'.
\item Vague time intervals: '2674000' -> 'about a month ago'.
\item Bytes: '1337' -> '1.34 kB'.
\item p-values: '0.00001' -> '<0.0001'.
\item Colors: '#FF0000' -> 'red'.
\item Quantities: '1239437' -> '1.24 M'.
}
}
\seealso{
Useful links:
\itemize{
\item \url{https://github.com/r-lib/prettyunits}
\item Report bugs at \url{https://github.com/r-lib/prettyunits/issues}
}
}
\author{
\strong{Maintainer}: Gabor Csardi \email{csardi.gabor@gmail.com}
Other contributors:
\itemize{
\item Bill Denney \email{wdenney@humanpredictions.com} (\href{https://orcid.org/0000-0002-5759-428X}{ORCID}) [contributor]
\item Christophe Regouby \email{christophe.regouby@free.fr} [contributor]
}
}
prettyunits/DESCRIPTION 0000644 0001762 0000144 00000002405 14504122652 014374 0 ustar ligges users Package: prettyunits
Title: Pretty, Human Readable Formatting of Quantities
Version: 1.2.0
Authors@R: c(
person("Gabor", "Csardi", email="csardi.gabor@gmail.com", role=c("aut", "cre")),
person("Bill", "Denney", email="wdenney@humanpredictions.com", role=c("ctb"), comment=c(ORCID="0000-0002-5759-428X")),
person("Christophe", "Regouby", email="christophe.regouby@free.fr", role=c("ctb"))
)
Description: Pretty, human readable formatting of quantities.
Time intervals: '1337000' -> '15d 11h 23m 20s'.
Vague time intervals: '2674000' -> 'about a month ago'.
Bytes: '1337' -> '1.34 kB'.
Rounding: '99' with 3 significant digits -> '99.0'
p-values: '0.00001' -> '<0.0001'.
Colors: '#FF0000' -> 'red'.
Quantities: '1239437' -> '1.24 M'.
License: MIT + file LICENSE
URL: https://github.com/r-lib/prettyunits
BugReports: https://github.com/r-lib/prettyunits/issues
Depends: R(>= 2.10)
Suggests: codetools, covr, testthat
RoxygenNote: 7.2.3
Encoding: UTF-8
NeedsCompilation: no
Packaged: 2023-09-24 10:53:19 UTC; gaborcsardi
Author: Gabor Csardi [aut, cre],
Bill Denney [ctb] (),
Christophe Regouby [ctb]
Maintainer: Gabor Csardi
Repository: CRAN
Date/Publication: 2023-09-24 21:10:02 UTC
prettyunits/tests/ 0000755 0001762 0000144 00000000000 14253066262 014034 5 ustar ligges users prettyunits/tests/testthat/ 0000755 0001762 0000144 00000000000 14504122652 015667 5 ustar ligges users prettyunits/tests/testthat/test-vague-dt.r 0000644 0001762 0000144 00000004205 14503574705 020555 0 ustar ligges users
context("Vague time intervals")
## Amount, unit, default result, short, terse
all_tests <- list(
list( 1, "secs", "moments ago", "<1 min", " 1s"),
list( 30, "secs", "less than a minute ago", "<1 min", "30s"),
list( 50, "secs", "about a minute ago", "1 min", " 1m"),
list( 14, "mins", "14 minutes ago", "14 min", "14m"),
list( 70, "mins", "about an hour ago", "1 hour", " 1h"),
list( 5, "hours", "5 hours ago", "5 hours", " 5h"),
list( 25, "hours", "a day ago", "1 day", " 1d"),
list( 5, "days", "5 days ago", "5 day", " 5d"),
list( 30, "days", "about a month ago", "1 mon", " 1M"),
list( 30 * 3, "days", "3 months ago", "3 mon", " 3M"),
list(365, "days", "about a year ago", "1 year", " 1y"),
list(365 * 10, "days", "10 years ago", "10 years", "10y")
)
test_that("vague_dt works", {
sapply(all_tests, function(case) {
dt <- as.difftime(case[[1]], units = case[[2]])
default <- vague_dt(dt)
short <- vague_dt(dt, format = "short")
terse <- vague_dt(dt, format = "terse")
expect_equal(default, case[[3]], info = paste(case[[1]], case[[2]], "default"))
expect_equal(short, case[[4]], info = paste(case[[1]], case[[2]], "short"))
expect_equal(terse, case[[5]], info = paste(case[[1]], case[[2]], "terse"))
})
expect_equal(
vague_dt(as.difftime(numeric(), units = "secs")),
character()
)
})
test_that("time_ago works", {
sapply(all_tests, function(case) {
t <- Sys.time() - as.difftime(case[[1]], units = case[[2]])
default <- time_ago(t)
short <- time_ago(t, format = "short")
terse <- time_ago(t, format = "terse")
expect_equal(default, case[[3]], info = paste(case[[1]], case[[2]], "default"))
expect_equal(short, case[[4]], info = paste(case[[1]], case[[2]], "short"))
expect_equal(terse, case[[5]], info = paste(case[[1]], case[[2]], "terse"))
})
expect_equal(
time_ago(c(Sys.time() - 60, Sys.time() - 3600)),
c("about a minute ago", "about an hour ago")
)
})
prettyunits/tests/testthat/test-numbers.r 0000644 0001762 0000144 00000011162 14504004233 020475 0 ustar ligges users
context("Pretty numbers")
test_that("sizes.R is standalone", {
stenv <- environment(format_num$pretty_num)
objs <- ls(stenv, all.names = TRUE)
funs <- Filter(function(x) is.function(stenv[[x]]), objs)
funobjs <- mget(funs, stenv)
for (f in funobjs) expect_identical(environmentName(topenv(f)), "base")
expect_message(
mapply(codetools::checkUsage, funobjs, funs,
MoreArgs = list(report = message)),
NA)
})
test_that("pretty_num gives errors on invalid input", {
expect_error(pretty_num(''), 'is.numeric.*is not TRUE')
expect_error(pretty_num('1'), 'is.numeric.*is not TRUE')
expect_error(pretty_num(TRUE), 'is.numeric.*is not TRUE')
expect_error(pretty_num(list(1,2,3)), 'is.numeric.*is not TRUE')
})
test_that("pretty_num converts properly", {
expect_equal(pretty_num(1e-24), '1 y')
expect_equal(pretty_num(-1e-4), '-100.00 u')
expect_equal(pretty_num(-0.01), '-10 m')
expect_equal(pretty_num(0), '0 ')
expect_equal(pretty_num(10), '10 ')
expect_equal(pretty_num(999), '999 ')
expect_equal(pretty_num(1001), '1.00 k')
expect_equal(pretty_num(1000 * 1000 - 1), '1.00 M')
expect_equal(pretty_num(1e16), '10 P')
expect_equal(pretty_num(1e30), '1000000 Y')
})
test_that("pretty_num handles NA and NaN", {
expect_equal(pretty_num(NA_real_), "NA ")
expect_equal(pretty_num(NA_integer_), "NA ")
expect_error(pretty_num(NA_character_), 'is.numeric.*is not TRUE')
expect_error(pretty_num(NA), 'is.numeric.*is not TRUE')
expect_equal(pretty_num(NaN), "NaN ")
})
test_that("pretty_num handles vectors", {
expect_equal(pretty_num(1:10), paste(format(1:10), ""))
v <- c(NA, -1e-7, 1, 1e4, 1e6, NaN, 1e5)
expect_equal(pretty_num(v),
c(" NA ", "-100.00 n"," 1 ", " 10 k", " 1 M", " NaN ", " 100 k"))
expect_equal(pretty_num(numeric()), character())
})
test_that("pretty_num nopad style", {
v <- c(NA, 1, 1e4, 1e6, NaN, 1e5)
expect_equal(pretty_num(v, style = "nopad"),
c("NA ", "1 ", "10 k", "1 M", "NaN ", "100 k"))
expect_equal(pretty_num(numeric(), style = "nopad"), character())
})
test_that("pretty_num handles negative values", {
v <- c(NA, -1, 1e4, 1e6, NaN, -1e5)
expect_equal(pretty_num(v),
c(" NA ", " -1 ", " 10 k", " 1 M", " NaN ", "-100 k"))
})
test_that("always two fraction digits", {
expect_equal(
pretty_num(c(5.6, 5, NA) * 1000 * 1000),
c("5.60 M", " 5 M", " NA ")
)
})
test_that("6 width style", {
cases <- c(
" -10 k" = -1e4, # 1
"-111 " = -111.33333, # 2
"-100 " = -100, # 3
" -10 " = -10.33333, # 4
" -10 " = -9.99999, # 5
"-9.0 " = -9, # 6
"-1.0 " = -1, # 7
"0.00 " = 0, # 8
"1.00 " = 1, # 9
"9.00 " = 9, # 10
"10.0 " = 9.99999, # 11
"10.3 " = 10.33333, # 12
" 100 " = 100, # 13
" 111 " = 111.33333, # 14
"1.00 k" = 1e3, # 15
"1.05 k" = 1049, # 16
"1.05 k" = 1051, # 17
"1.10 k" = 1100, # 18
"10.0 k" = 1e4, # 19
" 100 k" = 1e5, # 20
"1.00 M" = 1e6, # 21
" NaN " = NaN, # 22
" NA " = NA # 23
)
expect_equal(pretty_num(unname(cases), style = "6"), names(cases))
})
test_that("No fractional bytes (#23)", {
cases <- c(
" -1 " = -1, # 1
" 1 " = 1, # 2
" 16 " = 16, # 3
" 128 " = 128, # 4
" 1.02 k" = 1024, # 5
"16.38 k" = 16384, # 6
" 1.05 M" = 1048576, # 7
"-1.05 M" = -1048576, # 8
" NA " = NA # 9
)
expect_equal(pretty_num(unname(cases)), names(cases))
})
test_that("compute_num handles `smallest_prefix` properly", {
expect_equal(compute_num(1e-24, smallest_prefix = "m"), data.frame(amount = 1e-21, prefix = "m", negative = FALSE, stringsAsFactors = FALSE))
expect_equal(compute_num(-1e-4, smallest_prefix = "m"), data.frame(amount = 0.1, prefix = "m", negative = TRUE, stringsAsFactors = FALSE))
expect_equal(compute_num(-0.01, smallest_prefix = "m"), data.frame(amount = 10, prefix = "m", negative = TRUE, stringsAsFactors = FALSE))
expect_equal(compute_num(0, smallest_prefix = "m"), data.frame(amount = 0, prefix = "", negative = FALSE, stringsAsFactors = FALSE))
})
prettyunits/tests/testthat/test-p-value.r 0000644 0001762 0000144 00000001162 14504001530 020367 0 ustar ligges users context("p-values")
test_that("p-values work", {
expect_equal(
pretty_p_value(c(1, 0, NA, 0.01, 0.0000001)),
c("1.0000", "<0.0001", NA_character_, "0.0100", "<0.0001")
)
expect_equal(
pretty_p_value(c(1, 0, NA, 0.01, 0.0000001), minval=0.05),
c("1.00", "<0.05", NA_character_, "<0.05", "<0.05")
)
expect_equal(pretty_p_value(NA_real_), NA_character_)
expect_error(pretty_p_value(1, minval="A"))
expect_error(pretty_p_value("A"))
expect_error(pretty_p_value(1.1))
expect_error(pretty_p_value(-1))
expect_error(pretty_p_value(0.5, minval=0))
expect_error(pretty_p_value(0.5, minval=1))
})
prettyunits/tests/testthat/test-color.r 0000644 0001762 0000144 00000000602 14504001530 020132 0 ustar ligges users context("Pretty color")
test_that("pretty_color works", {
expect_equal(
pretty_color("black"),
structure("black", alt = c("black", "gray0", "grey0", "Black"))
)
expect_equal(
pretty_color("#123456"),
structure("Prussian Blue", alt = c("Prussian Blue"))
)
expect_equal(
pretty_color(NA_character_),
structure(NA_character_, alt = NA_character_)
)
})
prettyunits/tests/testthat/test-bytes.r 0000644 0001762 0000144 00000007402 14504004233 020152 0 ustar ligges users
context("Pretty bytes")
test_that("sizes.R is standalone", {
stenv <- environment(format_bytes$pretty_bytes)
objs <- ls(stenv, all.names = TRUE)
funs <- Filter(function(x) is.function(stenv[[x]]), objs)
funobjs <- mget(funs, stenv)
for (f in funobjs) expect_identical(environmentName(topenv(f)), "base")
expect_message(
mapply(codetools::checkUsage, funobjs, funs,
MoreArgs = list(report = message)),
NA)
})
test_that("pretty_bytes gives errors on invalid input", {
expect_error(pretty_bytes(''), 'is.numeric.*is not TRUE')
expect_error(pretty_bytes('1'), 'is.numeric.*is not TRUE')
expect_error(pretty_bytes(TRUE), 'is.numeric.*is not TRUE')
expect_error(pretty_bytes(list(1,2,3)), 'is.numeric.*is not TRUE')
})
test_that("pretty_bytes converts properly", {
expect_equal(pretty_bytes(0), '0 B')
expect_equal(pretty_bytes(10), '10 B')
expect_equal(pretty_bytes(999), '999 B')
expect_equal(pretty_bytes(1001), '1.00 kB')
expect_equal(pretty_bytes(1000 * 1000 - 1), '1.00 MB')
expect_equal(pretty_bytes(1e16), '10 PB')
expect_equal(pretty_bytes(1e30), '1000000 YB')
})
test_that("pretty_bytes handles NA and NaN", {
expect_equal(pretty_bytes(NA_real_), "NA B")
expect_equal(pretty_bytes(NA_integer_), "NA B")
expect_error(pretty_bytes(NA_character_), 'is.numeric.*is not TRUE')
expect_error(pretty_bytes(NA), 'is.numeric.*is not TRUE')
expect_equal(pretty_bytes(NaN), "NaN B")
})
test_that("pretty_bytes handles vectors", {
expect_equal(pretty_bytes(1:10), paste(format(1:10), "B"))
v <- c(NA, 1, 1e4, 1e6, NaN, 1e5)
expect_equal(pretty_bytes(v),
c(" NA B", " 1 B", " 10 kB", " 1 MB", " NaN B", "100 kB"))
expect_equal(pretty_bytes(numeric()), character())
})
test_that("pretty_bytes nopad style", {
v <- c(NA, 1, 1e4, 1e6, NaN, 1e5)
expect_equal(pretty_bytes(v, style = "nopad"),
c("NA B", "1 B", "10 kB", "1 MB", "NaN B", "100 kB"))
expect_equal(pretty_bytes(numeric(), style = "nopad"), character())
})
test_that("pretty_bytes handles negative values", {
v <- c(NA, -1, 1e4, 1e6, NaN, -1e5)
expect_equal(pretty_bytes(v),
c(" NA B", " -1 B", " 10 kB", " 1 MB", " NaN B", "-100 kB"))
})
test_that("always two fraction digits", {
expect_equal(
pretty_bytes(c(5.6, 5, NA) * 1000 * 1000),
c("5.60 MB", " 5 MB", " NA B")
)
})
test_that("6 width style", {
cases <- c(
"< 0 kB" = -1e4, # 1
"< 0 kB" = -100, # 2
"< 0 kB" = -1, # 3
"0.0 kB" = 0, # 4
"0.0 kB" = 1, # 5
"0.0 kB" = 9, # 6
"0.0 kB" = 9.99999, # 7
"0.0 kB" = 10.33333, # 8
"0.1 kB" = 100, # 9
"0.1 kB" = 111.33333, # 10
"1.0 kB" = 1e3, # 11
"1.0 kB" = 1049, # 12
"1.1 kB" = 1051, # 13
"1.1 kB" = 1100, # 14
" 10 kB" = 1e4, # 15
"100 kB" = 1e5, # 16
"1.0 MB" = 1e6, # 17
"NaN kB" = NaN, # 18
" NA kB" = NA # 19
)
expect_equal(pretty_bytes(unname(cases), style = "6"), names(cases))
})
test_that("No fractional bytes (#23)", {
cases <- c(
" -1 B" = -1, # 1
" 1 B" = 1, # 2
" 16 B" = 16, # 3
" 128 B" = 128, # 4
" 1.02 kB" = 1024, # 5
"16.38 kB" = 16384, # 6
" 1.05 MB" = 1048576, # 7
"-1.05 MB" = -1048576, # 8
" NA B" = NA # 9
)
expect_equal(pretty_bytes(unname(cases)), names(cases))
})
prettyunits/tests/testthat/test-ms.r 0000644 0001762 0000144 00000010606 14503574705 017462 0 ustar ligges users
context("Pretty milliseconds")
test_that("pretty_ms works", {
expect_equal(pretty_ms(0), '0ms')
expect_equal(pretty_ms(0.1), '1ms')
expect_equal(pretty_ms(1), '1ms')
expect_equal(pretty_ms(1000 + 400), '1.4s')
expect_equal(pretty_ms(1000 * 2 + 400), '2.4s')
expect_equal(pretty_ms(1000 * 55), '55s')
expect_equal(pretty_ms(1000 * 67), '1m 7s')
expect_equal(pretty_ms(1000 * 60 * 5), '5m')
expect_equal(pretty_ms(1000 * 60 * 67), '1h 7m')
expect_equal(pretty_ms(1000 * 60 * 60 * 12), '12h')
expect_equal(pretty_ms(1000 * 60 * 60 * 40), '1d 16h')
expect_equal(pretty_ms(1000 * 60 * 60 * 999), '41d 15h')
})
test_that("compact pretty_ms works", {
expect_equal(pretty_ms(1000 + 4, compact = TRUE), '~1s')
expect_equal(pretty_ms(1000 * 60 * 60 * 999, compact = TRUE), '~41d')
})
test_that("pretty_ms handles vectors", {
v <- c(0, 0.1, 1, 1400, 2400, 1000 * 55, 1000 * 67,
1000 * 60 * 5, 1000 * 60 * 67, 1000 * 60 * 60 * 12,
1000 * 60 * 60 * 40, 1000 * 60 * 60 * 999)
v2 <- c("0ms", "1ms", "1ms", "1.4s", "2.4s", "55s", "1m 7s",
"5m", "1h 7m", "12h", "1d 16h", "41d 15h")
expect_equal(pretty_ms(v), v2)
})
context("Pretty seconds")
test_that("pretty_sec works", {
expect_equal(pretty_sec(0 / 1000), '0ms')
expect_equal(pretty_sec(0.1 / 1000), '1ms')
expect_equal(pretty_sec(1 / 1000), '1ms')
expect_equal(pretty_sec((1000 + 400) / 1000), '1.4s')
expect_equal(pretty_sec((1000 * 2 + 400) / 1000), '2.4s')
expect_equal(pretty_sec(1000 * 55 / 1000), '55s')
expect_equal(pretty_sec(1000 * 67 / 1000), '1m 7s')
expect_equal(pretty_sec(1000 * 60 * 5 / 1000), '5m')
expect_equal(pretty_sec(1000 * 60 * 67 / 1000), '1h 7m')
expect_equal(pretty_sec(1000 * 60 * 60 * 12 / 1000), '12h')
expect_equal(pretty_sec(1000 * 60 * 60 * 40 / 1000), '1d 16h')
expect_equal(pretty_sec(1000 * 60 * 60 * 999 / 1000), '41d 15h')
})
test_that("compact pretty_sec works", {
expect_equal(pretty_sec((1000 + 4) / 1000, compact = TRUE), '~1s')
expect_equal(pretty_sec(1000 * 60 * 60 * 999 / 1000, compact = TRUE), '~41d')
})
test_that("pretty_sec handles vectors", {
v <- c(0, 0.1, 1, 1400, 2400, 1000 * 55, 1000 * 67,
1000 * 60 * 5, 1000 * 60 * 67, 1000 * 60 * 60 * 12,
1000 * 60 * 60 * 40, 1000 * 60 * 60 * 999) / 1000
v2 <- c("0ms", "1ms", "1ms", "1.4s", "2.4s", "55s", "1m 7s",
"5m", "1h 7m", "12h", "1d 16h", "41d 15h")
expect_equal(pretty_sec(v), v2)
})
context("Pretty dt")
test_that("pretty_dt works", {
expect_equal(pretty_dt(as.difftime(units = "secs", 0 / 1000)), '0ms')
expect_equal(pretty_dt(as.difftime(units = "secs", 0.1 / 1000)), '1ms')
expect_equal(pretty_dt(as.difftime(units = "secs", 1 / 1000)), '1ms')
expect_equal(pretty_dt(as.difftime(units = "secs", (1000 + 400) / 1000)), '1.4s')
expect_equal(pretty_dt(as.difftime(units = "secs", (1000 * 2 + 400) / 1000)), '2.4s')
expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 55 / 1000)), '55s')
expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 67 / 1000)), '1m 7s')
expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 5 / 1000)), '5m')
expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 67 / 1000)), '1h 7m')
expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 12 / 1000)), '12h')
expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 40 / 1000)), '1d 16h')
expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 999 / 1000)), '41d 15h')
})
test_that("compact pretty_dt works", {
expect_equal(pretty_dt(as.difftime(units = "secs", (1000 + 4) / 1000), compact = TRUE), '~1s')
expect_equal(pretty_dt(as.difftime(units = "secs", 1000 * 60 * 60 * 999 / 1000), compact = TRUE), '~41d')
})
test_that("pretty_dt handles vectors", {
v <- c(0, 0.1, 1, 1400, 2400, 1000 * 55, 1000 * 67,
1000 * 60 * 5, 1000 * 60 * 67, 1000 * 60 * 60 * 12,
1000 * 60 * 60 * 40, 1000 * 60 * 60 * 999) / 1000
v2 <- c("0ms", "1ms", "1ms", "1.4s", "2.4s", "55s", "1m 7s",
"5m", "1h 7m", "12h", "1d 16h", "41d 15h")
expect_equal(pretty_dt(as.difftime(units = "secs", v)), v2)
})
test_that("pretty_dt works with NAs", {
stime <- Sys.time()
v <- .difftime(c(difftime(NA, NA), difftime(stime + 1, stime)), "secs")
v2 <- c(NA_character_, "~1s")
v3 <- c(NA_character_, "1s")
expect_equal(pretty_dt(v, compact = TRUE), v2)
expect_equal(pretty_dt(v, compact = FALSE), v3)
})
prettyunits/tests/testthat/test-rounding.r 0000644 0001762 0000144 00000010350 14504005011 020640 0 ustar ligges users context("Rounding to string values")
test_that("Rounding", {
expect_error(pretty_round(1, c(2, 3)),
regexp="digits must either be a scalar or the same length as x")
expect_equal(pretty_round(11), "11")
expect_equal(pretty_round(5), "5")
expect_equal(pretty_round(0.05), "0")
expect_equal(pretty_round(NA), "NA")
expect_equal(pretty_round(NaN), "NaN")
expect_equal(pretty_round(Inf), "Inf")
expect_equal(pretty_round(-Inf), "-Inf")
## Respecting the digits
expect_equal(pretty_round(0.05, 3), "0.050")
expect_equal(pretty_round(123.05, 3), "123.050")
expect_equal(pretty_round(c(100, 0.1), 3), c("100.000", "0.100"),
info="Vectors work with different orders of magnitude work")
expect_equal(pretty_round(c(100, 0.1), c(0, 3)), c("100", "0.100"),
info="Vectors of digits work")
expect_equal(pretty_round(c(0.1, NA), digits=3), c("0.100", "NA"),
info="Mixed inputs (NA, NaN, Inf or numeric), NA")
expect_equal(pretty_round(c(0.1, NA, NaN, Inf, -Inf), digits=3),
c("0.100", "NA", "NaN", "Inf", "-Inf"),
info="Mixed inputs (NA, NaN, Inf or numeric)")
## All zeros
expect_equal(pretty_round(0, digits=3), "0.000")
expect_equal(pretty_round(c(0, NA), digits=3), c("0.000", "NA"))
# scientific notation
expect_equal(pretty_round(1234567, digits=3, sci_range=5), "1.234567000e6",
info="sci_range works with pretty_round (even if it looks odd)")
expect_equal(pretty_round(1234567, digits=3, sci_range=5),
pretty_round(1234567, digits=3, sci_range=5),
info="sci_range works with pretty_round (even if it looks odd)")
expect_equal(pretty_round(1234567, digits=3, sci_range=5, sci_sep="x10^"),
"1.234567000x10^6",
info="sci_sep is respected.")
expect_equal(pretty_round(c(1e7, 1e10), digits=c(-3, -9), sci_range=5),
c("1.0000e7", "1.0e10"),
info="Different numbers of digits for rounding work with pretty_round")
})
test_that("Significance", {
expect_equal(pretty_signif(11), "11.0000")
expect_equal(pretty_signif(5), "5.00000")
expect_equal(pretty_signif(0.05), "0.0500000")
expect_equal(pretty_signif(NA), "NA")
expect_equal(pretty_signif(NaN), "NaN")
expect_equal(pretty_signif(Inf), "Inf")
expect_equal(pretty_signif(-Inf), "-Inf")
## Respecting the digits
expect_equal(pretty_signif(0.05, 3), "0.0500")
expect_equal(pretty_signif(123.05, 3), "123")
expect_equal(pretty_signif(123456.05, 3), "123000")
expect_equal(pretty_signif(123456.05, 3, sci_range=6), "123000")
expect_equal(pretty_signif(123456.05, 3, sci_range=5), "1.23e5")
expect_equal(pretty_signif(-123000.05, 3, sci_range=5), "-1.23e5")
expect_equal(pretty_signif(999999, 3, sci_range=6), "1.00e6",
info="Rounding around the edge of the sci_range works correctly (going up)")
expect_equal(pretty_signif(999999, 7, sci_range=6), "999999.0",
info="Rounding around the edge of the sci_range works correctly (going staying the same)")
expect_equal(pretty_signif(-.05, 3), "-0.0500")
## Exact orders of magnitude work on both sides of 0
expect_equal(pretty_signif(0.01, 3), "0.0100")
expect_equal(pretty_signif(1, 3), "1.00")
expect_equal(pretty_signif(100, 3), "100")
## Vectors work with different orders of magnitude work
expect_equal(pretty_signif(c(100, 0.1), 3), c("100", "0.100"))
## Rounding to a higher number of significant digits works correctly
expect_equal(pretty_signif(0.9999999, 3), "1.00")
## Mixed inputs (NA, NaN, Inf or numeric)
expect_equal(pretty_signif(NA), "NA")
expect_equal(pretty_signif(c(0.1, NA), digits=3), c("0.100", "NA"))
expect_equal(pretty_signif(c(0.1, NA, NaN, Inf, -Inf), digits=3),
c("0.100", "NA", "NaN", "Inf", "-Inf"))
## All zeros
expect_equal(pretty_signif(0, digits=3), "0.000")
expect_equal(pretty_signif(c(0, NA), digits=3), c("0.000", "NA"))
expect_equal(pretty_signif(1234567, digits=3, sci_range=5, sci_sep="x10^"),
"1.23x10^6",
info="sci_sep is respected.")
expect_equal(pretty_signif(c(1e7, 1e10), digits=3),
c("1.00e7", "1.00e10"),
info="Different numbers of digits for rounding work with pretty_signif")
})
prettyunits/tests/testthat.R 0000644 0001762 0000144 00000000102 14253066262 016010 0 ustar ligges users library(testthat)
library(prettyunits)
test_check("prettyunits")
prettyunits/R/ 0000755 0001762 0000144 00000000000 14504004731 013063 5 ustar ligges users prettyunits/R/sysdata.rda 0000644 0001762 0000144 00000225102 14504001530 015217 0 ustar ligges users BZh91AY&SY ;<