Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@
# 2: 2021-02-03 # was 18661
# 3: 4611686018427387906 # was error 'please use as.character'
```

47. `tables()` failed with `argument "..." is missing` when called from within a function taking `...`; e.g. `function(...) { tables() }`, [#5197](https://github.com/Rdatatable/data.table/issues/5197). Thanks @greg-minshall for the report and @michaelchirico for the fix.

## NOTES

Expand Down
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
Copy Markdown
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
Copy Markdown
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(mget(all_obj, envir=env), is.data.table)
Copy link
Copy Markdown
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.

changing from get to mget here meant that inherits was changed from TRUE to FALSE because the defaults are different. But doesn't matter as the input is ls() in that env so it does exist there. Ok, got it.

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
6 changes: 5 additions & 1 deletion inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -7490,7 +7490,8 @@ test(1536, duplicated(dt, incomparables=TRUE), error = "argument 'incomparables
test(1537 , names(melt(dt, id.vars=1L, variable.name = "x", value.name="x")), c("x", "x.1", "x.2"), output = "Duplicate column names")

# test for tables()
test(1538, tables(), output = "Total:")
test(1538.1, tables(), output = "Total:")
test(1538.2, !is.unsorted(tables(order.col="NROW")$NROW))

# uniqueN not support list-of-list: reverted #1224
d1 <- data.table(a = 1:4, l = list(list(letters[1:2]),list(Sys.time()),list(1:10),list(letters[1:2])))
Expand Down Expand Up @@ -18262,3 +18263,6 @@ for (fun in funs) {
}
test(testnum+0.01, DT[, prod(l), g], error="GForce prod can only be applied to columns, not .SD or similar.")

# tables() error when called from inside a function(...), #5197
test(2221, (function(...) tables())(), output = "No objects of class data.table exist")