Skip to content
Merged
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,32 @@
}
}

.assign_in_parent = function(name, value, env, err_msg_len, err_msg_na) {
k = eval(name[[2L]], env, env)
if (is.list(k)) {
origj = j = if (name[[1L]] == "$") as.character(name[[3L]]) else eval(name[[3L]], env, env)
Comment thread
MichaelChirico marked this conversation as resolved.
Outdated
if (length(j) != 1L) {
Comment thread
MichaelChirico marked this conversation as resolved.
stopf(err_msg_len, length(j))
}
if (is.character(j)) {
idx = match(j, names(k))
if (is.na(idx)) {
if (is.null(err_msg_na)) {
internal_error("item '%s' not found in names of list", origj)

Check warning on line 142 in R/data.table.R

View check run for this annotation

Codecov / codecov/patch

R/data.table.R#L142

Added line #L142 was not covered by tests
Comment thread
MichaelChirico marked this conversation as resolved.
Outdated
} else {
stopf(err_msg_na, origj)
}
}
j = idx
}
.Call(Csetlistelt, k, as.integer(j), value)
} else if (is.environment(k) && exists(as.character(name[[3L]]), k, inherits = FALSE)) {
Comment thread
MichaelChirico marked this conversation as resolved.
assign(as.character(name[[3L]]), value, k, inherits = FALSE)
} else if (isS4(k)) {
.Call(CsetS4elt, k, as.character(name[[3L]]), value)
}
}

"[.data.table" = function(x, i, j, by, keyby, with=TRUE, nomatch=NA, mult="all", roll=FALSE, rollends=if (roll=="nearest") c(TRUE,TRUE) else if (roll>=0.0) c(FALSE,TRUE) else c(TRUE,FALSE), which=FALSE, .SDcols, verbose=getOption("datatable.verbose"), allow.cartesian=getOption("datatable.allow.cartesian"), drop=NULL, on=NULL, env=NULL, showProgress=getOption("datatable.showProgress", interactive()))
{
# ..selfcount <<- ..selfcount+1 # in dev, we check no self calls, each of which doubles overhead, or could
Expand Down Expand Up @@ -1214,21 +1240,12 @@
setalloccol(x, n, verbose=verbose) # always assigns to calling scope; i.e. this scope
if (is.name(name)) {
assign(as.character(name),x,parent.frame(),inherits=TRUE)
} else if (.is_simple_extraction(name)) { # TODO(#6702): use a helper here as the code is very similar to setDT().
k = eval(name[[2L]], parent.frame(), parent.frame())
if (is.list(k)) {
origj = j = if (name[[1L]] == "$") as.character(name[[3L]]) else eval(name[[3L]], parent.frame(), parent.frame())
if (is.character(j)) {
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))
j = match(j, names(k))
if (is.na(j)) internal_error("item '%s' not found in names of list", origj) # nocov
}
.Call(Csetlistelt,k,as.integer(j), x)
} else if (is.environment(k) && exists(as.character(name[[3L]]), k)) {
assign(as.character(name[[3L]]), x, k, inherits=FALSE)
} else if (isS4(k)) {
.Call(CsetS4elt, k, as.character(name[[3L]]), x)
}
} else if (.is_simple_extraction(name)) {
.assign_in_parent(
Comment thread
MichaelChirico marked this conversation as resolved.
Outdated
name, x, parent.frame(),
err_msg_len = "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",
Comment thread
MichaelChirico marked this conversation as resolved.
Outdated
err_msg_na = NULL # Triggers internal_error for this case # nocov
Comment thread
MichaelChirico marked this conversation as resolved.
Outdated
)
} # TO DO: else if env$<- or list$<-
}
}
Expand Down Expand Up @@ -2973,22 +2990,11 @@
assign(name, x, parent.frame(), inherits=TRUE)
} else if (.is_simple_extraction(name)) {
# common case is call from 'lapply()'
k = eval(name[[2L]], parent.frame(), parent.frame())
if (is.list(k)) {
origj = j = if (name[[1L]] == "$") as.character(name[[3L]]) else eval(name[[3L]], parent.frame(), parent.frame())
if (length(j) == 1L) {
if (is.character(j)) {
j = match(j, names(k))
if (is.na(j))
stopf("Item '%s' not found in names of input list", origj)
Comment thread
MichaelChirico marked this conversation as resolved.
}
}
.Call(Csetlistelt, k, as.integer(j), x)
} else if (is.environment(k) && exists(as.character(name[[3L]]), k)) {
assign(as.character(name[[3L]]), x, k, inherits=FALSE)
} else if (isS4(k)) {
.Call(CsetS4elt, k, as.character(name[[3L]]), x)
}
.assign_in_parent(
Comment thread
MichaelChirico marked this conversation as resolved.
Outdated
name, x, parent.frame(),
err_msg_len = "The index for recursive assignment must be length 1, but its length is %d.",
err_msg_na = "Item '%s' not found in names of input list"
)
} else if (name %iscall% "get") { # #6725
# edit 'get(nm, env)' call to be 'assign(nm, x, envir=env)'
name = match.call(get, name)
Expand Down
Loading