Skip to content

Commit e8eaa40

Browse files
authored
Update data.table.R
1 parent 8785804 commit e8eaa40

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

R/data.table.R

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,22 +1212,9 @@ replace_dot_alias = function(e) {
12121212
setalloccol(x, n, verbose=verbose) # always assigns to calling scope; i.e. this scope
12131213
if (is.name(name)) {
12141214
assign(as.character(name),x,parent.frame(),inherits=TRUE)
1215-
} else if (.is_simple_extraction(name)) { # TODO(#6702): use a helper here as the code is very similar to setDT().
1216-
k = eval(name[[2L]], parent.frame(), parent.frame())
1217-
if (is.list(k)) {
1218-
origj = j = if (name[[1L]] == "$") as.character(name[[3L]]) else eval(name[[3L]], parent.frame(), parent.frame())
1219-
if (is.character(j)) {
1220-
if (length(j)!=1L) stopf("Cannot assign to an under-allocated recursively indexed list -- L[[i]][,:=] syntax is only valid when i is length 1, but its length is %d", length(j))
1221-
j = match(j, names(k))
1222-
if (is.na(j)) internal_error("item '%s' not found in names of list", origj) # nocov
1223-
}
1224-
.Call(Csetlistelt,k,as.integer(j), x)
1225-
} else if (is.environment(k) && exists(as.character(name[[3L]]), k)) {
1226-
assign(as.character(name[[3L]]), x, k, inherits=FALSE)
1227-
} else if (isS4(k)) {
1228-
.Call(CsetS4elt, k, as.character(name[[3L]]), x)
1229-
}
1230-
} # TO DO: else if env$<- or list$<-
1215+
} else if (.is_simple_extraction(name)) {
1216+
assign_to_extracted_target(name, x, parent.frame())
1217+
}
12311218
}
12321219
}
12331220
}
@@ -2970,19 +2957,8 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) {
29702957
name = as.character(name)
29712958
assign(name, x, parent.frame(), inherits=TRUE)
29722959
} else if (.is_simple_extraction(name)) {
2973-
# common case is call from 'lapply()'
2974-
k = eval(name[[2L]], parent.frame(), parent.frame())
2975-
if (is.list(k)) {
2976-
origj = j = if (name[[1L]] == "$") as.character(name[[3L]]) else eval(name[[3L]], parent.frame(), parent.frame())
2977-
if (length(j) == 1L) {
2978-
if (is.character(j)) {
2979-
j = match(j, names(k))
2980-
if (is.na(j))
2981-
stopf("Item '%s' not found in names of input list", origj)
2982-
}
2983-
}
2984-
.Call(Csetlistelt, k, as.integer(j), x)
2985-
} else if (is.environment(k) && exists(as.character(name[[3L]]), k)) {
2960+
assign_to_extracted_target(name, x, parent.frame())
2961+
} else if (is.environment(k) && exists(as.character(name[[3L]]), k)) {
29862962
assign(as.character(name[[3L]]), x, k, inherits=FALSE)
29872963
} else if (isS4(k)) {
29882964
.Call(CsetS4elt, k, as.character(name[[3L]]), x)
@@ -3451,3 +3427,23 @@ is_constantish = function(q, check_singleton=FALSE) {
34513427
names(on) = xCols
34523428
list(on = on, ops = idx_op)
34533429
}
3430+
assign_to_extracted_target <- function(name, x, parent_env = parent.frame()) {
3431+
k = eval(name[[2L]], parent_env, parent_env)
3432+
if (is.list(k)) {
3433+
origj = j = if (name[[1L]] == "$") as.character(name[[3L]]) else eval(name[[3L]], parent_env, parent_env)
3434+
if (is.character(j)) {
3435+
if (length(j) != 1L) {
3436+
stopf("Cannot assign to an under-allocated recursively indexed list -- L[[i]][,:=] syntax is only valid when i is length 1, but its length is %d", length(j))
3437+
}
3438+
j = match(j, names(k))
3439+
if (is.na(j)) {
3440+
internal_error("item '%s' not found in names of list", origj) # nocov
3441+
}
3442+
}
3443+
.Call(Csetlistelt, k, as.integer(j), x)
3444+
} else if (is.environment(k) && exists(as.character(name[[3L]]), k)) {
3445+
assign(as.character(name[[3L]]), x, k, inherits = FALSE)
3446+
} else if (isS4(k)) {
3447+
.Call(CsetS4elt, k, as.character(name[[3L]]), x)
3448+
}
3449+
}

0 commit comments

Comments
 (0)