-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathobject.size.R
58 lines (55 loc) · 2.1 KB
/
object.size.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File src/library/utils/R/object.size.R
# Part of the R package, https://www.R-project.org
#
# Copyright (C) 1995-2016 The R Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available at
# https://www.R-project.org/Licenses/
object.size <- function(x)
structure(.Call(C_objectSize, x), class = "object_size")
format.object_size <- function(x, units = "b", ...)
{
units <- match.arg(units, c("b", "auto", "Kb", "Mb", "Gb", "Tb", "Pb",
"B", "KB", "MB", "GB", "TB", "PB",
"KiB", "MiB", "GiB", "TiB",
"PiB", "EiB", "ZiB", "YiB"))
if (units == "auto")
units <-
if (x >= 1024^4) "Tb"
else if (x >= 1024^3) "Gb"
else if (x >= 1024^2) "Mb"
else if (x >= 1024 ) "Kb" else "b"
switch(units,
"b" =, "B" = paste(x, "bytes"),
"Kb" =, "KB" = paste(round(x/1024 , 1L), "Kb"),
"Mb" =, "MB" = paste(round(x/1024^2, 1L), "Mb"),
"Gb" =, "GB" = paste(round(x/1024^3, 1L), "Gb"),
"Tb" =, "TB" = paste(round(x/1024^4, 1L), "Tb"),
"Pb" =, "PB" = paste(round(x/1024^5, 1L), "Pb"),
"KiB" = paste(round(x/1024 , 1L), "KiB"),
"MiB" = paste(round(x/1024^2, 1L), "MiB"),
"GiB" = paste(round(x/1024^3, 1L), "GiB"),
"TiB" = paste(round(x/1024^4, 1L), "TiB"),
"PiB" = paste(round(x/1024^5, 1L), "PiB"),
"EiB" = paste(round(x/1024^6, 1L), "EiB"),
"ZiB" = paste(round(x/1024^7, 1L), "ZiB"),
"YiB" = paste(round(x/1024^8, 1L), "YiB")
)
}
print.object_size <-
function(x, quote = FALSE, units = "b", ...)
{
y <- format.object_size(x, units = units)
if(quote) print.default(y, ...) else cat(y, "\n", sep = "")
invisible(x)
}