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

dt[, uniqueN(col)] does not warn when col is evaluated outside of dt's frame #2186

Closed
mbacou opened this issue Jun 4, 2017 · 6 comments
Closed

Comments

@mbacou
Copy link

mbacou commented Jun 4, 2017

Hi, the very simple code below returns 20 without warning. This is potentially very error-prone, when the user would normally expect a column named v in dt, but that column is accidentally missing. Not sure if this is intended. Thx!

Tested with data.table 1.10.4.

v <- 1:20
dt <- data.table(a=1:10, b=1:10)
dt[, uniqueN(v)]
# [1] 20
@MichaelChirico
Copy link
Member

MichaelChirico commented Jun 4, 2017 via email

@mbacou
Copy link
Author

mbacou commented Jun 4, 2017

Yes and no, data.frame would throw an error obviously. Generally, it seems to me that evaluation in ascending scope in j's (and also in by's ?) element is very prone to user errors. Somehow I feel like forcing the user to make an explicit call to an out-of-scope object would be safer (e.g. maybe using something like ..(col) -- but even then, calls like dt[, unique(..(col))] or dt[, sum(..(col))] would make no sense at all, and should throw warnings or errors.

@MichaelChirico
Copy link
Member

MichaelChirico commented Jun 4, 2017 via email

@franknarf1
Copy link
Contributor

franknarf1 commented Jun 4, 2017

I think the proposed inherits = FALSE would cover this #633 and agree it would be nice to have.

How/why should that differ for a [] call?

@MichaelChirico Because it protects us from writing j expressions that we don't want to be writing, just like DT[1, v := x ] will protect us if x does not have the right class, etc.

@mbacou mbacou changed the title dt[, uniqueN(col)] does not warm when col is evaluated outside of dt's frame dt[, uniqueN(col)] does not warn when col is evaluated outside of dt's frame Jun 4, 2017
@mbacou
Copy link
Author

mbacou commented Jun 4, 2017

I agree here, inherits = FALSE should be default behavior, with an explicit ..(col1, col2) notation possibly allowed.

Else in cases as below, a simple warning that "col and by were evaluated outside of dt's scope" would not hurt!

by <- 1:10
col <- 1:10
dt <- data.table(a=1:10, b=1:10)
dt[, c := sum(a*col), by=by]

Still feel like dt[, c := sum(a*..(col)), by=..(by)] is (ugly) but safer!

@arunsrinivasan
Copy link
Member

As @franknarf1 pointed out, it's a nice feature to have, and is a dup of #633.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants