asSize: convert numeric value or R object to human-readable size

asSizeR Documentation

convert numeric value or R object to human-readable size

Description

convert numeric value or R object to human-readable size

Usage

asSize(
  x,
  digits = 3,
  abbreviateUnits = TRUE,
  unitType = "bytes",
  unitAbbrev = gsub("^(.).*$", "\\1", unitType),
  kiloSize = 1024,
  sep = " ",
  ...
)

Arguments

x

numeric vector, class object_size which is converted to numeric, any other R object is converted to a single numeric value using utils::object.size().

digits

integer number of digits used by base::format() when formatting the number to create a character string

abbreviateUnits

logical, default TRUE, whether to print abbreviated units, for example using k, M, G, T, P instead of kilo, mega, Giga, Tera, Peta, respectively.

unitType

character string indicating the base unit of measure, by default "bytes". Note that trailing "s" is removed when the number is singular.

unitAbbrev

character string indicating an abbreviated base unit, by default it uses the first character from unitType.

kiloSize

numeric, default 1024, number of base units when converting from to one "kilo" base unit. For computer-based size such as file size and object size, this value is 1024. For other purposes, such as scientific or monetary numbers, this value should usually be 1000.

sep

delimiter used between the numeric value and the unit, default " ".

...

other parameters passed to base::format().

Details

This function returns human-readable size based upon numeric input. Alternatively, when input is any other R object, it calls utils::object.size() to produce a single numeric value which is then used to produce human-readable size.

The default behavior is to report computer size in bytes, where 1024 is considered "kilo", however argument kiloSize can be used to produce values where kiloSize=1000 which is suitable for monetary and other scientific values.

Value

character vector representing human-friendly size, based upon the kiloSize argument to determine whether to report byte (1024) or scientific (1000) units.

See Also

Other jam string functions: breaksByVector(), fillBlanks(), formatInt(), gsubOrdered(), gsubs(), makeNames(), nameVector(), nameVectorN(), padInteger(), padString(), pasteByRow(), pasteByRowOrdered(), sizeAsNum(), tcount(), ucfirst()

Examples

asSize(c(1, 10,2010,22000,52200))
#> "1 byte"   "10 bytes" "2 kb"     "21 kb"    "51 kb"

# demonstration of straight numeric units
asSize(c(1, 100, 1000, 10000), unitType="", kiloSize=100)


jmw86069/jamba documentation built on Feb. 28, 2025, 9:07 p.m.