Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/r-lib/prettyunits
Pretty, human readable formatting of quantities
https://github.com/r-lib/prettyunits
Last synced: 4 days ago
JSON representation
Pretty, human readable formatting of quantities
- Host: GitHub
- URL: https://github.com/r-lib/prettyunits
- Owner: r-lib
- License: other
- Created: 2014-10-03T19:59:19.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2024-08-05T16:01:51.000Z (3 months ago)
- Last Synced: 2024-10-06T05:28:12.808Z (about 1 month ago)
- Language: JavaScript
- Homepage: http://r-lib.github.io/prettyunits/
- Size: 4.81 MB
- Stars: 132
- Watchers: 5
- Forks: 15
- Open Issues: 9
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
- jimsghstars - r-lib/prettyunits - Pretty, human readable formatting of quantities (JavaScript)
README
---
output: github_document
---```{r, setup, echo = FALSE, message = FALSE}
knitr::opts_chunk$set(
comment = "##>",
tidy = FALSE,
error = FALSE)
```[![R-CMD-check](https://github.com/r-lib/prettyunits/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/prettyunits/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/r-lib/prettyunits/branch/main/graph/badge.svg)](https://app.codecov.io/gh/r-lib/prettyunits?branch=main)
[![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/prettyunits)](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 eval = FALSE}
install.packages("prettyunits")
```If you need the development version, install it from GitHub:
```{r eval = FALSE}
pak::pak("r-lib/prettyunits")
``````{r include=FALSE}
library(prettyunits)
library(magrittr)
```## Bytes
`pretty_bytes` formats number of bytes in a human readable way:
```{r}
pretty_bytes(1337)
pretty_bytes(133337)
pretty_bytes(13333337)
pretty_bytes(1333333337)
pretty_bytes(133333333337)
```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()
```## Quantities
`pretty_num` formats number related to linear quantities in a human readable way:
```{r}
pretty_num(1337)
pretty_num(-133337)
pretty_num(1333.37e-9)
```
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)
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))
```## 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))
pretty_ms(c(1337, 13370, 133700, 1337000, 1337000000),
compact = TRUE)
pretty_sec(c(1337, 13370, 133700, 1337000, 13370000))
pretty_sec(c(1337, 13370, 133700, 1337000, 13370000),
compact = TRUE)```
## 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"))
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"))
``````{r}
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"))
```## Rounding
`pretty_round()` and `pretty_signif()` preserve trailing zeros.
```{r}
pretty_round(1, digits=6)
pretty_signif(c(99, 0.9999), digits=3)
```## 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))
```## Colors
`pretty_color` converts colors from other representations to human-readable
names.```{r}
pretty_color("black")
pretty_color("#123456")
```