#' Object size
#'
#' This function calculates the object size
#'
#' @param x object vector, data.frame, environment, ...
#' @param unit size unit; "B", "KB", "MB", "GB"
#' @examples
#' \dontrun{
#' sizeof(x = globalenv(), unit = "kb")
#' }
#' @export
sizeof <- function(x, unit) UseMethod("sizeof")
#' @rdname sizeof
#' @export
sizeof.default <- function(x, unit = "mb") {
class <- paste0(class(x), collapse = ",")
type <- typeof(x)
size <- object.size(x)[1L]
power <- switch(tolower(unit), b = 0, kb = 1, mb = 2, gb = 3)
data.table(class = class,
type = type,
size = round(size / (2^10)^power, 3),
unit = toupper(unit))
}
#' @rdname sizeof
#' @export
sizeof.data.frame <- function(x, unit = "mb") {
col <- names(x)
class <- sapply(x, class)
type <- sapply(x, typeof)
size <- c(sapply(x, function(s) object.size(s)), object.size(x))
power <- switch(tolower(unit), b = 0, kb = 1, mb = 2, gb = 3)
data.table(col = c(col, "total"),
class = c(class, "total"),
type = c(type, "total"),
size = round(size / (2^10)^power, 3),
unit = toupper(unit))
}
#' @rdname sizeof
#' @export
sizeof.environment <- function(x, unit = "mb") {
env <- ls(envir = x)
if (length(env) == 0L) stop("Object not found.")
class <- sapply(env, function(s) paste0(class(get(s, envir = x)), collapse = ","))
size <- sapply(env, function(s) object.size(get(s, envir = x)))
sizes <- c(size, sum(size))
power <- switch(tolower(unit), b = 0, kb = 1, mb = 2, gb = 3)
data.table(obj = c(names(size), "total"),
class = c(class, "total"),
size = round(sizes / (2^10)^power, 3),
unit = toupper(unit))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.