From d2639244126328467925adeadd2450f4079f913f Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Fri, 17 Jan 2025 05:03:53 -0800 Subject: [PATCH] Allow setDT(get(...)) to work as previously (#6726) * 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 --- R/data.table.R | 6 ++++++ inst/tests/tests.Rraw | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/R/data.table.R b/R/data.table.R index 270962421..909121852 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -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) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index ffbf1915f..f5c789ba2 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -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%')