When I create an environment (intended as hash table) like this
set.seed(1)
key <- do.call(paste0, Map(stringi::stri_rand_strings, n=2e5, length = 16)) # error on n > 49991
val <- sample.int(750, size = 2e5, replace = T)
make_dict <- function(keys, values){
require(rlang)
e <- new.env(size = length(keys))
l <- list2(!!!setNames(values, keys))
list2env(l, envir = e, hash = T)
}
d <- make_dict(key, val)
.rs.describeObject
throws an error protect(): protection stack overflow
when it tries to generate a summary in the environments pane for an environment d
that contains more than 50 000 key value pairs. When I grab some values like this
`%||%` <- function(x,y) if(is.null(x)) y else x
grab <- function(...){
vector("integer", length(..2)) |>
(\(.){. = Vectorize(\(e, x) e[[x]] %||% NA_integer_, list("x"), T, F)(..1, ..2); .})()
}
out <- vector("integer", length(key))
out <- grab(d, sample(key))
It seems the object is fine. Object d
exists and can be accessed ( d$GNZuCtwed3CAgNlU
) and
anyNA(out) | !lobstr::obj_size(out) == lobstr::obj_size(val)
[1] FALSE
Use case
Intend to use this function as a utility function in a package. A minimalistic package is available using remotes::install_github("D-Se/minimal")
. This dummy package passes R CMD check but throws that error when make_dict
is run.
Question
How to properly handle this error?
N.B this question was originally asked on stackoverflow.