Skip to content

Commit

Permalink
Allow setDT(get(...)) to work as previously (#6726)
Browse files Browse the repository at this point in the history
* Allow setDT(get(...)) to work as previously

* Quirks of test.data.table...

* need to eval() in the right place

* imitate the approach in other branches more closely

* maybe we just needed enclos=?

* Comment for posterity

* Actually do the test
  • Loading branch information
MichaelChirico authored Jan 17, 2025
1 parent 1dd2976 commit d263924
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -2987,6 +2987,12 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) {
} else if (isS4(k)) {
.Call(CsetS4elt, k, as.character(name[[3L]]), x)
}
} else if (name %iscall% "get") { # #6725
# edit 'get(nm, env)' call to be 'assign(nm, x, envir=env)'
name = match.call(get, name)
name[[1L]] = quote(assign)
name$value = x
eval(name, parent.frame(), parent.frame())
}
.Call(CexpandAltRep, x) # issue#2866 and PR#2882
invisible(x)
Expand Down
12 changes: 12 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -20653,6 +20653,18 @@ setDT(d2)
test(2295.1, !is.data.table(d1))
test(2295.2, rownames(d1), 'b')
test(2295.3, is.data.table(d2))
# Ensure against regression noted in #6725
x = data.frame(a=1)
e = environment()
foo = function(nm, env) {
setDT(get(nm, envir=env))
}
foo('x', e)
test(2295.4, is.data.table(x))
e = new.env(parent=topenv())
e$x = data.frame(a=1)
foo('x', e)
test(2295.5, is.data.table(e$x))

# #6588: .checkTypos used to give arbitrary strings to stopf as the first argument
test(2296, d2[x %no such operator% 1], error = '%no such operator%')
Expand Down

0 comments on commit d263924

Please sign in to comment.