-
Notifications
You must be signed in to change notification settings - Fork 3
/
jamba-lldf.R
75 lines (73 loc) · 2.17 KB
/
jamba-lldf.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#' Long listing of R session objects
#'
#' Long listing of R session objects
#'
#' This function expands `base::ls()` by also determining the
#' object size, and sorting to display the top `n` objects by
#' size, largest first.
#'
#' This package will call `pryr::object_size` if available,
#' otherwise falls back to `utils::object.size()`.
#'
#' @family jam practical functions
#'
#' @param n `integer` or `Inf` indicating how many objects to
#' include in the output `data.frame`.
#' @param envir `environment` where the list of objects is obtained.
#' Note this environment is also where objects are evaluated,
#' so the `envir` and `items` should be compatible.
#' @param items optional `character` vector of items to include
#' in the output `data.frame`. Note that these items should be
#' available in the environment `envir`.
#' @param all.names `logical` passed to `base::ls()` indicating whether
#' to include all names, where `all.names=TRUE` will include
#' hidden objects whose name begin with `"."` such as `".First"`.
#' @param ... additional arguments are passed to `ls()`
#'
#' @examples
#' lldf(10);
#'
#' # custom environment
#' newenv <- new.env();
#' newenv$A <- 1:10;
#' newenv$df <- data.frame(A=1:10, B=11:20);
#' lldf(envir=newenv);
#' rm(newenv);
#'
#' @export
lldf <- function
(n=Inf,
envir=.GlobalEnv,
items=NULL,
use_utils_objectsize=TRUE,
all.names=TRUE,
...)
{
# light check for pryr package without using require()
if (!use_utils_objectsize &&
nchar(system.file(package="pryr")) > 0) {
osfun <- pryr::object_size;
} else {
osfun <- utils::object.size;
}
if (length(n) == 0) {
n <- Inf;
}
if (length(items) == 0) {
items <- ls(envir=envir,
all.names=all.names,
sorted=FALSE,
...)
}
df <- jamba::rbindList(lapply(items, function(i){
data.frame(name=i,
class=jamba::cPaste(class(get(i, envir=envir)), sep=", "),
bytes=as.numeric(osfun(get(i, envir=envir)))
)
}));
df <- head(jamba::mixedSortDF(df, byCols="-bytes"), n)
rownames(df) <- df$name;
# convert bytes to human friendly units
df$size <- jamba::asSize(df$bytes);
df;
}