The prettyunits
package formats quantities in human readable form. Currently time units and information (i.e. bytes) are supported.
You can install the package from CRAN:
pretty_bytes
formats number of bytes in a human readable way:
##> [1] "1.34 kB"
##> [1] "133.34 kB"
##> [1] "13.33 MB"
##> [1] "1.33 GB"
##> [1] "133.33 GB"
Here is a simple function that emulates the Unix ls
command, with nicely formatted file sizes:
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 440.00 B 2019-03-25 10:25:08 NEWS.md
##> 644 gaborcsardi staff 4.65 kB 2017-12-15 11:00:16 README.md
##> 644 gaborcsardi staff 2.95 kB 2019-03-27 09:58:43 README.Rmd
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.
##> [1] "1.3s" "13.4s" "2m 13.7s" "22m 17s"
##> [5] "15d 11h 23m 20s"
##> [1] "~1.3s" "~13.4s" "~2m" "~22m" "~15d"
##> [1] "22m 17s" "3h 42m 50s" "1d 13h 8m 20s"
##> [4] "15d 11h 23m 20s" "154d 17h 53m 20s"
##> [1] "~22m" "~3h" "~1d" "~15d" "~154d"
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.
##> [1] "<1 min"
##> [1] "14 min"
##> [1] "5 hours"
##> [1] "1 day"
##> [1] "5 day"
##> [1] "moments ago"
##> [1] "less than a minute ago"
##> [1] "14 minutes ago"
##> [1] "5 hours ago"
##> [1] "a day ago"