Skip to content

Commit

Permalink
add pretty_num tibble example
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe-Regouby committed Nov 7, 2020
1 parent 421704f commit 5dc009f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 13 deletions.
35 changes: 24 additions & 11 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ knitr::opts_chunk$set(
The `prettyunits` package formats quantities in human readable form. Currently
- time units
- information (i.e. bytes)
- linear quantities (i.e. quantities representing meters, but not square-meters)
- linear quantities (i.e. like quantities representing distance, but not surface or volume)
are supported.

## Installation
Expand All @@ -26,7 +26,7 @@ You can install the package from CRAN:
install.packages("prettyunits")
```

```{r}
```{r include=FALSE}
library(prettyunits)
library(magrittr)
```
Expand Down Expand Up @@ -61,6 +61,28 @@ uls <- function(path = ".") {
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
Expand Down Expand Up @@ -101,13 +123,4 @@ time_ago(now - as.difftime(5, units = "hours"))
time_ago(now - as.difftime(25, units = "hours"))
```

## 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)
```


57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
[![Windows Build status](https://ci.appveyor.com/api/projects/status/github/r-lib/prettyunits?svg=true)](https://ci.appveyor.com/project/gaborcsardi/prettyunits)
[![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. Currently
time units and information (i.e. bytes) are supported.
- time units
- information (i.e. bytes)
- linear quantities (i.e. like quantities representing distance, but not surface or volume)
are supported.

## Installation

Expand Down Expand Up @@ -97,6 +99,57 @@ uls()
##> 644 gaborcsardi staff 2.95 kB 2019-03-27 09:58:43 README.Rmd
```

## 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 µ"
```
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))
```

```
##> # A tibble: 3 x 3
##> name `size in m` `speed in m/s`
##> <chr> <chr> <chr>
##> 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
Expand Down

0 comments on commit 5dc009f

Please sign in to comment.