Skip to content

Commit c0469f4

Browse files
authored
Provide a .formula2varlist implementation (#6842)
Since base::.formula2varlist is not an API and it is now needed in two places, provide our own implementation.
1 parent 17aa167 commit c0469f4

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

R/data.table.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,7 +2454,7 @@ split.data.table = function(x, f, drop = FALSE, by, sorted = FALSE, keep.by = TR
24542454
# same as split.data.frame - handling all exceptions, factor orders etc, in a single stream of processing was a nightmare in factor and drop consistency
24552455
# evaluate formula mirroring split.data.frame #5392. Mimics base::.formula2varlist.
24562456
if (inherits(f, "formula"))
2457-
f = eval(attr(terms(f), "variables"), x, environment(f))
2457+
f = formula_vars(f, x)
24582458
# be sure to use x[ind, , drop = FALSE], not x[ind], in case downstream methods don't follow the same subsetting semantics (#5365)
24592459
return(lapply(split(x = seq_len(nrow(x)), f = f, drop = drop, ...), function(ind) x[ind, , drop = FALSE]))
24602460
}
@@ -2530,7 +2530,7 @@ sort_by.data.table <- function(x, y, ...)
25302530
{
25312531
if (!cedta()) return(NextMethod()) # nocov
25322532
if (inherits(y, "formula"))
2533-
y <- .formula2varlist(y, x)
2533+
y <- formula_vars(y, x)
25342534
if (!is.list(y))
25352535
y <- list(y)
25362536
# use forder instead of base 'order'

R/utils.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,11 @@ rss = function() { #5515 #5517
212212
round(ans / 1024.0, 1L) # return MB
213213
# nocov end
214214
}
215+
216+
formula_vars = function(f, x) { # .formula2varlist is not API and seems to have appeared after R-4.2, #6841
217+
terms <- terms(f)
218+
setNames(
219+
eval(attr(terms, "variables"), x, environment(f)),
220+
attr(terms, "term.labels")
221+
)
222+
}

0 commit comments

Comments
 (0)