Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclude ... from tables() search #5199

Merged
merged 7 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions R/tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
MB = NCOL = NROW = NULL

tables = function(mb=TRUE, order.col="NAME", width=80,
env=parent.frame(), silent=FALSE, index=FALSE)
env=parent.frame(), silent=FALSE, index=FALSE)
{
# Prints name, size and colnames of all data.tables in the calling environment by default
all_obj = objects(envir=env, all.names=TRUE)
is_DT = which(vapply_1b(all_obj, function(x) is.data.table(get(x, envir=env))))
if (!length(is_DT)) {
# include "hidden" objects (starting with .) via all.names=TRUE, but exclude ... specifically, #5197
all_obj = grep("...", objects(envir=env, all.names=TRUE, sorted=order.col == "NAME"), invert=TRUE, fixed=TRUE, value=TRUE)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could also pass pattern="^([^.]|[.][^.]|[.][.][^.])" to do it in one call, but that's pretty ugly, and anyway objects() itself is just calling grep() so there's no efficiency benefit (i.e., pattern is not being handled in C), so wrapping in grep() does the same thing with a more readable grep() call.

Copy link
Member

@mattdowle mattdowle Nov 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

objects() only calls grep() when pattern is not missing, iiuc. But pattern is missing in this objects() call. So doesn't adding grep here add overhead of grep when all we want to do is remove the string "..." if it's present?

is_DT = vapply_1b(all_obj, function(x) is.data.table(get(x, envir=env)))
if (!any(is_DT)) {
if (!silent) catf("No objects of class data.table exist in %s\n", if (identical(env, .GlobalEnv)) ".GlobalEnv" else format(env))
return(invisible(data.table(NULL)))
}
Expand All @@ -23,8 +24,11 @@ tables = function(mb=TRUE, order.col="NAME", width=80,
KEY = list(key(DT)),
INDICES = if (index) list(indices(DT)))
}))
if (!order.col %chin% names(info)) stopf("order.col='%s' not a column name of info", order.col)
info = info[base::order(info[[order.col]])] # base::order to maintain locale ordering of table names
# objects() above handled the sorting for order.col=="NAME"
if (order.col != "NAME") {
if (!order.col %chin% names(info)) stopf("order.col='%s' not a column name of info", order.col)
info = info[base::order(info[[order.col]])] # base::order to maintain locale ordering of table names
}
if (!silent) {
# prettier printing on console
pretty_format = function(x, width) {
Expand Down
2 changes: 2 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -18222,3 +18222,5 @@ DT1 = data.table(id=1:3, grp=c('a', 'a', 'b'), value=4:6)
DT2 = data.table(grp = c('a', 'b'), agg = list(c('1' = 4, '2' = 5), c('3' = 6)))
test(2217, DT1[, by = grp, .(agg = list(setNames(as.numeric(value), id)))], DT2)

# tables() doesn't find `...`, #5197
test(2218, (function(...) tables())(), output = "No objects of class data.table exist")