
```{r, setup, echo = FALSE, message = FALSE}
knitr::opts_chunk$set(
  comment = "##>",
  tidy = FALSE,
  error = FALSE)
```

[![Linux Build Status](https://travis-ci.org/gaborcsardi/prettyunits.svg?branch=master)](https://travis-ci.org/gaborcsardi/prettyunits)
[![Windows Build status](https://ci.appveyor.com/api/projects/status/github/gaborcsardi/prettyunits?svg=true)](https://ci.appveyor.com/project/gaborcsardi/prettyunits)
[![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/prettyunits)](http://cran.r-project.org/web/packages/prettyunits/index.html)


# prettyunits

The `prettyunits` package formats quantities in human readable form. Currently
time units and information (i.e. bytes) are supported.

## Installation

You can install the package from github:

```{r eval = FALSE}
library(devtools)
install_github("gaborcsardi/prettyunits")
```

```{r}
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()
```

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

```{r}

```
